简体   繁体   中英

Ms Access, parameterized queries, What is wrong with this code?

I like to use a parameterized query to access a PostreSQL Database with ADO/Ms Access.

'sql-statement: SELECT id, str FROM tbl WHERE str LIKE '<userstring>';
'str is character varying in the DB

Dim cmd As ADODB.Command
Dim param1 As ADODB.Parameter

Set cmd = New ADODB.Command
Set cmd.ActiveConnection = con 'connection is open
cmd.CommandText = "SELECT id, str FROM tbl WHERE str LIKE @FirstParameter"
Set param1 = cmd.CreateParameter("@FirstParameter", adVarWChar, adParamInput)
param1.Value = userstring
cmd.Parameters.Append param1 ' ERROR

I have to translate the error: "The parameter-object is not set properly. Inconsitent or invalid."

ADO uses question marks to indicate parameters. Likely, using your combination of provider and parameter indicator, the parameter is not recognized.

Postgresql uses dollarsigns to indicate parameters using the PREPARE ... EXECUTE syntax, but that requires you to pass the parameters again (with questionmarks) in the EXECUTE clause, so I don't recommend that

Unfortunately I don't have a postgresql setup handy, but try the following:

Set cmd = New ADODB.Command
Set cmd.ActiveConnection = con 'connection is open
cmd.CommandText = "SELECT id, str FROM tbl WHERE str LIKE ?"
Set param1 = cmd.CreateParameter(, adVarWChar, adParamInput, Len(userstring), userstring) 'Unnamed parameter
cmd.Parameters.Append param1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM