Results 1 to 8 of 8

Thread: ADO Recordset returning blank fields *RESOLVED*

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    ADO Recordset returning blank fields *RESOLVED*

    Hey everyone,

    I've got the following code:

    Code:
    Set rsMyRS = cn.execute("SELECT * FROM INDEX")
    Do until rsMyRS.EOF
        Debug.Print(rsMyRs.Fields(26).Value)
    
         rsMyRS.MoveNext
    Loop
    Where cn is a connection to an SQL Server running on my computer.

    The problem is that this prints out a blank, even though I can go look at the table in Ent. Manager and see that the field has a value in it. After I play around with the recordset object in the Debug window, sometimes I can get it to display the real value, but not always.

    Anyone know why it would return a blank? It's not even returning NULL, but just a blank string.

    Thanks!
    Last edited by BenFinkel; Apr 29th, 2004 at 10:07 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    If you're only looking for Fields(26).Value 9whatever that field is) then simply specify its name in the select instead:
    VB Code:
    1. Set rsMyRS = cn.execute("SELECT SomeField FROM INDEX")
    2. Do until rsMyRS.EOF
    3.     Debug.Print "" & rsMyRs!SomeField 'or use IsNull() to handle nulls
    4.      rsMyRS.MoveNext
    5. Loop
    Note: personally, I never use syntax such Fields(Index) as it's too easy to be off and get wrong results.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Also, change the table name from INDEX to something else. Its
    not good programming to name user defied objects the same
    name as system objects.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    maybe if you set the CursorLocation to "your side"

    VB Code:
    1. rsMyRS.CursorLocation = adUseClient 'Set the cursor to your side
    2.  
    3. Set rsMyRS = cn.execute("SELECT * FROM INDEX")
    4. Do until rsMyRS.EOF
    5.     Debug.Print(rsMyRs.Fields(26).Value)
    6.  
    7.      rsMyRS.MoveNext
    8. Loop
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Yeah I fixed this type of problem messing with the cursor location of the connection object as well - although in my case - switching to server side did the trick. Maybe play around with that a bit.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    Robdog, Rhinobull,

    The example I gave was a trimmed down version of the code that explicitly showed the error I was receiving. My table isn't really named INDEX and I'm not really using the field index to reference the field. I was looking for some information that pertained specifically to the field returning blank data when I knew for a fact that there was data in it.


    *RESOLUTION*

    Turns out what I needed to do (and this may relate to the Cursor location) was use the Recordset.Open method instead of the Connection.Execute method and it worked find. It also runs much more quickly, so I'm assuming that the Open method somehow loads the data locally while the Execute method continually re-connects to the database.

    --Ben

  7. #7
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Indeed, the open method places the cursor location (server side I believe) differently than the execute method which if memory serves puts the cursor client side (might be the other way around - my dislexia aside!! )
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Despite the obvious difference - location of your cursor has nothing to do with reading data from field as long as field name (or index) is correct.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width