C
Chris Burnette
I am trying to run a couple of SQL queries based on input from a form. I
know it's possible to use a parameter query to do this, but it doesn't seem
to work for certain parameters, notably the field that you want to update.
So, I created a form with a combo box and a couple of command buttons to
allow me to choose which field I want to update.
My code is below:
Private Sub btnChange_Click()
Dim myField As String
Dim sSQL As String
Dim numCount As Integer
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Set db = CurrentDb()
myField = cboMyField.Value
Set rst = db.OpenRecordset("EntireSpreadsheet_local")
Set fld = rst.Fields(myField)
sSQL = "UPDATE EntireSpreadsheet_local SET fld = CVDate(fld.Value) WHERE
IsDate(fld) = True"
db.Execute sSQL, dbFailOnError
numCount = db.RecordsAffected
MsgBox numCount & " records were updated by this query"
End Sub
The second Sub procedure is the same as the first, except that the SQL
statement reads like this:
sSQL = "UPDATE EntireSpreadsheet_local SET fld = '' WHERE fld='0'"
When I try to run the first Sub procedure, I get an error message saying
"Too few parameters: expected 2."
When I try to run the second, I get the message: "Too few parameters:
expected 1"
In both cases, the debugger points me to this line:
db.Execute sSQL, dbFailOnError
I am not quite sure why I keep getting this error, as I clearly have the
parameters defined. My SQL statements don't seem to be generating any errors
in the debug window, and I use both parameters in the Execute method in both
Sub procedures, yet one tells me 'Expected 2' and the other says 'Expected 1'.
Does anyone have any idea why I might be getting this error? Any help would
be appreciated.
Thanks,
Chris
know it's possible to use a parameter query to do this, but it doesn't seem
to work for certain parameters, notably the field that you want to update.
So, I created a form with a combo box and a couple of command buttons to
allow me to choose which field I want to update.
My code is below:
Private Sub btnChange_Click()
Dim myField As String
Dim sSQL As String
Dim numCount As Integer
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Set db = CurrentDb()
myField = cboMyField.Value
Set rst = db.OpenRecordset("EntireSpreadsheet_local")
Set fld = rst.Fields(myField)
sSQL = "UPDATE EntireSpreadsheet_local SET fld = CVDate(fld.Value) WHERE
IsDate(fld) = True"
db.Execute sSQL, dbFailOnError
numCount = db.RecordsAffected
MsgBox numCount & " records were updated by this query"
End Sub
The second Sub procedure is the same as the first, except that the SQL
statement reads like this:
sSQL = "UPDATE EntireSpreadsheet_local SET fld = '' WHERE fld='0'"
When I try to run the first Sub procedure, I get an error message saying
"Too few parameters: expected 2."
When I try to run the second, I get the message: "Too few parameters:
expected 1"
In both cases, the debugger points me to this line:
db.Execute sSQL, dbFailOnError
I am not quite sure why I keep getting this error, as I clearly have the
parameters defined. My SQL statements don't seem to be generating any errors
in the debug window, and I use both parameters in the Execute method in both
Sub procedures, yet one tells me 'Expected 2' and the other says 'Expected 1'.
Does anyone have any idea why I might be getting this error? Any help would
be appreciated.
Thanks,
Chris