I’m a bit stuck with parameters and transactions in ADO, in VBScript and Access. Basically, I’m working through a massive loop and writing the results to a database, so I need to wrap it in a transaction otherwise it takes ages.
I’ve written the below script which works for a single parameter, (although this seems a bit of a long way of doing it, so if anyone knows a shorter way, please shout). However I can’t work out how to expand this to two parameters:
objConn.BeginTrans
set oParm = CreateObject("ADODB.Parameter")
oParm.Value = ""
oParm.Type = 200
oParm.Direction = 1
oParm.Size = 100
Set oCmd = CreateObject("ADODB.Command")
oCmd.ActiveConnection = objConn
oCmd.commandText = "INSERT INTO table (field) VALUES (?)"
oCmd.commandType = 1
oCmd.Parameters.Append oParm
'Big loop here that goes through lots of lines.
oCmd.Execute ,"Field",1
'Loop
objConn.CommitTrans
For example, if I wanted to expand this to:
oCmd.commandText = "INSERT INTO table (field1, field2) VALUES (?,?)"
I can’t figure out what I do with my parameters. I’m sure I’m just being stupid here and not quite following how these work.
I’ve never tried passing parameter values through the
Executemethod, so I can’t quite say what’s wrong. I will say that the documentation states that the second argument should be an array of values, so maybe if you triedArray("Field1Val", "Field2Val"), that would work.What I usually do is give each parameter a name, then you can reference it within your loop to change its value. You can use any name you like, as long each parameter has a unique name. As an example:
As far as shortening the code, the only suggestion I can make is using the
CreateParametermethod to, well, create the parameter. That will allow you to set all the relevant properties on one line.