Return value in auto number field when inserting

svjensen

Board Regular
Joined
Jun 10, 2009
Messages
118
I am using VBA in excel to insert values into an Access DB (if I should post in excel questions please let me know).

In order to link my tables together I use the id from one table as the reference in others. Pretty standard I guess.
Now the thing is, that the id is inserted using auto numbering, and I would very much like to have the issued value returned to me, so that I can use it immediately for inserting data in related tables.
How can I do that?

I have inserted a very simple example of my code below.

Code:
Const ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Q:\PL\PLA\ALLE\03 Periodeplan\Sandkasse\Access programmering\TestDB.mdb;Persist Security Info=False"
 
Function SkrivTilDBViaSQL(Tekst As String) As Boolean
    Dim SQLstr As String
    Dim MyCon As New ADODB.Connection
 
    MyCon.ConnectionString = ConStr
    MyCon.Open
 
    SQLstr = "INSERT INTO tblTest (Tekst) VALUES ('" & Tekst & "')"
 
    MyCon.Execute SQLstr
 
    SkrivTilDBViaSQL = True
 
    'Oprydning i fht. DB
    MyCon.Close
    Set MyCon = Nothing
End Function


It seems that there might be some help here: http://databases.aspfaq.com/general.../autonumber-value-for-the-row-i-inserted.html
But I am uncertain how I could include that in my VBA script.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,813
Messages
6,121,705
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top