Return value in auto number field when inserting

svjensen

Board Regular
Joined
Jun 10, 2009
Messages
118
This question is a mirror of
http://www.mrexcel.com/forum/showthread.php?t=491616 as I am uncertain if it should be posted as en excel or an access question.

I am using VBA in excel to insert values into an Access DB.

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"
Code:
[FONT=Verdana][COLOR=black]Function SkrivTilDBViaSQL(Tekst As String) As Boolean[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] Dim SQLstr As String[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] Dim MyCon As New ADODB.Connection[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black] MyCon.ConnectionString = ConStr[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] MyCon.Open[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black] SQLstr = "INSERT INTO tblTest (Tekst) VALUES ('" & Tekst & "')"[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black] MyCon.Execute SQLstr[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black] SkrivTilDBViaSQL = True[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black] 'Oprydning i fht. DB[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] MyCon.Close[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] Set MyCon = Nothing[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]End Function[/FONT][/COLOR]


It seems that there might be some help here: http://databases.aspfaq.com/general/...-inserted.html
But I am uncertain how I could include that in my VBA script.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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