W
Wullie
I have 3 forms in a database which allow users to edit information contained
in different tables - 1 form for each table.
The following code works perfectly well, displays the current list, moves
the selection from the list to the edit box and updates the table with the
new value
---------------------
Dim nameToUpdate As Integer
Dim SQL As String
nameToUpdate = Me.Edit_Current_List.Value
SQL = "UPDATE Staff SET StaffName = '" & Replace(Me.NewItem, "'", "''") & "'
WHERE ID = " & nameToUpdate
CurrentDb.Execute SQL, dbFailOnError
Me.NewItem = ""
Me.Refresh
--------------------
The other 2 have exactly the same code (except different table names/field
names) but I get an error stating too few parameters. When it breaks and I
hover over the SQL statement (or pull up an immediate window), I get the SQL
statement I would expect.
I have searched and I would hazard a guess that the correct way to do it
would be through setting parameters, but I didn't fully understand the
parameters (I tried it, but it skipped the loop, which gives the impression
that maybe my QueryDef is wrong - before any points it out, I know the below
is commented)
---------------------------
'Dim db As DAO.Database
'Dim qdf As DAO.QueryDef
'Dim prm As DAO.Parameter
'Dim rst1 As Recordset
'Dim nameToUpdate As Integer
'Set db = CurrentDb
'Set qdf = db.QueryDefs("AreaQuery")
'nameToUpdate = Me.Edit_Current_List.Value
'For Each prm In qdf.Parameters
'prm.Value = Eval(prm.Name)
'Next prm
'Set rst1 = qdf.OpenRecordset
'With rst1
'[AreaName] = nameToUpdate
'End With
'rst1.Close
'Set rst1 = Nothing
'Set qdf = Nothing
'Set db = Nothing
in different tables - 1 form for each table.
The following code works perfectly well, displays the current list, moves
the selection from the list to the edit box and updates the table with the
new value
---------------------
Dim nameToUpdate As Integer
Dim SQL As String
nameToUpdate = Me.Edit_Current_List.Value
SQL = "UPDATE Staff SET StaffName = '" & Replace(Me.NewItem, "'", "''") & "'
WHERE ID = " & nameToUpdate
CurrentDb.Execute SQL, dbFailOnError
Me.NewItem = ""
Me.Refresh
--------------------
The other 2 have exactly the same code (except different table names/field
names) but I get an error stating too few parameters. When it breaks and I
hover over the SQL statement (or pull up an immediate window), I get the SQL
statement I would expect.
I have searched and I would hazard a guess that the correct way to do it
would be through setting parameters, but I didn't fully understand the
parameters (I tried it, but it skipped the loop, which gives the impression
that maybe my QueryDef is wrong - before any points it out, I know the below
is commented)
---------------------------
'Dim db As DAO.Database
'Dim qdf As DAO.QueryDef
'Dim prm As DAO.Parameter
'Dim rst1 As Recordset
'Dim nameToUpdate As Integer
'Set db = CurrentDb
'Set qdf = db.QueryDefs("AreaQuery")
'nameToUpdate = Me.Edit_Current_List.Value
'For Each prm In qdf.Parameters
'prm.Value = Eval(prm.Name)
'Next prm
'Set rst1 = qdf.OpenRecordset
'With rst1
'[AreaName] = nameToUpdate
'End With
'rst1.Close
'Set rst1 = Nothing
'Set qdf = Nothing
'Set db = Nothing