Jeff Slavin
New Member
Using Chandoo's code in his artile 'Using Excel As Your Database'. It seems the database connection never closes, so need help figuring this out.
I changed his code so my dashboard.xlsx is reading data from a separate data.xlsx file. Here is the code I'm running (open the database, read the data, close the database). This code is in my dashboard.xlsm file:
After I run this code, if I navigate to my data.xlsx file and double click it to open, i get the 'File In Use' message and can only open as read-only. It appears Excel/Windows thinks the data.xlsx file is still in use even though I've closed all the connection in my code. Any idea how to make sure data.xlsx gets closed completely after the above code runs to ensure others can update data.xlsx?
Thanks in advance!!
Jeff Slavin
jmslavin@gmail.com
I changed his code so my dashboard.xlsx is reading data from a separate data.xlsx file. Here is the code I'm running (open the database, read the data, close the database). This code is in my dashboard.xlsm file:
Code:
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
'Setup connection with the data.xlsx file
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & "data.xlsx"
cnn.Open
strSQL = "Select Distinct [Name] From [Sheet1$] Order by [Name]"
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Do While Not rs.EOF
MsgBox rs.Fields(0)
rs.MoveNext
Loop
Else
MsgBox "I was not able to find any unique Names.", vbCritical + vbOKOnly
Exit Sub
End If
rs.Close
rs.CursorLocation = adUseClient
cnn.Close
Set rs = Nothing
Set cnn = Nothing
After I run this code, if I navigate to my data.xlsx file and double click it to open, i get the 'File In Use' message and can only open as read-only. It appears Excel/Windows thinks the data.xlsx file is still in use even though I've closed all the connection in my code. Any idea how to make sure data.xlsx gets closed completely after the above code runs to ensure others can update data.xlsx?
Thanks in advance!!
Jeff Slavin
jmslavin@gmail.com
Last edited: