ADP equivalent for CurrentDB

P

Peter Wone

I realise that CurrentDb returns Nothing because there is no current Jet
database in an ADP. So how does one get a reference to the default database
connection? In bound forms I've been using Me.Recordset.ActiveConnection but
in an unbound form this doesn't work for fairly obvious reasons.
 
S

Steve Jorgensen

I realise that CurrentDb returns Nothing because there is no current Jet
database in an ADP. So how does one get a reference to the default database
connection? In bound forms I've been using Me.Recordset.ActiveConnection but
in an unbound form this doesn't work for fairly obvious reasons.

As I recall, it's CurrentProject.Connection
 
S

steel

I use this template for recordsets, works well;

Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strMsg As String

'SQL_Connection
On Error GoTo <function_name>_Error

conn.Open CurrentProject.Connection
Set cmd.ActiveConnection = conn

strSQL = <insert SQL TExt>

cmd.CommandText = strSQL
cmd.CommandType = adCmdText
Set rs = cmd.Execute

If rs.State = adStateClosed Then
'no connection made
Goto <function_name>_error
else
<Connection OK - carry out function>
End if

<function_name>_Exit:
Set rs = Nothing
Set cmd = Nothing
conn.Close
Set conn = Nothing
Exit Sub

<function_name>_Error:
strMsg = "ERROR: " & Err.Description
If MsgBox(strMsg, vbCritical, "Error:") = vbOK Then
End If
Resume <function_name>_Exit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top