Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What type of connection...

Status
Not open for further replies.

skinrock

Programmer
Nov 20, 2004
23
US
Would I need to use SQL queries against a MS Access database in VB.NET. Right now I'm using
Code:
Driver={Microsoft Access Driver (*.mdb)};DBQ=..\cfss.mdb

Which connects fine, and also I was able to successfully run a SELECT query on it, however I need to run INSERT statements, and mine just isn't working. The code is
Code:
dbCommand.CommandText = "INSERT INTO personalinfo VALUES('" & fname & "','" & lname & "','" & street & "','" & city & "','" & state & "','" & zip & "','" & phone & "','" & email & "')"
Just so you know, the first column in the table is an autonumber column, and you can see in the statement that I left it out. All of these fields are text fields, as well. Every time I execute it with
Code:
dbCommand.ExecuteNonQuery()
it just gives me a general error on the main form, not describing anything. Any ideas?
 
BTW, fname, lname, street, etc. are all string variables.
 
I'm not that familiar with Access, but I believe you need to specify the columns you are inserting, as you would in SQL Server, since you are not inserting values into all columns.
Code:
dbCommand.CommandText = "INSERT INTO personalinfo (fname, lname, street, city, state, zip, phone, email) VALUES('" & fname & "','" & lname & "','" & street & "','" & city & "','" & state & "','" & zip & "','" & phone & "','" & email & "')"
This assumes that your columns are named the same as your variables. If not, you will need to change the column names in the insert statement.
 
Thank you so much, that was it. It's weird, because I have some PHP code that runs some SQL against a mySQL database, and I didn't need to specify the columns even though I was using an autonumber column and didn't account for it in the INSERT statement. Anyways, it works, thanks a bunch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top