AllExperts > Using MS Access 
Search      
Using MS Access
Volunteer
Answers to thousands of questions
 Home · More Using MS Access Questions · Answer Library  · Encyclopedia ·
More Using MS Access Answers
Question Library

Ask a question about Using MS Access
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Scottgem
Expertise
I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience
I've been designing databases for over 15 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

 
   

You are here:  Experts > Computing/Technology > Business Software > Using MS Access > Running INSERT via VBA

Using MS Access - Running INSERT via VBA


Expert: Scottgem - 7/2/2009

Question
I've created a custom form that I want to use enter data into my table. My code currently looks like this:

fname = FirstNameInput.Value
lname = LastNameInput.Value
myString = "INSERT INTO Contacts ('FirstName','LastName') VALUES ('" & fname & " ','" & lname & "')"
DoCmd.RunSQL (myString)

I get the error 'The INSERT INTO statement contains the following unknown field names: "FirsName". Make sure you have typed the name correctly, and try the operation again.'

In another similar snippet of code I was getting a syntax error.

What am I missing?

Answer
First, why are you using an unbound form?

Second, drop the single quotes from the fieldnames. They are telling Access that those are text strings not fieldnames. If anything, you need to surround fieldnames with brackets [].

Third, you need to close the SQL statement with a semi colon.

myString = "INSERT INTO Contacts (FirstName,LastName) VALUES ('" & fname & " ','" & lname & "');"

But I go back to why you are using an unbound form. Unbound forms have their place, but unless you need to satisfy those reasons, you are better off using bound forms.

Hope this helps,
Scott<>
Microsoft Access MVP 2007
Author: Microsoft Office Access 2007 VBA


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.