Results 1 to 11 of 11
  1. #1
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30

    Inserting data and retrieving fields

    Afternoon,



    So after lots of very helpful advice form this forum!

    i have the below code which inserts data and then i want to retrieve the value in the first two columns but firstly it says "you must use dbseechanges" but i have put this into the code you may see below,

    Code:
     Dim db As DAO.Database
    Dim rs As DAO.Recordset
    dim sSQL as string 
    
    
    sSQL = "INSERT INTO claims (Clientcc, DriverCc, Crefname, Crefcc, creg, cadt) VALUES ('" & ClientCc & "', '" & DriverCc & "', '" & CrefName & "', '" & CrefCc & "', '" & Creg & "', '" & DOA & "')"
    
    
    Set db = CurrentDb
    
    
    db.Execute sSQL
    
    
    Set rs = db.OpenRecordset("claims", , dbSeeChanges)
    
    
    CClc = rs.Fields(0)
    Debug.Print rs.Fields(0)
    
    
    CClaimref = rs.Fields(1)
    Debug.Print CClaimref
    
    
    
    
    rs.Close
    Set rs = Nothing
    
    
    db.Close
    Set db = Nothing
    Once again big thanks to anyone that helps!

  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,641
    Try

    Set rs = db.OpenRecordset("claims", dbOpenDynaset, dbSeeChanges)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    Try

    Set rs = db.OpenRecordset("claims", dbOpenDynaset, dbSeeChanges)
    Ahh that solved that problem!

    you are a star!!!

    How do i set it to return the values of the data i just entered and not the 'first' record?

    Really appreciate all your help.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    If you're trying to add a record and then return values from it (one would ask why, since you already have the values), I'd do it all with a recordset. Here's an example and then getting the autonumber from the record just added:

    Code:
            With rsMaster
              .AddNew
              !Field1 = 123
              !Field2 = "abc"
              .Update
              .Bookmark = .LastModified
              lngARKey = !AR_Key
            End With
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    If you're trying to add a record and then return values from it (one would ask why, since you already have the values), I'd do it all with a recordset. Here's an example and then getting the autonumber from the record just added:

    Code:
            With rsMaster
              .AddNew
              !Field1 = 123
              !Field2 = "abc"
              .Update
              .Bookmark = .LastModified
              lngARKey = !AR_Key
            End With
    Thanks again.

    The fields i want to return one is the primary key and the other is a number generated by a function in the sql server. so they arent values i already have.

  6. #6
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    got it working so once again massive thanks for your help!

    im having an issue i haven't experienced before when the new record in the sub-form is created i cant leave that record, i can move the courser etc but cannot click any where.

    Regards

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    Happy to help!
    Once again i am struggling, for some reason it will not pull the latest record seems that it is pulling a random record (but does increment the record it pulls one up each time)

    Code:
    Dim db As DAO.DatabaseDim rs As DAO.Recordset
    
    
    
    
    Set db = CurrentDb
    
    
    'db.Execute sSQL
    
    
    Set rs = db.OpenRecordset("claims", dbOpenDynaset, dbSeeChanges)
    
    
    With rs
    rs.AddNew
    rs!ClientCc = CClientCc
    rs!DriverCc = CDriverCc
    rs!CrefName = CCrefCc
    rs!Creg = CCreg
    rs!Cadt = CDOA
    rs.Update
    rs.Move 0, rs.LastModified
    'get the values to return
    
    
    CClc = rs!CLc
    CClaimref = rs!ClaimRef
    rs.Close
    db.Close
            
    
    
    
    
    Debug.Print CClc
    
    
    
    
    Debug.Print CClaimref
    
    
    End With
    
    
    Set rs = Nothing
    
    
    Set db = Nothing
    So in short it is pulling data but not from the record i have just added.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You didn't use the method I posted to get to that record for some reason.

    .Bookmark = .LastModified
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Quote Originally Posted by pbaldy View Post
    You didn't use the method I posted to get to that record for some reason.

    .Bookmark = .LastModified
    I used that to start of with and it returns the same values. Was testing a few options.

    Could this be related to the save issue i am having above?

    Maybe i will rebuild the subform and table and see if there is just somthing there that is causing the issues

  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,641
    I don't think so. The .Update saves the new record, the .Bookmark line should go to the just-saved record. Can you post a sample db where it doesn't?
    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. Web retrieving of Data
    By drunkenneo in forum Programming
    Replies: 1
    Last Post: 05-23-2014, 01:38 AM
  2. Replies: 3
    Last Post: 07-10-2013, 10:29 AM
  3. Replies: 7
    Last Post: 05-23-2012, 02:19 PM
  4. Replies: 2
    Last Post: 11-02-2011, 08:10 PM
  5. Retrieving data on my website
    By kattys in forum Access
    Replies: 1
    Last Post: 04-27-2010, 05:02 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