Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / April 2006

Tip: Looking for answers? Try searching our database.

need help with gotorecord

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
congngo@yahoo.com - 12 Apr 2006 04:07 GMT
Below is my code.  What I want do is ,after running the insert
statement into tblarchive, I want to go to the subform and set focus to
the record that I just added in

   Confirm = "'" + CONF + "'"
   usr = "'" + CurrentUser() + "'"
   strSQL = "INSERT INTO tblarchive ( conf, csrID, csdate, comments )
SELECT " & Confirm  & " AS Expr1, " & usr & " AS Expr2, Date() AS
Expr3, 'cxl' AS Expr4;"
   DoCmd.SetWarnings False
   DoCmd.RunSQL strSQL  (running the insert sql statement)
   DoCmd.SetWarnings True
   DoCmd.Close acForm, "cancel", acSaveYes
   Forms![MAIN-members]![Child135].SetFocus  (set focus to the
subform)
   Forms![MAIN-members]![Child135]!comments.SetFocus
   DoCmd.GoToRecord   (need help here go to the record that just ran
above)

thank
Allen Browne - 12 Apr 2006 07:57 GMT
You might be able to Requery the subform, and then go to the last record.
But if the table does not have an AutoNumber, or if the AutoNumber is not
sequential, or if other users are adding records, this might not take you to
to desired one.

Would you consider an AddNew on the RecordsetClone of the subform instead of
running the update query? That avoids the Requery, and you know for certain
that you are ending up with the correct record.

This kind of thing (assuming the code goes in the main form's module):
   Dim rs As DAO.Recordset
   With Me.Child135.Form
       Set rs = .RecordsetClone
       rs.AddNew
           rs!conf = Confirm
           rs!csrID = use
           rs!csdate = Date()
           rs!comments = "cxl"
       rs.Update
       .Bookmark = rs.LastModified
   End With
   Set rs = Nothing

The alternative would be to Execute the query statement instead of RunSQL,
and ask Access for the new identity value, requery the subform, and then
FindFirst in its RecordsetClone. That's a long way round for a shortcut, and
works in JET 4 only (Access 2000 and later.)

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

> Below is my code.  What I want do is ,after running the insert
> statement into tblarchive, I want to go to the subform and set focus to
[quoted text clipped - 14 lines]
>    DoCmd.GoToRecord   (need help here go to the record that just ran
> above)
congngo@yahoo.com - 12 Apr 2006 18:36 GMT
Thank you for the code but it's keep on jumping back the first record
on the subform
child135.   it does not stay at the record that it just modified.

just curious if I set
    Set rst = CurrentDb.OpenRecordset("tblarchive", dbOpenDynaset)
this should works as well.   is there a preference of RecordsetClone
over
OpenRecordset?

Dim rst As DAO.Recordset
Dim strSQL, Confirm, usr As String

   Confirm = CONF
   usr = CurrentUser()
   DoCmd.Close acForm, "cancel", acSaveYes

   Forms![MAIN-members]![Child135].SetFocus
   Forms![MAIN-members]![Child135]!comments.SetFocus

   With Forms![MAIN-members].Child135.Form
       Set rst = .RecordsetClone
       rst.AddNew
           rst!CONF = Confirm
           rst!csrID = usr
           rst!csdate = DATE
           rst!comments = "cxl"
       rst.Update
       .Bookmark = rst.LastModified
   End With
   Set rst = Nothing
Allen Browne - 13 Apr 2006 01:30 GMT
Jumping back to the first record after setting the form bookmark is not the
normal behavior, so something else must be going on as well.

It could be a Requery, or it might be related to something that happens in
the main form. Access reloads the subform when the main form moves record
(or is requeried.)

If you OpenRecordset on the table and add the record there, the new record
will not be in the form's recordset until a requery. You will then need to
FindFirst in the RecrodsetClone of the of the form, i.e. you cannot match
the Bookmark of the table's recordset against that of the form.

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

> Thank you for the code but it's keep on jumping back the first record
> on the subform
[quoted text clipped - 27 lines]
>    End With
>    Set rst = Nothing
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.