GKChesterton
Programmer
I like to use .Addnew with a DAO.recordset. As the new record is added, my habit is to grab the newly-generated primary key for use in the next steps, which I can do before .Update.
Now I'm switching from DAO to ADO, and from MS Access back-end to MySQL 5 back-end. And I don't understand how to grab the primary key. Also, with multiple users, I need to make a clean fast grab. I'm using InnoDB tables (I need foreign key constraints).
So far my new VBA code looks like this:
[tt]
Dim MyDatabase As ADODB.Connection
Dim rsQuote As New ADODB.Recordset
Set MyDatabase = New ADODB.Connection
MyDatabase .Open "File Name=C:\MyDatabase.udl"
rsQuote.Open "pBOM", MyDatabase, _
adOpenDynamic, adLockOptimistic
rsQuote.AddNew
rsQuote!field1 = var1
rsQuote!field2 = var2
rsQuote!field3 = var3
- etc., I like not using a flat
SQL query/command for all this.
rsQuote.Update
MyKey = rsQuote!PrimaryKey
rsQuote.Close[/tt]
All works well, but MyKey doesn't get a value.
Should I change my ways and switch to .Execute and SQL statements? Or is there a way to adapt the code above to get the new AUTO_INCREMENT value?
Now I'm switching from DAO to ADO, and from MS Access back-end to MySQL 5 back-end. And I don't understand how to grab the primary key. Also, with multiple users, I need to make a clean fast grab. I'm using InnoDB tables (I need foreign key constraints).
So far my new VBA code looks like this:
[tt]
Dim MyDatabase As ADODB.Connection
Dim rsQuote As New ADODB.Recordset
Set MyDatabase = New ADODB.Connection
MyDatabase .Open "File Name=C:\MyDatabase.udl"
rsQuote.Open "pBOM", MyDatabase, _
adOpenDynamic, adLockOptimistic
rsQuote.AddNew
rsQuote!field1 = var1
rsQuote!field2 = var2
rsQuote!field3 = var3
- etc., I like not using a flat
SQL query/command for all this.
rsQuote.Update
MyKey = rsQuote!PrimaryKey
rsQuote.Close[/tt]
All works well, but MyKey doesn't get a value.
Should I change my ways and switch to .Execute and SQL statements? Or is there a way to adapt the code above to get the new AUTO_INCREMENT value?
[purple]If we knew what it was we were doing, it would not be called
research [blue]database[white].[/white]development[/blue], would it? [tab]-- Albert Einstein[/purple]