Results 1 to 2 of 2

Thread: [RESOLVED] select statement in an excel file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006

    Resolved [RESOLVED] select statement in an excel file

    VB Code:
    1. Set cn = New ADODB.Connection
    2. With cn
    3.    .Provider = "MSDASQL"
    4.    .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
    5.     "DBQ=" & Text1.Text & "; ReadOnly=False;"
    6.    .Open
    7. End With
    8. strQuery = "Select * from [sheet1$]"
    10. Set rs = New ADODB.Recordset
    11. rs.Open strQuery, cn
    13. For i = 1 To rs.RecordCount
    14.     account(i) = rs.Fields(i).Value
    15.     rs.MoveNext
    16.     Next i

    why is that the recordset is -1??

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™

    Re: select statement in an excel file

    Because without passing the rest of the arguments you will be opening a forward-only and read-only recordset that doesnt support the recordcount property.

    Pass these args

    VB Code:
    1. rs.Open strQuery, cn, adOpenKeyset, adLockOptimistic, adCmdText
    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

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