Jonhalo
Jonhalo

Reputation: 13

Inserting values into a Microsoft Access database using a button on a form

Hi I am currently trying to added a few values inputted in form into my database using the provided line.

Private Sub btnInsert_Click()
    DoCmd.RunSQL "INSERT INTO Band (BandID, BandName, BandAddress, ContactName, PhoneNumber)"
End Sub

I am getting a 3431 runtime error. Where I am messing up here?

Upvotes: 1

Views: 3617

Answers (3)

Cahaba Data
Cahaba Data

Reputation: 622

In Access, a form is best bound to the table - and then there is no append query needed. When you input it into the form it is going into the table.

If there is a real compelling reason to have an unbound form - then things are more complicated and you must first establish these values in a Select Query so they exist as a record set; then use that as the starting point for your Append query....but like I said - this generally is not the way to go.....

Upvotes: 0

Harshit kyal
Harshit kyal

Reputation: 350

You don't need to include auto increment fields. But you to specify fields if you are not inserting every field value.

INSERT INTO Band (fieldName1, fieldName2, fieldName3)
VALUES (1, 'DEF', 'ABC')

Alternative:

sql ="INSERT INTO Band (fieldName1, fieldName2, fieldName3) " & _
"VALUES (1, 'DEF', 'ABC') " & _
"
currentDb.execute sql 

Upvotes: 0

wazz
wazz

Reputation: 5068

You need to use the term 'Values'.

If you include all fields:

INSERT INTO Band Values BandID, BandName, BandAddress, ContactName, PhoneNumber

If you're not adding all fields, specify the fields first

INSERT INTO Band (BandID, BandName, BandAddress)
VALUES (1, 'blah', 'blahblah')

I don't think you have to add the ID field if it's an auto-increment id.

Upvotes: 1

Related Questions