Using the code below I retrieve a Recordset from the Excel file and can run and run this code and it works each time. My problem lies in freeing up the Excel file to be read by other users following import of the recordset. Once I've run this, trying to access the file via Excel reports that the file is in use. I think I free up the reference to Excel at the end of the first procedure but must be making a fundamental mistake somewhere?
Private Sub
Set Excel = New Excel.Application
Set Workbook = Excel.Workbooks.Open("U:\CIS Details\CISs.xls")
Workbook.Activate
Set Worksheet = Workbook.Sheets("CIS List")
Dim X As Integer
With Worksheet
m_sHere = Chr(64 + ActiveCell.SpecialCells(xlLastCell).Column) & ActiveCell.SpecialCells(xlLastCell).Row
End With
MsgBox m_sHere
Here2
Set Worksheet = Nothing
Set Workbook = Nothing
Set Excel = Nothing
' Set db = DBEngine.OpenDatabase("U:\CIS Details\CIS.mdb")
End Sub
Private Sub Here2()
sTableName = "[CIS List$A1:" & m_sHere & "]"
'Open the ADO connection to the Excel workbook
Set oConn = New ADODB.Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "U:\CIS Details\CISs.xls" & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;"""
Set oRs = New ADODB.Recordset
oRs.Open "SELECT * FROM " & sTableName, oConn, adOpenStatic, adLockOptimistic
oRs.MoveLast
nRows = oRs.RecordCount
nCols = oRs.Fields.Count
End Sub
Private Sub
Set Excel = New Excel.Application
Set Workbook = Excel.Workbooks.Open("U:\CIS Details\CISs.xls")
Workbook.Activate
Set Worksheet = Workbook.Sheets("CIS List")
Dim X As Integer
With Worksheet
m_sHere = Chr(64 + ActiveCell.SpecialCells(xlLastCell).Column) & ActiveCell.SpecialCells(xlLastCell).Row
End With
MsgBox m_sHere
Here2
Set Worksheet = Nothing
Set Workbook = Nothing
Set Excel = Nothing
' Set db = DBEngine.OpenDatabase("U:\CIS Details\CIS.mdb")
End Sub
Private Sub Here2()
sTableName = "[CIS List$A1:" & m_sHere & "]"
'Open the ADO connection to the Excel workbook
Set oConn = New ADODB.Connection
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "U:\CIS Details\CISs.xls" & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;"""
Set oRs = New ADODB.Recordset
oRs.Open "SELECT * FROM " & sTableName, oConn, adOpenStatic, adLockOptimistic
oRs.MoveLast
nRows = oRs.RecordCount
nCols = oRs.Fields.Count
End Sub