I have an excel sheet and woudl like to import it into a SQL table using VB6. I am getting the error "The Microsoft Jet Database Engine could not find the object 'Sheet1$'"
The worksheet IS called Sheet1 in my file, so there is no misspelled information.
Thanks.
Code:
Option Explicit
Global Const g_strConnDB = "Provider=sqloledb;Data Source=sql1;Initial Catalog=_Hytek; User Id=sa;Password=;"
Global Const g_strConnXL = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DATA_test.xls;" & _
"Extended Properties=Excel 8.0;"
Dim g_oCnnDB As ADODB.Connection 'SQL
Dim g_oCnnXL As ADODB.Connection 'Excel
'--------------------------------------------
Public Function ImportDataTest()
Dim oRsetXL As ADODB.Recordset
Dim oRsetDB As ADODB.Recordset
Dim oCmd As ADODB.Command
Dim i As Integer
On Error GoTo ErrHandler
'Establish connection to SQL database
Set g_oCnnDB = New ADODB.Connection
g_oCnnDB.Open g_strConnDB
Set oRsetDB = New ADODB.Recordset
oRsetDB.Open "DataDetailTable", g_oCnnDB, adOpenDynamic, adLockOptimistic
'Establish connection to Excel worksheet
Set g_oCnnXL = New ADODB.Connection
g_oCnnXL.Open g_strConnXL
Set oRsetXL = New ADODB.Recordset
Set oCmd = New ADODB.Command
oCmd.ActiveConnection = g_oCnnXL
oCmd.CommandText = "SELECT * FROM [Sheet1$]"
oRsetXL.Open oCmd, , adOpenStatic, adLockOptimistic
oRsetXL.MoveFirst
Do Until oRsetXL.EOF
MsgBox oRsetXL.Fields(0)
oRsetXL.MoveNext
Loop
Debug.Print oRsetXL.RecordCount
MsgBox "recordcount = " & i
Set oRsetXL = Nothing
Set oRsetDB = Nothing
Exit Function
ErrHandler:
MsgBox Err.Number & " -> " & Err.Description
End Function
The worksheet IS called Sheet1 in my file, so there is no misspelled information.
Thanks.