I have 2 tables, CONTACT and ADDRESS. The primary key in the ADDRESS table is a foreign key in the CONTACT table.
I want to write some VB to add a new Contact. This means first adding an entry in the ADDRESS table (to determine the auto-numbered primary key) and then adding an entry in the CONTACT table, including the newly assigned address PK.
Looking at previous posts, it looks like there isn't a neat way of automatically determining the (newly assigned) ADDRESS primarykey and using this in the CONTACT insert statement. Rather, a select statement is needed to obtain this primarykey, followed by another insert statement into the CONTACT table. Please can someone confirm that this is indeed the case?
If this is the case, I have a problem in that I can't get the (newly assigned) ADDRESS primarykey... it looks like the insert statement hasn't been committed yet, and so the PK hasn't been created yet. (I get a Run-time error '3021' - No Current Record on the rst.MoveLast line - see below)
I don't really want to force a commit as I may need to roll back. I guess I'm looking for something similar to the @@IDENTITY command in SQL Server...
Please can someone advise me on the best way to approach this and apologies in advance if this has been answered many times before... it must be a common question, but I can't find a suitable answer.
Thanks in advance.
My existing code is shown below:
<code>
Dim AddressSQL As String
AddressSQL = "INSERT INTO ADDRESS (AddressLine1, AddressLine2, City, County, Country, Postcode) values (AddressLine1, AddressLine2, City, County, Country, Postcode)"
DoCmd.RunSQL AddressSQL
Dim dbs As Database, rst As Recordset, strSQL As String
Dim fld As Field
Set dbs = CurrentDb
strSQL = "SELECT DISTINCTROW [AddressPK]" _
& "FROM ADDRESS WHERE [AddressLine1] = ' & AddressLine1 & '"
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
rst.MoveLast
<code>
I want to write some VB to add a new Contact. This means first adding an entry in the ADDRESS table (to determine the auto-numbered primary key) and then adding an entry in the CONTACT table, including the newly assigned address PK.
Looking at previous posts, it looks like there isn't a neat way of automatically determining the (newly assigned) ADDRESS primarykey and using this in the CONTACT insert statement. Rather, a select statement is needed to obtain this primarykey, followed by another insert statement into the CONTACT table. Please can someone confirm that this is indeed the case?
If this is the case, I have a problem in that I can't get the (newly assigned) ADDRESS primarykey... it looks like the insert statement hasn't been committed yet, and so the PK hasn't been created yet. (I get a Run-time error '3021' - No Current Record on the rst.MoveLast line - see below)
I don't really want to force a commit as I may need to roll back. I guess I'm looking for something similar to the @@IDENTITY command in SQL Server...
Please can someone advise me on the best way to approach this and apologies in advance if this has been answered many times before... it must be a common question, but I can't find a suitable answer.
Thanks in advance.
My existing code is shown below:
<code>
Dim AddressSQL As String
AddressSQL = "INSERT INTO ADDRESS (AddressLine1, AddressLine2, City, County, Country, Postcode) values (AddressLine1, AddressLine2, City, County, Country, Postcode)"
DoCmd.RunSQL AddressSQL
Dim dbs As Database, rst As Recordset, strSQL As String
Dim fld As Field
Set dbs = CurrentDb
strSQL = "SELECT DISTINCTROW [AddressPK]" _
& "FROM ADDRESS WHERE [AddressLine1] = ' & AddressLine1 & '"
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
rst.MoveLast
<code>