ADO error: "closed or invalid connection object"

D

Dave

Can anyone give me a clue as to why the following code is failing at the
"cmd.execute" line with a message "closed or invalid connection object:"


Dim objConn As ADODB.Connection
Dim cmd As New ADODB.Command

Dim sql As String

Set objConn = CurrentProject.Connection
sql = "SELECT * FROM test WHERE testid=1"

MsgBox (sql)

cmd.CommandText = sql
cmd.CommandType = adCmdText
cmd.Execute

Set cmd = Nothing
Set objConn = Nothing
 
J

Juan M Afán de Ribera

Hi,

you need to connect the command object to the data. So, you better do it
this other way

....

MsgBox (sql)

cmd.ActiveConnection = objConn
cmd.CommandText = sql
cmd.CommandType = adCmdText
cmd.Execute

....

HTH
 
D

Dave

Ah, yes. Very good.

Thank you Juan.


Juan M Afán de Ribera said:
Hi,

you need to connect the command object to the data. So, you better do it
this other way

...

MsgBox (sql)

cmd.ActiveConnection = objConn
cmd.CommandText = sql
cmd.CommandType = adCmdText
cmd.Execute

...

HTH

--
Saludos :)
Juan M Afan de Ribera [MVP Access]
http://www.mvp-access.com/juanmafan

Dave said:
Can anyone give me a clue as to why the following code is failing at the
"cmd.execute" line with a message "closed or invalid connection object:"


Dim objConn As ADODB.Connection
Dim cmd As New ADODB.Command

Dim sql As String

Set objConn = CurrentProject.Connection
sql = "SELECT * FROM test WHERE testid=1"

MsgBox (sql)

cmd.CommandText = sql
cmd.CommandType = adCmdText
cmd.Execute

Set cmd = Nothing
Set objConn = Nothing
 

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