Christineeve
Programmer
Hi,
I have to sub routines: One to open a connection and one to close a connection.
When I run this code, it keeps telling me that the connection is open and it won't close. But when it tests to see if the connection is closed, it just doesn't seem to evaluate that correctly. It seems like it's not closing the connection.
I'd like to call the connection from a routine, process some VBA and SQL Code then call the closing procedure after I'm finished with the sub routine. Everytime I open my connection, instead of the closing working, I just have to close my database. That's not the desired result. Any help is sincerely appreciated.
Here is my code:
Sub OpenADODatabase()
'Sub routine to open an active database connection.
Dim conn As ADODB.Connection
'Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
Dim myconnection As String
strPath = CurrentProject.Path & "\FM_Dcsh.mdb"
Set conn = New ADODB.Connection
myconnection = conn
conn.Mode = adModeReadWrite
If IsEmpty(myconnection) Then
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = " & strPath
conn.Open
Set rst = New ADODB.Recordset
End If
End Sub
Sub closeADODatabase()
'Sub Routine to close active database connection
If Not IsNull(conn) Then
If Not IsEmpty(conn) Then
conn.Close
conn = Nothing
Set cmd = Nothing
End If
End If
End Sub
I have to sub routines: One to open a connection and one to close a connection.
When I run this code, it keeps telling me that the connection is open and it won't close. But when it tests to see if the connection is closed, it just doesn't seem to evaluate that correctly. It seems like it's not closing the connection.
I'd like to call the connection from a routine, process some VBA and SQL Code then call the closing procedure after I'm finished with the sub routine. Everytime I open my connection, instead of the closing working, I just have to close my database. That's not the desired result. Any help is sincerely appreciated.
Here is my code:
Sub OpenADODatabase()
'Sub routine to open an active database connection.
Dim conn As ADODB.Connection
'Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
Dim myconnection As String
strPath = CurrentProject.Path & "\FM_Dcsh.mdb"
Set conn = New ADODB.Connection
myconnection = conn
conn.Mode = adModeReadWrite
If IsEmpty(myconnection) Then
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = " & strPath
conn.Open
Set rst = New ADODB.Recordset
End If
End Sub
Sub closeADODatabase()
'Sub Routine to close active database connection
If Not IsNull(conn) Then
If Not IsEmpty(conn) Then
conn.Close
conn = Nothing
Set cmd = Nothing
End If
End If
End Sub