E
EricG
I have a very simple Workbook_Open routine that is supposed to grab some data
from another Excel workbook. I'm not doing anything with the data yet,
because I can't get past the automation error I'm getting. The error is:
"The Microsoft Jet database engine could not find the object 'Sheet2$'.
Make sure the object exists and that you spell its name and the path name
correctly."
Sheet2 definitely exists and there is data on it. Does anyone see an error
in my code below, or is there some other source for the error. The error
occurs on the line marked with **************.
Thanks,
Eric
Option Explicit
Private Const masterFile = "D:\data\thisUser\Desktop\MasterFile.xls"
Private Sub Workbook_Open()
Dim conData As New ADODB.Connection
Dim rstAssigns As New ADODB.Recordset
'
Dim intCount As Integer
Dim strSelect As String
Dim strResults As String
'
With conData
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & masterFile & _
";Extended Properties=Excel 8.0;Persist Security Info=False"
.ConnectionTimeout = 30
.Open
End With
'
strSelect = "SELECT * FROM [Sheet2$]"
'
On Error GoTo Oops
'
rstAssigns.ActiveConnection = conData
rstAssigns.Source = strSelect
rstAssigns.Open '***************** This does not work.
On Error GoTo 0
'
Do While Not rstAssigns.EOF
For intCount = 0 To rstAssigns.Fields.Count - 1
' Do stuff with the data here...
Next
rstAssigns.MoveNext
Loop
'
conData.Close
Exit Sub
'
Oops:
Debug.Print "Oops! Something went wrong."
Debug.Print Err.Description
End Sub
from another Excel workbook. I'm not doing anything with the data yet,
because I can't get past the automation error I'm getting. The error is:
"The Microsoft Jet database engine could not find the object 'Sheet2$'.
Make sure the object exists and that you spell its name and the path name
correctly."
Sheet2 definitely exists and there is data on it. Does anyone see an error
in my code below, or is there some other source for the error. The error
occurs on the line marked with **************.
Thanks,
Eric
Option Explicit
Private Const masterFile = "D:\data\thisUser\Desktop\MasterFile.xls"
Private Sub Workbook_Open()
Dim conData As New ADODB.Connection
Dim rstAssigns As New ADODB.Recordset
'
Dim intCount As Integer
Dim strSelect As String
Dim strResults As String
'
With conData
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & masterFile & _
";Extended Properties=Excel 8.0;Persist Security Info=False"
.ConnectionTimeout = 30
.Open
End With
'
strSelect = "SELECT * FROM [Sheet2$]"
'
On Error GoTo Oops
'
rstAssigns.ActiveConnection = conData
rstAssigns.Source = strSelect
rstAssigns.Open '***************** This does not work.
On Error GoTo 0
'
Do While Not rstAssigns.EOF
For intCount = 0 To rstAssigns.Fields.Count - 1
' Do stuff with the data here...
Next
rstAssigns.MoveNext
Loop
'
conData.Close
Exit Sub
'
Oops:
Debug.Print "Oops! Something went wrong."
Debug.Print Err.Description
End Sub