Connecting to Excel From Access

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,052
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I have the following which works conditionally

VBA Code:
Sub ConnectToExcel()
'Ref needed to Microsoft ActiveX Data Objects 6.1 Library
Dim strSQL  As String, conStr As String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim ExcelFile As String
ExcelFile = "J:\Private\AcessImports\test1.xlsx"

conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ExcelFile & "';" & _
             "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"

strSQL = "SELECT Prefix, Category, Type FROM [Sheet1$] " 'WHERE [Thing1] > 1"

cnn.Open conStr
rs.Open strSQL, cnn, adOpenStatic, adLockOptimistic, adCmdText
End Sub

The problem is strSQL. The above works as I know the Excel Column names. I can also use "*"
I remember a situation where you could have e.g. "SELECT [F1], [F2], [F3], Category, Type FROM [Sheet1$] "
but this gives error No value given for one or more required parameters.
I'm trying to figure out why.
Thanks for any advice.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top