473,463 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How I know what number was generated?

Hi, I'm working with Ms Access 97 and I have two tables like these:

Table: Person Table: Phone
Field: ID (Autonumeric) (primary key) Field: Person_ID (foering key)
Filed: Name Field: Phone_number

the point is that I must insert a record into Person using
DoCmd.RunSQL("INSERT INTO...;") and also I must do the same with
Phone, but How I know the key generated for Person before?
Nov 12 '05 #1
4 1494
You won't know using that syntax. Unfortunately, Access doesn't support the
@@IDENTITY function like many db systems. Instead, you'll have to use a
recordset. Sorry, but my DAO is rusty:

Dim rst As DAO.Recordset

Set rst = Currentdb.OpenDatabase("SELECT * FROM YourTAble WHERE 1=0")

rst.AddNew
rst("Field1") = Value1
rst("Field2") = Value2

YourId = rst("YourIDField")

rst.Update

Sorry if the .AddNew and .Update are out of place ... check online help for
full syntax explanations regarding recordsets ...

"Andrés Guerrero" <ao********@hotmail.com> wrote in message
news:82*************************@posting.google.co m...
Hi, I'm working with Ms Access 97 and I have two tables like these:

Table: Person Table: Phone
Field: ID (Autonumeric) (primary key) Field: Person_ID (foering key)
Filed: Name Field: Phone_number

the point is that I must insert a record into Person using
DoCmd.RunSQL("INSERT INTO...;") and also I must do the same with
Phone, but How I know the key generated for Person before?

Nov 12 '05 #2
DFS
"Andrés Guerrero" <ao********@hotmail.com> wrote in message
news:82*************************@posting.google.co m...
Hi, I'm working with Ms Access 97 and I have two tables like these:

Table: Person Table: Phone
Field: ID (Autonumeric) (primary key) Field: Person_ID (foering key)
Filed: Name Field: Phone_number

the point is that I must insert a record into Person using
DoCmd.RunSQL("INSERT INTO...;") and also I must do the same with
Phone, but How I know the key generated for Person before?


You may want to write a routine that does three things:

Public Sub addPerson()
add record to Person table
retrieve highest ID (the one just inserted)
use the ID to add child record to Phone
End Sub
If it's a multi-user system with fairly high transactions, you may want to
use a global indicator to "lock out" the routine until it's finished so no
mixup of IDs will occur:

Public Sub addPerson()
addingPerson = True
add record to Person table
retrieve highest ID (the one just inserted)
use the ID to add child record to Phone
addingPerson = False
End sub

Call the routine, which executes only if it's not in use by someone else:
If addingPerson = False then addPerson


Nov 12 '05 #3
Andrés Guerrero wrote:
Hi, I'm working with Ms Access 97 and I have two tables like these:

Table: Person Table: Phone
Field: ID (Autonumeric) (primary key) Field: Person_ID (foering key)
Filed: Name Field: Phone_number

the point is that I must insert a record into Person using
DoCmd.RunSQL("INSERT INTO...;") and also I must do the same with
Phone, but How I know the key generated for Person before?


I might use a recordset
Dim rstPerson As Recordset
rstPerson.AddNew
rstPerson.FirstName = "Andre:
rstPerson.Update
rstPerson.BookMark = rstPerson.LastModified 'at the last record
added

DoCmd.RunSQL("INSERT INTO.PhoneTable (PersonID, PhoneNumber) Values
(" & rstPerson!ID & ", '123-456-7890'")
Nov 12 '05 #4
RE/
the point is that I must insert a record into Person using
DoCmd.RunSQL("INSERT INTO...;") and also I must do the same with
Phone, but How I know the key generated for Person before?


You don't and you never will for sure.... The Max() thing probably works most
of the time if there aren't that many users banging on it, but it's not
technically tight.

I do one of two things, depending on the situation:

1) If it's a small record, I just do a .AddNew via DAO.

2) If it's a big record I might do a .AddNew for just an empty shell of a record
from which I capture the ID. Then I follow the .Update up with running a query
to fill in the rest of the info.

3) If there's going to be a lot of this going on, I change from AutoNumber to
just a Long for the PK,and create a table: zstblRecordNumberLatest. Then I go
to that table and increment a field in it to get my next ID number before adding
a record.

Option 3 has worked pretty well for a number of years. It adds some complexity
at first, but once you get a function fully debugged, all you have to code is
something like NewID = RecordNumberNextGet("TimesheetID").
--
PeteCresswell
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joe Wong | last post by:
Hi, I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this? Thanks...
4
by: MUSTAFA IRSHAD | last post by:
DEAR SIRS, I AM DEVELOPING A SOFTWARE BY USING VB.NET AND SQLSERVER 2000 FOR A NETWORK ENVIRONMENT. I HAVE A BROBLEM THAT I HAVE A FIELD FOR PRIMERY KEY WHICH IS AUTO GENERATED. I NEED THAT WHEN...
0
by: Scott | last post by:
I need to have a field to generate the report number with the following format:- The first part consists of four digits for each year. It could be obtained from the date field entered manually....
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
9
by: Nooby | last post by:
New to Access here. I inherited a db that has the first column as an automatically generated sequential number. I want to bump it up, but for the life of me I can't figure out how to do it. Is...
2
by: anuragpj | last post by:
I want that in the number textfield a number is automatically generated, and If there is a number in the table of the database the next number is generated in the number textfield. How can I do this?
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
2
by: Irving Guy | last post by:
Hi all, I’m trying to create a form with 15 fields that generates an auto number in one of those 15 fields. The auto generated number which should display like this – “081-2008-00001”...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.