James Dawson
James Dawson

Reputation: 5409

How can I use values from a form's input fields in an INSERT query?

I have a form for adding a customer into a table, and I'm trying to write the query for it. I'm having trouble getting the values from the form's input fields (name, address etc.) and putting it into the query.

How can I do this?

Upvotes: 0

Views: 1004

Answers (1)

Fionnuala
Fionnuala

Reputation: 91376

You can refer to the controls on the form or the fields in the recordset. They may or may not have the same values depending on whether the form is dirty or not.

A simple query might be 9query design window)

UPDATE ATable SET AName=Forms!Form1!txtName

INSERT INTO ATable ( AName ) Values ( Forms!Form1!txtName )

In the code belonging to the form you could say

Dim db As Database
Set db = CurrentDB

sSQL = "UPDATE ATable SET AName='" & Replace(Forms!Form1!txtName,"'","''") & "'"
db.Execute sSQL, dbFailOnError

sSQL = "INSERT INTO ATable ( AName ) Values ('" _
     & Replace(Forms!Form1!txtName,"'","''") & "')"
db.Execute sSQL, dbFailOnError

Upvotes: 1

Related Questions