Update and insert with value in variables 
Author Message
 Update and insert with value in variables

Hi,
   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));"

Thank for the help!
Guillaume



Tue, 15 Nov 2005 23:18:07 GMT  
 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



Fri, 18 Nov 2005 21:42:27 GMT  
 Update and insert with value in variables

Quote:

>     CurrentDb.Execute "INSERT INTO livrer VALUES (cham,code,num,prin);"

SQL doesn't know what cham, code etc are.

....VALUES ('" & cham & "', '" & code & "', " & num & ", " & prin & ");"

Regards

Peter Russell



Mon, 21 Nov 2005 17:37:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to use variable to Insert value using a query

2. ADO update not inserting identity value

3. Variable and field value changes after .Update in VBA

4. Updating value of private variable in a form

5. oledb: foxpro index file not updating after inserts and updates

6. The value of a variable (String type) is the name of a variable

7. Variable Name to Variable value.

8. Create a Variable from the value of another variable

9. Getting data values to CR from variable arrays and stand alone variables in VB

10. Variable values as variable names

11. Assigning the value to the value of a variable

12. using variables in Insert Into statement

 

 
Powered by phpBB® Forum Software