Getting Data from Excel (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 06:17
Joined
Oct 30, 2008
Messages
1,257
Code:
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Dim objConnection
Dim objRecordSet
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Scripts\Test.xls;" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"";" 

objRecordset.Open "Select * FROM [Sheet1$]", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

stop

Do Until objRecordset.EOF
    Debug.Print objRecordset.Fields.Item("Name"), _
        objRecordset.Fields.Item("Number")
    objRecordset.MoveNext
Loop

Using the code above (as far as the Stop). Have tried with and without HDR
and column names but I get errors

No value given for one or more required parameters.
or
Too many fields defined.

How do I name and define just a few fields in the sql please?
 

JHB

Have been here a while
Local time
Today, 20:17
Joined
Jun 17, 2012
Messages
7,732
Try the below, put in the column names you want instead of [NameOfXTheColumn]:
Code:
objRecordSet.Open "Select [NameOf1TheColumn], [NameOf2TheColumn], [NameOf3TheColumn] FROM ..."
 

kirkm

Registered User.
Local time
Tomorrow, 06:17
Joined
Oct 30, 2008
Messages
1,257
Thanks JHB, I thought I'd tried that but must have done something wrong as it's working fine now.
 

JHB

Have been here a while
Local time
Today, 20:17
Joined
Jun 17, 2012
Messages
7,732
You're welcome, good you got it working now.
 

Users who are viewing this thread

Top Bottom