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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Primary key?

Status
Not open for further replies.

derketa

Programmer
May 13, 2002
43
0
0
TR
I will ask a very simple question.I created a database with access and defined some fields and also an identity field.I took this file with ADODB connection and i want to write a code to add new records to this database file. I looked at some books and also MSDN for help but i could not understand how to connect the database to my program with this primary key(identity key)?Also it is defined as an automatic number but should i increase it manually while adding new records?
 
Dear derketa

Ypu donot need to refrence the identity column/field in your SQL statement, and on adding a new record the identity column will automatically get inserted with the value of max (identity value before the insert) + 1.

Bye
mshetti@yahoo.com
 
Hi,

You cannot (as far as I know) force values into autonumber fields (corresponing to the JET datatype COUNTER) in access (however it is possible in SQL server).
When you add new records (by using the .addnew method of and ADO recordset or evaluating an INSERT statement) your autonumber field will automatically be increased.

Here is a snipplet that shows how to open a database and add a record.

-----------------------------------------------------------
'Set a reference Microsoft ActiveX Objects 2.5 Library (MSADO25.tlb).
Dim con As ADODB.Connection
Dim StrCon As String
Const dbname = "c:\test.mdb"
StrCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & dbname
Set con = new adodb.connection
con.open StrCon

con.Execute ("INSERT INTO TblPerson (projectID,surname) VALUES (1,'testname')")
'Tblperson contains a autonumber column tblprojectID, which is automatically increased
con.Close
Set con = Nothing
-----------------------------------------------------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Yes in the examples i looked it seems like i dont need to reference the identity key but when it's running gives


Run-time error 3265, means "ADO could not find the object in the collection corresponding to the name or ordinal reference requested by the application."

I can not find what is different in my code(It is about 15 lines:)) to give this error.
 
post your code... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Chances are one of the field names is spelled wrong in your select, update or insert statement. You don't include the Identity Column on an insert and the only time you usually include one on an update is in the where clause. Check the spelling of your other fields first. Snaggs
tribesaddict@swbell.net
Life can only be understood backwards; but it must be lived forwards.
 
Definitely the problem is wrong spelling:))I just can not see it.I think i can not be a good programmer unless i find a method avoids me doing these simple mistakes.

I will try not to ask every difficulty i face with:)
Thanks a lot
 
It's not weather you spell it right or wrong, it's how consistent you are in your spelling. Snaggs
tribesaddict@swbell.net
Life can only be understood backwards; but it must be lived forwards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top