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!

Writing to a table

Status
Not open for further replies.

MCSGraham

Programmer
May 28, 2002
52
US
I get an error when I try to put a "1" in a field inside a table...this is the code I'm using:

Dim db As Database
Dim rst1 As Recordset
Set db = CurrentDb
Set rst1 = db.OpenRecordset("NewDB")

rst1.AddNew
rst1![Units] = "1"
rst1.Update

(It gives an error of "Index or Primary Key cannot contain a Null value")

NewDB is my table and Units is the field I want to write a "1" into.

Is there a better way to write info into a table's field?

thank you!
 
What is the primary key for that table??? The error is stemming from the fact that you are creating a new record, setting the Units field to 1, but you aren't setting a primary key......You code should be something like...

Dim db As Database
Dim rst1 As Recordset
Set db = CurrentDb
Set rst1 = db.OpenRecordset("NewDB")

rst1.AddNew
rst1![nameofprimarykey] = "somevalueforprimarykey"
rst1![Units] = "1"
rst1.Update

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein [spin]

Robert L. Johnson III, A+, Network+, MCP
Access Developer/Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top