Results 1 to 8 of 8
  1. #1
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149

    RecordSet only read the first record

    I have 2 records in vDayTran.
    When I debug below coding, it will go thought the loop 2 times then exit.
    When first jump into the loop, i get the values from the vDayTran first record.
    But in the second loop, i get the same values as the vDayTran first record, not the values from the second record.

    Code:
    Set gblDb = CurrentDb
    Set gblRst = gblDb.OpenRecordset("SELECT * FROM vDayTran", dbOpenDynaset, dbSeeChanges)
    Do Until gblRst.EOF
        DoCmd.RunSQL ("Insert into Tranlog VALUES ......")
        gblRst.MoveNext
    Loop
    gblRst.Close
    
    Set gblRst = Nothing
    gblDb.Close
    
    Set gblDb = Nothing


  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Put a debug.print in the loop before the docmd and post back the results here.
    Also show us a pic of the table vDayTran
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Since you instantiate a db object you might as well use it instead of runsql.
    As GasMan stated use debug.print as shown to see what it resolves to.


    Code:
    Dim strSql as string
    
    Set gblDb = CurrentDb
    Set gblRst = gblDb.OpenRecordset("SELECT * FROM vDayTran", dbOpenDynaset, dbSeeChanges)
    
    Do Until gblRst.EOF
    
        strSql = "Insert into Tranlog VALUES ......"
        debug.print strSql
    
        gblDb.Execute strSql, DbFailOnError
    
        gblRst.MoveNext
    
    Loop
    
    gblRst.Close
    Set gblRst = Nothing
    gblDb.Close
    Set gblDb = Nothing
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    There's no field list for the INSERT INTO ??
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Micron View Post
    There's no field list for the INSERT INTO ??
    I took that to be just rubbish text to indicate what was meant to happen, concentrating on the changing value, which we still do not know?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  7. #7
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This is how I would write the snippet of code posted by the OP:
    Code:
        Dim sSQL As String
    
        Set gblDb = CurrentDb
    
        Set gblRst = gblDb.OpenRecordset("SELECT * FROM vDayTran", dbOpenDynaset, dbSeeChanges)
        If Not gblRst.BOF And Not gblRst.EOF Then
            gblRst.MoveLast
            Debug.Print "Number of records = " & gblRst.RecordCount
            gblRst.MobeFirst
    
            Do Until gblRst.EOF
                sSQL = "Insert into Tranlog VALUES ......"
                Debug.Print sSQL
                gblRst.Execute sSQL, dbFailOnError + dbSeeChanges
                '           DoCmd.RunSQL ("Insert into Tranlog VALUES ......")
                gblRst.MoveNext
            Loop
        End If
    
        gblRst.Close
    
        Set gblRst = Nothing
        '   gblDb.Close
    
        Set gblDb = Nothing
    (I added the lines in blue)


    " gblDb.Close" should not be there.

    The rule is
    If you open it, close it
    If you create it, destroy it

    You created the recordset, so you destroy it ( Set gblRst = Nothing)
    You opened the recordset, so you close the recordset ( blRst.Close)


    You created the application object, so you destroy the application object ( Set gblDb = Nothing)
    BUT you did not open the application object, therefore you do NOT close it ( ' gblDb.Close)

  8. #8
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    thanks all, specially ssanfu.
    by using debug.print, i make mistake in the SQL statement.
    also thanks to learn to use Execute instead of RunSQL inside db object.

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

Similar Threads

  1. DAO recordset read issues
    By WAVP375 in forum Access
    Replies: 3
    Last Post: 09-29-2020, 11:39 AM
  2. Replies: 4
    Last Post: 08-25-2018, 09:57 AM
  3. Replies: 5
    Last Post: 04-22-2013, 12:32 PM
  4. How to read last record using VBA?
    By chuki2 in forum Programming
    Replies: 7
    Last Post: 12-10-2011, 12:40 PM
  5. VBA Record Set - Read Only
    By Scyclone in forum Programming
    Replies: 8
    Last Post: 10-18-2011, 07:10 AM

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