Full Version: Appending Record - Insert Into
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
RCJT
I've got two forms. Form1 is filled out. Form2 shares four common fields with form1. I'm having trouble with the INSERT INTO:
CODE
SQLString = "INSERT INTO tblNotes (PNID, PNDate, PNLength, PNNote, PNCG1 ) " & _
"VALUES (50000, #02/12/2012# , 15 , 'Testing' , 70000);"
DoCmd.RunSQL (SQLString)


The table I'm inserting into has an AutoID for the first column (which I do nothing with), then my four fields should be appended to the table. The PNID is a required field in the table along with the autonumber. All other fields are optional. When I go to insert the record, it complains that I have a NULL value somewhere and prevents the insert.

Edit2: More details.

Edit: Access 2010 (NOT web) 32bit
theDBguy
Hi JT,

Are there any other "required" fields in your table?
Alan_G
Hi

You say "then my four fields should be appended to the table", but you have 5 fields along with 5 values listed in your SQL ?

Is the PNID field the autonumber PK field in the tblNotes table ? If so, you're trying to append a value of 50000 to it which you wont be able to do as Access will assign the autonumber value. Maybe this

CODE
SQLString = "INSERT INTO tblNotes (PNDate, PNLength, PNNote, PNCG1) " & _
"VALUES (#02/12/2012#, 15, 'Testing', 70000);"
DoCmd.RunSQL (SQLString)
RCJT
I was just looking at that dbGuy. The first field is the primary key (not inserting anything here). The fields PNDate and PNCG1 are indexed and duplicates are allowed. PNID is required. I am inserting values into each of those.

JT

Below is the basic structure of the table:

tblNotes.
NotesID Auto Primary Key
RID Long Required
PNDate Date Indexed, Duplicates OK
PNLength Long
PNNote Memo
PNCG1 Long Indexed, Duplicates OK
PNFirst String
PNLast String
RCJT
QUOTE (Alan_G @ May 25 2012, 03:30 PM) *
Hi

You say "then my four fields should be appended to the table", but you have 5 fields along with 5 values listed in your SQL ?

Is the PNID field the autonumber PK field in the tblNotes table ? If so, you're trying to append a value of 50000 to it which you wont be able to do as Access will assign the autonumber value. Maybe this

CODE
SQLString = "INSERT INTO tblNotes (PNDate, PNLength, PNNote, PNCG1) " & _
"VALUES (#02/12/2012#, 15, 'Testing', 70000);"
DoCmd.RunSQL (SQLString)


You are right, I insert four fields. I was thinking five because I expected the autonumber to fill itself in. (Edit: the autonumber is column1. The insert deals with other fields and not the autonumber. I'm assuming the autonumber will appear correctly when I do the append)

JT
Alan_G
Hi

So, just to confirm. PNID isn't the autonumber primary key, but it is a numerical field in the table to which you're wanting to insert a value into and there really are 5 values to append ?

I got confused with the "then my four fields should be appended to the table" part of your post

Is this table related as a child table to another table, and if it is has the parent record been created before you try and add the child record ?
RCJT
QUOTE (Alan_G @ May 25 2012, 03:40 PM) *
Hi

So, just to confirm. PNID isn't the autonumber primary key, but it is a numerical field in the table to which you're wanting to insert a value into and there really are 5 values to append ?

I got confused with the "then my four fields should be appended to the table" part of your post

Is this table related as a child table to another table, and if it is has the parent record been created before you try and add the child record ?


Yea the PNID is not the PK. See above for a better description of the table. I don't believe it has any required parent or child records, but not exactly sure on that point.

JT
Alan_G
Hi

QUOTE
but not exactly sure on that point.


If it were a child table and you had referential integrity enforced then it would need a parent record before you could add the child record which is what I was thinking

There doesn't look to be anything wrong with the SQL, so all things being equal I'd say check the table to make sure everything is as it should be in the field properties
RCJT
Sorry to vanish from the conversation on Friday. I had a three day weekend that I took seriously smile.gif. I'm going to re-read the thread and move forward. JT
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.