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/how-do-i-get-the-identity/autonumber-value-for-the-row-i-inserted.html
But I am uncertain how I could include that in my VBA script.
 

Forum statistics

Threads
1,081,726
Messages
5,360,906
Members
400,602
Latest member
newaqua

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top