Form Code won't run properly

  • Thread starter Thread starter JP
  • Start date Start date
J

JP

I have a form with one field (Text1 - Unbound) and a table
with 2 fields (Test1 - Autonumber, Test2 - Text)

The form has a button which runs this code:

Dim SQL as String
SQL = "Insert Into Table1 (Test2) Values (" & Me.Text1
& ");"

DoCmd.RunSQL SQL

I have feeling nothing is wrong with this code yet what
happens is that Access pops up a msgbox with whatever I
typed into the unbound field and asks for a parameter.

When I put in what I had in the unbound text box into the
msgbox/parameter request it inserts it into the table.
When I leave the parameter request blank it puts in a
blank entry.

What am I doing wrong?
 
If your field, Test2, is a text field, I think you will need to get quotes -
Chr(34) - around the value you want inserted into the table. Try this...

SQL = "Insert Into Table1 (Test2) Values (" & chr(34) & Me.Text1 & chr(34)
& ");"

Also, while I don't believe that this is causing the problem, I recommend
that you dim your variable, SQL, as strSQL. "SQL" is a reserved word in
Access and using it as a object, variable or field name can cause unexpected
behavior. There is more info about Reserved Words here:

http://support.microsoft.com/default.aspx?scid=kb;en-us;209187
 
Back
Top