insert into in the code

hair

Registered User.
Local time
Today, 12:21
Joined
Mar 27, 2003
Messages
125
Hi

I have a form called x
On this form a text field named Text1

I want to pressa button so that what was written in the Text1 field to go in a table
I did something like this:

Private Sub Commande72_Click()

Dim db As DAO.Database
Dim rc As DAO.Recordset
Dim q As QueryDef
Dim a As String
Set db = CurrentDb
a = Text1

Set q = db.CreateQueryDef("")

q.SQL = "insert into x(descriptionprb) values(here I don't know what to do to pass the a value);"
q.Execute



Thanks for helping
 
That looks really heavy :D

Try this:
Code:
Private Sub Commande72_Click()
  Dim strSQL As String

  strSQL = "INSERT INTO x ( descriptionprb ) SELECT '" & _
                  Me!Text1 & " AS descriptionprb"
  DoCmd.RunSQL strSQL
Exit Sub
 
hi Bert
are u sure that the quotas are good in u're code?
I get a msgbox as Me!Text1 was not functioning
 
You're right :(
It has to be this: (single quote after the double quote after Me!Text1)
Code:
Private Sub Commande72_Click()
  Dim strSQL As String

  strSQL = "INSERT INTO x ( descriptionprb ) SELECT '" & _
                  Me!Text1 & "' AS descriptionprb"
  DoCmd.RunSQL strSQL
Exit Sub

greets
 
just like 10 seconds before u're reply, I did this:

Private Sub Commande72_Click()

Dim strSQL As String
Dim a As String
a = Me!Texte45
strSQL = "INSERT INTO probleme(descriptionprb) SELECT Forms!ffiche.Texte45;"
DoCmd.RunSQL strSQL

Exit Sub


...and it worked just fine.
Tell me, is it stupid to declare a variable a for this or I could have avoid it?
Thanks a lot sir
 
Dim a As String
a = Me!Texte45

Are you using the a further down ? or somewhere else? In this bit of coding, yes its stupid (your words). I would say redundant...

Futhermore i would advise you to NOT use nice names like Texte45 and Commande72. These dont say anything, use more comprehencive names like cmdExecuteQuery (for the button) and txtDescription for the textbox.

Regards
 
Actualy I wanted to use it, after I've changed my mind, and after forgot to erase it...Mea culpa..

Thanks for help and yes, I will name the buttons after their function.
 

Users who are viewing this thread

Back
Top Bottom