Access VB to caputure a Control Table record ID and pass it back to use as a field in records being imported to another table

This is a follow on question to a prior thread where I posed the question regarding how to create a new record in a table that I use for capturing control information about date ranges of info being imported to another table.

Here is link to prior question for reference
Prior Question Link

I am importing a date range of GL Transactions at various times (usually once per month).  I wanted a control table to maintain the range of dates of what was being imported as well as the date that the import happened.  

THIS QUESTION deals with when I import the thousands of records of GL Transactions how can I capture the ID# of that Control record I just created and pass it back to all of the records being imported and added as a field to each of the records being imported at that time.

 Below is my Event Procedure on On Click where it executes a "SavedImport" to get all of the transactions from the Excel sheet and Imports those transactions into the table named "QB GL Transactions".  The table that has the newly created control ID is "Data Import Control".

Private Sub cmdImportGLTransactions_Click()

    On Error GoTo cmdCloseImport_Click_Err
    DoCmd.RunSavedImportExport "Import-QBGLTransactions"
    CurrentDb.Execute "INSERT INTO [Data Import Control]([BeginningTransactDate],[EndingTransactDate], [EarliestInputDate],[LatestInputDate]) SELECT [Beginning Transact Date], [Ending Transact Date], [Earliest Input Date], [Latest Input Date] FROM [GL Transaction Import Date Ranges]"
    MsgBox "Your data has been Imported from Excel", vbOKOnly, "Excel Data Imported"
    Exit Sub

    MsgBox "Error$"
    Exit Sub
End Sub

Open in new window

Who is Participating?
peter57rConnect With a Mentor Commented:
There isn't any way you can get inside an Import to add extra data or fields.
You would have to complete your import of transactions and then run an update query to apply the controlId to the imported records  as below.

Assuming your ControlID is an autonumber or other sequential number, you can get it from the import control table using a DMAX() function.

dim strsql as string
Dim MaxID as long
MaxiD= dmax("[ControlIDFieldname]", "[Data Import Control]")
strsql = "Update [QB GL Transactions] set [controlidfieldname] = " maxid & " where [Controlidfieldname] is null"
currentdb.execute strsql, dbfailonerror
wlwebbAuthor Commented:
Just got back into town.  I will attempt this tomorrow.  Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.