
Update and insert with value in variables
Quote:
> I want to insert a new record in a table with the
>value, i have stored in variables but when i execute the
>instruction Access say "too few parameter expecting 4"
>There is my code
>Dim num As Integer
> Dim code As String
> Dim cham As String
> Dim Prin As Boolean
> num = NumRef.Value
> code = CodeProducteur.Value
> cham = Modifiable71.Value
> Prin = Cocher73.Value
> CurrentDb.Execute "INSERT INTO livrer VALUES
>(cham,code,num,prin);"
>I also want to do the same thing with Update
>CurrentDb.Execute "UPDATE livrer SET ChampPrinc = '" &
>Prin & "' WHERE ((numRef = num) AND (CodeProducteur =
>code) AND (NumChamp = cham));"
Guillaume,
when using SQL statements in code, you must concatenate the _contents_
of your variables, not their names. SQL doesn't know anything about
variables in code, for ex in the INSERT statement it thinks, cham,
code, etc are some table fields.
Try this code:
Dim num As Integer
Dim code As String
Dim cham As String
Dim Prin As Boolean
Dim strSQL As String
num = NumRef.Value
code = CodeProducteur.Value
cham = Modifiable71.Value
Prin = Cocher73.Value
'Just in case you should always specify
'the field names too; please substitute the
'real names from your table
'Also, note that strings have to be enclosed
'in quotes; I use here single quotes for cham
'and code
strSQL= " INSERT INTO livrer" _
& " (fldCham, fldCode, fldNum, fldPrin) VALUES (" _
& "'" & cham & "', '" & code & "', " & num & ", " & Prin _
& ")"
CurrentDb.Execute strSQL
The code for the UPDATE statement would be:
'You don't need quotes for a boolean,
'but note the single quotes for code and cham!
strSQL= UPDATE livrer SET ChampPrinc = " & Prin _
& " WHERE numRef =" & num _
& " AND CodeProducteur ='" & code & "' AND NumChamp ='" _
& cham & "'"
CurrentDb.Execute strSQL
Using a variable for the statement has the advantage that you can set
a break point (or MsgBox) and take a look to the SQL statement before
executing it.
HTH
Best regards
Emilia
Emilia Maxim
PC-SoftwareService, Stuttgart
http://www.maxim-software-service.de