Re: Parameters in INSERT query problem
From: Daniel Crichton (msnews_at_worldofspack.co.uk)
Date: 03/22/05
- Previous message: mona: "Re: Runtime error 3707"
- In reply to: MJS: "Parameters in INSERT query problem"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 22 Mar 2005 11:56:38 -0000
MJS wrote on Mon, 21 Mar 2005 15:27:22 -0600:
> I have a query that works.... but I want to clean it up, using ?'s for the
> parameters in the query... but I am having a problem, I keep getting an error:
> "Run-time error '3001': Arguments are of the wrong type, are out of
> acceptable range, or are in conflict with one another.".
>
> Like I stated... the query in this format works:
>
> cmd.CommandText = "INSERT INTO MyTable(SomeString1,
> SomeInt, SomeString2) VALUES('" & _
> str1 & "'," & _
> int1 & ",'" & _
> str2 & "')"
>
> But I want to simplify the CommandText string... and use queries like the
> following... some of my queries will have many params.
> cmd.CommandText = "INSERT INTO MyTable(SomeString1, SomeInt,
> SomeString2) VALUES(?,?,?)"
> Set rst = cmd.Execute(Parameters:=Array(str1, int1, str2))
>
> I am able to do this successfully if I have only one ?, but when I have
> multiple, it doesn't want to work.
>
> Any Ideas?
The Array function always returns an array of elements that have the same
intrinsic data type. Therefore, an implicit conversion is going on to make
all of the elements the same type.
How about creating the parameters explicitly using the CreateParameter
method so you can define the type and it's value knowing that it won't get
converted? I'm not sure if this will work as I haven't tested it, and you
might need to change the string types and size to match your field
definitions.
cmd.CommandText = "INSERT INTO MyTable(SomeString1, SomeInt, SomeString2)
VALUES(?,?,?)"
cmd.Parameters.Append cmd.CreateParameter(,adVarChar,,255,str1)
cmd.Parameters.Append cmd.CreateParameter(,adInteger,,,int1)
cmd.Parameters.Append cmd.CreateParameter(,adVarChar,,255,str2)
cmd.Execute
Btw, why are you creating a recordset from the Execute method of the Command
object? You'll only get an empty recordset, right?
Dan
- Previous message: mona: "Re: Runtime error 3707"
- In reply to: MJS: "Parameters in INSERT query problem"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|