Results 1 to 31 of 31

Thread: [RESOLVED]Using Ado To Access Excel Files

  1. #1

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Resolved [RESOLVED]Using Ado To Access Excel Files

    I am trying to read an Excel worksheet into an ADO recordset so that I can read the values from the Excel file. However, I cant seem to get this last part to work.

    I have tried everything I know - even searching the internet - but I cant seem to figure out why when I try to execute this line in the code

    VB Code:
    1. oRs.Open sRequest, sCnn, adOpenDynamic, adLockOptimistic
    I always seem to get the following error:

    The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure that the object exist and that its name and path name correctly.

    I know that Sheet1 exist in the Excels File Path.

    Any other ideas why this error is occurring? - Thanks In Advance

    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     Dim sPath As String
    4.     Dim oRs as ADODB.Recordset
    5.  
    6.     sPath = App.Path & "\Las Animas Text.xls"
    7.    
    8.     Set oRs = GetADOExcelRecordSet(sPath)
    9.    
    10. End Sub
    11.  
    12.  
    13. Private Function GetADOExcelRecordSet(ByVal Path As String, _
    14.                 Optional ByVal Headers As Boolean = True) As Recordset
    15.                
    16.     Dim sCnn As String
    17.     Dim sWorksheet As String
    18.     Dim sRequest As String
    19.    
    20.     sWorksheet = "Sheet1"
    21.    
    22.     Dim oCnn As ADODB.Connection
    23.     Set oCnn = New ADODB.Connection
    24.     Dim oRs As Recordset
    25.     Set oRs = New ADODB.Recordset
    26.    
    27.     sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.             "Data Source=" & Path & ";" & _
    29.             "Extended Properties=""Excel 8.0;HDR=No" & _
    30.             IIf(Headers, "Yes", "No") & """"
    31.            
    32.     'Read a sheet:
    33.  
    34.     sRequest = "SELECT * FROM [" & sWorksheet & "$]"
    35.     oRs.Open sRequest, sCnn, adOpenDynamic, adLockOptimistic
    36.     Set GetADOExcelConnection = oRs
    37.    
    38. End Function
    Last edited by Jumpercables; Jul 27th, 2005 at 01:34 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    You need to inser the "`" character surrounding your sheet name.
    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

  3. #3

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Using Ado To Access Excel Files

    Oh Lordy I guess that fixed one problem - Thanks

    Now I am getting an Syntax Error in Querey : Incomplete Querey Clause Error
    Last edited by Jumpercables; Jul 25th, 2005 at 11:03 PM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Did you place those characters around the sheetname for your select statement too? Also, add the semicolon at the end of your query.
    VB Code:
    1. sRequest = "SELECT * FROM [`" & sWorksheet & "$`];"
    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

  5. #5

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    I forgot the semicolon

    I have exactly what you suggested above but now... I have a Syntax Error from FROM Clause

    I must be too tired to program tonight.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Whats the Debug.Print value for sRequest ?
    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

  7. #7

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    SELECT * FROM [`Sheet1$`];

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Try it without the semicolon and which version of Excel are you running?.
    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

  9. #9

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Running Excel 2002
    Same error without semicolon

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Ok, I got it! You havent opened your connection object. Then you are trying to open a rs with a Cnn
    that is not opened.
    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

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Using Ado To Access Excel Files

    Office XP is 2002, and is version 10.0, not 8.0


    VB Code:
    1. sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    2.             "Data Source=" & Path & ";" & _
    3.             "Extended Properties=""[COLOR=Red]Excel 8.0[/COLOR];HDR=No" & _
    4.             IIf(Headers, "Yes", "No") & """"
    5.                 'Read a sheet:

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Yes but thats not an issue right now. If your not using and connectiong to the workbook then you can have "Excel 888.0;..." and
    it wont make a difference. It will work on Excel 2003 Btw.
    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

  13. #13

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    I know your not going to like this but that still doesn't work.

    You were correct about one thing I did need to open my connection string so good catch there!

    VB Code:
    1. oCnn.Open sCnn
    2.    
    3. 'Read a sheet:
    4. sRequest = "SELECT * FROM [`" & sWorksheet & "$`]"
    5. oRs.Open sRequest, oCnn, adOpenDynamic, adLockOptimistic

    Results in the same error...

    Do Not Worry about this too much - I am going to sleep on it tonight - but thank you for your help!

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Ok, I know its all there. Just missing something minor. Getting late here too.

    Post tomorrow
    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

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Try...
    VB Code:
    1. oRs.Open sRequest, oCnn, adOpenStatic, adLockReadOnly, 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

  16. #16

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    I don't know why I am having such difficulties with this - it doen't seem like it should be this hard to connect and open a sheet. hehe

    When I try the code you suggest I am getting a

    Error:Command Text was not set for the command object.

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Hmm, maybe there is some code of mine that would help you in these two threads.

    http://www.vbforums.com/showthread.p...ight=ADO+Excel
    http://www.vbforums.com/showthread.p...ight=ADO+Excel
    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

  18. #18

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Perhaps I will try to connect through ODBC Connection instead.

    Thank you for your help thus far.

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    If this is not a data sensitive workbook, could you zip it up and attach it so I can test with your actuall workbook? I'm not able
    to create any errors. That sounded funny.
    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

  20. #20

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Let me scan through the excel sheets to check if there is any sensitive data. I dont believe there is any however the current excel file is massive.

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    You could just attach a copy of the workbook but delete all sheets but sheet1.
    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

  22. #22

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    I have attached the TestFile.xls into the .rar file. Eventaully I am just trying to read all of row 5.


    I am attempting to read the code with the following code.
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     Dim sPath As String
    4.     Dim oRs as ADODB.Recordset
    5.  
    6.     sPath = App.Path & "\TestFile.xls"
    7.    
    8.     Set oRs = GetADOExcelRecordSet(sPath)
    9.    
    10. End Sub
    11.  
    12.  
    13. Private Function GetADOExcelRecordSet(ByVal Path As String, _
    14.                 Optional ByVal Headers As Boolean = True) As Recordset
    15.                
    16.     Dim sCnn As String
    17.     Dim sWorksheet As String
    18.     Dim sRequest As String
    19.    
    20.     sWorksheet = "Sheet1"
    21.    
    22.     Dim oCnn As ADODB.Connection
    23.     Set oCnn = New ADODB.Connection
    24.     Dim oRs As Recordset
    25.     Set oRs = New ADODB.Recordset
    26.    
    27.     sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.             "Data Source=" & Path & ";" & _
    29.             "Extended Properties=""Excel 8.0;HDR=No" & _
    30.             IIf(Headers, "Yes", "No") & """"
    31.            
    32.     'Read a sheet:
    33.  
    34.     sRequest = "SELECT * FROM [" & sWorksheet & "$]"
    35.     oRs.Open sRequest, sCnn, adOpenDynamic, adLockOptimistic
    36.     Set GetADOExcelConnection = oRs
    37.    
    38. End Function
    Attached Files Attached Files

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Can you attach a .zip instead. I dont have WinRar.
    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

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Your still not opening a ADODB connection and placing the reverse single quote around the sheet name.
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     Dim sPath As String
    4.     Dim oRs as ADODB.Recordset
    5.  
    6.     sPath = App.Path & "\TestFile.xls"
    7.    
    8.     Set oRs = GetADOExcelRecordSet(sPath)
    9.    
    10. End Sub
    11.  
    12.  
    13. Private Function GetADOExcelRecordSet(ByVal Path As String, _
    14.                 Optional ByVal Headers As Boolean = True) As Recordset
    15.                
    16.     Dim sCnn As String
    17.     Dim sWorksheet As String
    18.     Dim sRequest As String
    19.    
    20.     sWorksheet = "Sheet1"
    21.    
    22.     Dim oCnn As ADODB.Connection
    23.     Set oCnn = New ADODB.Connection
    24.     Dim oRs As Recordset
    25.     Set oRs = New ADODB.Recordset
    26.    
    27.     sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.             "Data Source=" & Path & ";" & _
    29.             "Extended Properties=""Excel 8.0;HDR=No" & _
    30.             IIf(Headers, "Yes", "No") & """"
    31.            
    32.     oCnn.ConnectionString = sCnn
    33.     oCnn.Open
    34.  
    35.     'Read a sheet:
    36.  
    37.     sRequest = "SELECT * FROM [`" & sWorksheet & "$`]"
    38.     oRs.Open sRequest, oCnn, adOpenDynamic, adLockOptimistic
    39.     Set GetADOExcelConnection = oRs
    40.    
    41. End Function
    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

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Using Ado To Access Excel Files

    Dave's conversion service.
    Attached Files Attached Files

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Too bad that thread was toasted. I cant remember that post you made. It would be good for your signature.

    Let me see what going on here now, but the code I posted whould work on this so hopefully not too long tofix.
    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

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Using Ado To Access Excel Files

    Done! The function name was not matching to its signature.

    GetADOExcelRecordSet <> GetADOExcelConnection

    It didnt know where to return it to since it didnt match.

    Also, no need for the reverse single quotes in this situation.

    I added my code I already posted to establish the connection too.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.    
    5.     Dim sPath As String
    6.     Dim oRs As ADODB.Recordset
    7.  
    8.     sPath = App.Path & "\TestFile.xls"
    9.    
    10.     Set oRs = GetADOExcelRecordSet(sPath)
    11.     oRs.MoveFirst
    12.     oRs.Move 5
    13.     MsgBox oRs.Fields(1).Value '6th row, column B
    14.    
    15. End Sub
    16.  
    17.  
    18. Private Function [b]GetADOExcelRecordSet[/b](ByVal Path As String, _
    19.                 Optional ByVal Headers As Boolean = True) As Recordset
    20.                
    21.     Dim sCnn As String
    22.     Dim sWorksheet As String
    23.     Dim sRequest As String
    24.    
    25.     sWorksheet = "Sheet1"
    26.    
    27.     Dim oCnn As ADODB.Connection
    28.     Set oCnn = New ADODB.Connection
    29.     Dim oRs As Recordset
    30.     Set oRs = New ADODB.Recordset
    31.    
    32.     sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    33.             "Data Source=" & Path & ";" & _
    34.             "Extended Properties=""Excel 8.0;HDR=No" & _
    35.             IIf(Headers, "Yes", "No") & """"
    36.            
    37.     oCnn.ConnectionString = sCnn
    38.     oCnn.Open
    39.  
    40.     'Read a sheet:
    41.  
    42.     sRequest = "SELECT * FROM [" & sWorksheet & "$]"
    43.     oRs.Open sRequest, oCnn, adOpenDynamic, adLockOptimistic
    44.     Set [b]GetADOExcelRecordSet[/b] = oRs
    45.    
    46. End Function
    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

  28. #28

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Opps I sent you the wrong code... I was screwing around with the code when I copied paste the wrong thing. In the orgininal code the function call and function have the correct names.

    So I thought - but I will double check my code at lunch.

    But even if its not the correct return signature I believe it gives me the error before I even try to return it.

    VB Code:
    1. oRs.Open sRequest, oCnn, adOpenDynamic, adLockOptimistic

    On this line of code it blows up on me giving me that Error: Syntax Error in FROM Clause.
    Last edited by Jumpercables; Jul 27th, 2005 at 10:09 AM.

  29. #29

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Let me regroup after lunch - I will get back to you I sent you the wrong snippet of code and that will give me time to try your suggestion.

  30. #30

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Using Ado To Access Excel Files

    Well I guess that was my problem - Thanks Alot your so smart :frog:

  31. #31
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED]Using Ado To Access Excel Files

    Thanks but it just takes time and experience. I have done this many times in my apps, passing ADO recordsets back and forth so I can do it blindfolded practically.
    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