param query in VBA against MySQL (1 Viewer)

amc

Registered User.
Local time
Yesterday, 17:34
Joined
Dec 12, 2015
Messages
14
I am failing to find the right way to execute a param query in MySQL

I am using the following code, where cmd param and cn are ADODB objects (Command, Parameter and Connection)


Dim pkc As String

sql = "SELECT * FROM Accions WHERE keyclient = @kc"
cmd.ActiveConnection = cn
cmd.CommandText = sql

pkc = "AMC"
Set param = cmd.CreateParameter("@kc", adVarChar, adParamInput, 12, pkc)
cmd.Parameters.Append param
cmd.CommandType = adCmdText

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


the snippet executes, but the rs recordset is empty, which is wrong.

Any hints on this please? I have seen posts around telling to use '?' instead of '@', but I am failing. Maybe is not a good idea using Mysql param queries from VBA/ADODB?

thanks
 

ByteMyzer

AWF VIP
Local time
Yesterday, 17:34
Joined
May 3, 2004
Messages
1,409
Try setting the parameter argument as ? in your SQL statement, but leaving the parameter name as @kc in your CreateParameter statement, like the following:
Code:
[COLOR="Navy"]Dim[/COLOR] pkc [COLOR="Navy"]As String[/COLOR]

Sql = "SELECT * FROM Accions WHERE keyclient = ?"
cmd.ActiveConnection = cn
cmd.CommandText = Sql

pkc = "AMC"
[COLOR="Navy"]Set[/COLOR] param = cmd.CreateParameter("@kc", adVarChar, adParamInput, 12, pkc)
cmd.Parameters.Append param
cmd.CommandType = adCmdText

[COLOR="Navy"]Set[/COLOR] rs = cmd.Execute
cmd.CommandType = adCmdText
[COLOR="Navy"]Set[/COLOR] rs = cmd.Execute
 

amc

Registered User.
Local time
Yesterday, 17:34
Joined
Dec 12, 2015
Messages
14
It works, thanks!
 

Users who are viewing this thread

Top Bottom