Click to See Complete Forum and Search --> : how do i insert a line into databse


pelegk1
09-18-2003, 06:12 AM
where the first col is AUTONUMBER?
i want to know whats the given value

how do i do that?
thnaks inadvance
peleg

TBor
09-18-2003, 09:01 AM
Assuming you're using Access for your DB, you can send an INSERT command to the database, but don't specify any value for the autonumber field. The DB should insert that automatically for you. Then, you could run a SELECT query to grab the autonumber field value.

For example, if you have a table with fields A,B,C, and D, with D being the autonumber field:

INSERT INTO table (A,B,C) VALUES ('1','2','3')
SELECT D FROM table WHERE A='1' AND B='2' AND C='3' ORDER BY D DESC

The "order by" is in there so you can grab the first value in the returned recordset, just in case there are other records in the DB that will have the same values for A, B, and C.

Obviously, this is not ideal, because there is still the chance that a record could be added with the same A,B,and C values after your INSERT but before your SELECT is executed. However, if you don't have a lot of changes happening in the DB at around the same time you might not have to worry about it. Judge that for yourself, I guess.

***Ideally, if you were using SQL Server or something like that, I would recommend doing everything in a stored procedure and returning the assigned value for the autonumber field, but I'm not sure if you can do that in Access or not. If you can, or if you're not using Access, let me know and I can give you a more elegant solution.

Hope this helps,
TBor