Results 1 to 11 of 11
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919

    How to set focus on record just added

    On a continuous form, how can I set focus on the record just added to the RecordSource recordset.

    Here's the code that adds the record:
    Code:
    strSQL = "INSERT INTO PAYMENTLEDGER (LedID,RetYear,LedDate) VALUES ("
    strSQL = strSQL & UserID & ",'" & gblRetreatYear & "',#" & DATE & "#);"
    CurrentDb.Execute strSQL
    Me.Requery
    I could probably use DoCmd.FindRecord "LedID" if I knew the newly assigned autonumber'd LedID? Maybe use DMax to find the LedID, but all of that seems a bit convoluted so I thought I'd ask here for the simplest approach.

    Thanks,


    Bill

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Hi Bill!

    I'd use a recordset to add the record, grab the ID, then use a bookmark to go to the record. Here's how to get the id (the lines in red are getting the ID):

    Code:
            With rsMaster
              .AddNew
              !FieldName = Whatever
              .Update
              .Bookmark = .LastModified
              Variable = !KeyFieldName
            End With
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    RESET!!!!!!

    Sorry, LedID IS NOT an autonumber field. Rather is the ID of the current entry within the "current ledgers collection" table "PAYMENTLEDGER".

    It is RecID that would be the newly assigned record ID of the new record for "current ledger collection" LedID

  4. #4
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    Paul,
    You mean like this:

    Code:
    Private Sub cmdAddPmt_Click()
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    ' Command to trigger the insertion of a new payment record.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim strSQL As String
    Dim UserID As Integer
    Dim intNewID As Integer
    
    UserID = Forms!frmRosterDetail!tbOpenID      'Parent stored current ID in an invisible text box for our use here
    
    Dim rs As DAO.Recordset
      
        Set rs = DBEngine(0)(0).OpenRecordset("QPaymentLedger")
        
        rs.AddNew
        
        rs!LedID = UserID
        rs.Update
        
        rs!RetYear = gblRetreatYear
        rs.Update
        
        rs!LedDate = DATE
        rs.Update
        
        rs.Bookmark = .LastModified
        intNewID = !RecID
    
        rs.Close
        Set rs = Nothing
    
    Me.Requery
    
    End Sub
    If so, then what? DoCmd.FindRecord intNewID?

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    The ".Edit"s are missing and rs.Bookmark needs to be rs.LastModified

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    More like

    rs.AddNew
    rs!LedID = UserID
    rs!RetYear = gblRetreatYear
    rs!LedDate = DATE
    rs.Update

    rs.Bookmark = .LastModified
    intNewID = !RecID

    Then adapting this to use intNewID after the requery:

    http://www.baldyweb.com/Requery.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    Paul,
    Revisions work perfectly as you've suggested. I like your method of adding new records. I've had other instances where the SQL statements with mixed data types have been a bugger to debug because of syntax among the values.

    Code:
    Private Sub cmdAddPmt_Click()
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    ' Command to trigger the insertion of a new payment record.
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim strSQL As String
    Dim UserID As Integer
    Dim lngNewID As Integer
    
    UserID = Forms!frmRosterDetail!tbOpenID      'Parent stored current ID in an invisible text box for our use here
    
    Dim rs As DAO.Recordset
      
        Set rs = DBEngine(0)(0).OpenRecordset("QPaymentLedger")
        
        rs.AddNew
        rs!LedID = UserID
        rs!RetYear = gblRetreatYear
        rs!LedDate = DATE
        rs.Update
        
        rs.Bookmark = rs.LastModified
        lngNewID = rs!RecID
    
        rs.Close
        Set rs = Nothing
    
    Me.Requery
    
        With Me.RecordsetClone
          .FindFirst "RecID = " & lngNewID
          Me.Bookmark = .Bookmark
        End With
    
    End Sub

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help Bill! Let's hope we get some snow in this upcoming storm.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    We got between 9 and 10 inches here Sunday night............... really wet stuff.

  10. #10
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    1,919
    The Feather river looks like the Mississippi from all the rain from the previous days. California has a long way to go, but moisture much improved compared to the previous 3 or 4 years.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Great! We need to fill the lakes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Newly added record Search
    By galadrwin in forum Access
    Replies: 2
    Last Post: 02-10-2015, 06:27 AM
  2. Replies: 3
    Last Post: 05-13-2012, 07:31 AM
  3. Blank Record Being Added to Table
    By Andrew in forum Programming
    Replies: 8
    Last Post: 12-22-2011, 04:41 AM
  4. New record not being added to main form
    By hmcquade in forum Forms
    Replies: 10
    Last Post: 07-21-2011, 10:07 AM
  5. Replies: 5
    Last Post: 06-29-2010, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums