Results 1 to 14 of 14
  1. #1
    JAPA1972 is offline Novice
    Windows 7 32bit Access 2016
    Join Date
    Jan 2019
    Posts
    17

    Recordset returns -1

    I have a view in SQL server (VIEW_TRIP_DATES). I am using Microsoft access to calculate the number of records in this table. While I can browse the recordset, I get -1 with the recordcount property. Am I missing something here?


    Thanks

    Public Function GetADOConn() As ADODB.Connection
    Dim conn As ADODB.Connection
    Dim adoConn As String

    Set conn = New ADODB.Connection

    adoConn = "Provider='SQLOLEDB';Data Source='co-nt-dmn6';" & _
    "Initial Catalog='FLD';Integrated Security='SSPI';"



    conn.Open adoConn
    Set GetADOConn = conn
    End Function




    MyCode sub


    Set rst = New ADODB.Recordset
    rst.Open "SELECT TRIP_DATE FROM VIEW_TRIP_DATES", GetADOConn, adOpenForwardOnly


    Debug.Print rst.RecordCount 'returns -1




    rst.MoveNext
    Debug.Print rst.RecordCount 'this also returns -1


    end sub



    Thanks

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    What do you get when you MoveLast?

    Then again, there is always Google?
    https://www.google.com/search?q=get+...obile&ie=UTF-8
    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
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    June7, I belive the issue is the command is not supported?, at least with that driver, which is what that link reports.
    Never used ADO, so cannot be sure.
    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

  5. #5
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Actually, I was going to post that link, but I think the answer is in paragraph 3, not 4.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    I was going on just the first block of text at that link.
    In fact if one select that firts link https://adodb.org/dokuwiki/doku.php?...he%20operation. more detail is available.
    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

  7. #7
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    Code:
    
    Public Function GetADOConn() As ADODB.Connection
    Dim conn As ADODB.Connection
    Dim adoConn As String
    
    
    Set conn = New ADODB.Connection
    
    
    adoConn = "Provider='SQLOLEDB';Data Source='co-nt-dmn6';" & _
    "Initial Catalog='FLD';Integrated Security='SSPI';"
    
    
    
    
    
    
    conn.Open adoConn
    Set GetADOConn = conn
    End Function
    
    
    
    
    
    
    
    
    MyCode sub
    
    
    
    
    Set rst = New ADODB.Recordset
    With rst
       .CursorType = adOpenDynamic ' This is for a Dynaset
       .LockType = adLockOptimistic ' Allows updating records
       .Open "SELECT TRIP_DATE FROM VIEW_TRIP_DATES", GetADOConn
       if not (.BOF and .EOF) Then
           .MoveLast
           .MoveFirst
       End If
    End With
    Debug.Print rst.RecordCount 
    
    
    
    
    
    
    
    
    rst.MoveNext
    Debug.Print rst.RecordCount 'this also returns -1
    
    
    
    
    end sub

  8. #8
    JAPA1972 is offline Novice
    Windows 7 32bit Access 2016
    Join Date
    Jan 2019
    Posts
    17
    Quote Originally Posted by Welshgasman View Post
    What do you get when you MoveLast?

    Then again, there is always Google?
    https://www.google.com/search?q=get+...obile&ie=UTF-8

    Thanks for your response. If I set it to MoveLast I get this error. "Rowset does not support fetching backward "

  9. #9
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    Why are you using a recordset for this at all?
    Why not just do a COUNT()?

    SELECT COUNT(*) FROM MyTable... sort of thing.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Rowset does not support fetching backward
    Is that because of "adOpenForwardOnly"
    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

  11. #11
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    adOpenForwardOnly should be something like adOpenStatic. You're not trying to move through the recordset at all. You want it to be static so you can get a count. But really, no, you don't want to use a recordset for this at all. Ever. Maybe write a function in SQL or write a stored procedure that filters a bunch of data and then passes the recordcount in an output parameter.

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    @ JAPA1972
    Can you please try this:
    Code:
    MyCode sub
    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseClient 'Vlad -add this line before rst.open
    rst.Open "SELECT TRIP_DATE FROM VIEW_TRIP_DATES", GetADOConn, adOpenForwardOnly
    Debug.Print rst.RecordCount 'returns -1
    rst.MoveNext
    Debug.Print rst.RecordCount 'this also returns -1
    end sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If all you want is a count, do as madpiet suggested in post 9, or use DCount() domain aggregate function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    Best practice when using SQL server as backend: pull as less as possible data over the net. So the solution #11 given by madpiet seems the best to me.
    Second best: link the view as a table to your access front end. No need to open the connection anymore.
    And if you open a new connection to SQL server with VBA, don't forget to close it! A few years ago I had to kick a developer out of my database and remove all rights because of the bad way the program was written (opening a new connection with a new SPID every minute without closing it crashed the server).

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

Similar Threads

  1. Replies: 14
    Last Post: 09-04-2024, 12:38 AM
  2. DOA Recordset RecordCount returns wrong value
    By GraeagleBill in forum Programming
    Replies: 2
    Last Post: 06-12-2021, 11:54 AM
  3. OnLoad - Recordset Method returns runtime error 91
    By Valentino in forum Programming
    Replies: 7
    Last Post: 03-24-2020, 07:18 AM
  4. Replies: 1
    Last Post: 11-13-2009, 03:03 AM
  5. Access 2003 returns empty recordset
    By Leelers in forum Queries
    Replies: 0
    Last Post: 03-20-2009, 11:11 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