I am getting error message when executing these statements after upgrading to Office 2010. This works perfectly fine in Office 2007.
Can someone help me identify what's going on and how to fix this issues.
I am trying to read data from an excel sheet into another excel file.
I am attaching a screenshot of the error message I am getting.
I am running Windows 7 Enterprise SP1 64 bit OS.
Office Standard 2010 - 32bit.
Private Sub GetData(ByVal SrcFile$)
Dim SrcSheet$, SrcRange$
Dim sTgt As String
Dim rTgt As Range
Dim fHdr As Boolean
SrcSheet$ = "EXPORT DATA"
SrcRange$ = "RAW_DATA"
sTgt = Replace(ActiveCell.Address, "$", "")
Set rTgt = ActiveSheet.Range(sTgt)
fHdr = False
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim a&
Dim cnct$
On Error GoTo err_GetData
' Initialize a variable for the connection string
cnct$ = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & SrcFile$
Set cn = New ADODB.Connection
With cn
.Open cnct$
Set rs = .Execute("SELECT * FROM [" & SrcRange$ & "]")
End With
' Initialize a variable for the upper left cell of the target range
Set rTgt = rTgt.Cells(1)
With rs
' Determine whether to get the field header
If fHdr Then
' Loop across the fields
For a& = 0 To .Fields.Count - 1
' Get the field names
rTgt.Offset(0, a&).Value = .Fields(a&).Name
Next a&
' Advance the target pointer to the next available row in the
' destination worksheet
Set rTgt = rTgt.Offset(1, 0)
End If
' Apply the CopyFromRecordset method
rTgt.CopyFromRecordset rs
' Close the RecordSet
.Close
End With
' Close the database connection
cn.Close
err_GetDataExit:
' Recover memory from object variables
Set cn = Nothing
Set rs = Nothing
Exit Sub
err_GetData:
If Err.Number = -2147217842 Then GoTo err_GetDataExit
'MsgBox "'Measurement Data' Workbook Must NOT Be Open", , "CHECK EXCEL WORKBOOKS:"
MsgBox Err.Description, , Err.Number
GoTo err_GetDataExit
End Sub
Bookmarks