I need to perform some sort of error check to test whether data that I am importing into a table has already been imported. I know very little VB and need help if someone is willing to point me in the right direction.
For simplicity sake, I will only refer to the three tables in my .mdb that will be affected. I have "tblInvoice," "tblProductDetails," & "Temp" tables. My process imports a .CSV file into the "Temp" table and from there cleans the data and then moves it via queries to the "tblInvoice" and "tblProductDetails" tables. The Invoice and ProductDetails tables have a primary key "InvoiceNum" with a one-to-many relationship. I want to be able to check and determine if a particular InvoiceNum has already been imported and if so, stop the entire process and alert the user without continuing the import process. I currently use MACROS to import each Invoice, "clean" the data and perform calculations, and then move the data to the appropriate tables.
I know that if I have warnings turned ON that the user will be alerted that there is a duplicate key but I would rather keep the warnings OFF and alert the user some other way.
I have converted the macro to code if that will help anyone who is willing to help me. The code is:
'------------------------------------------------------------
' mcrImportProcess
'
'------------------------------------------------------------
Function mcrImportProcess()
On Error GoTo mcrImportProcess_Err
DoCmd.SetWarnings False
' Imports Invoice Text File in tblTemp
DoCmd.RunMacro "mcrImportInvoice", , ""
' Runs query to populate the tblInvoice table
DoCmd.OpenQuery "qryInvoice,Date,& CustomerID", acNormal, acEdit
' Runs query to populate tblProduct Details table
DoCmd.OpenQuery "qryProductDetails", acNormal, acEdit
'There are a series of other queries that run here but I did not include them in this example.
'I want the code to stop the processing without populating any tables, via queries, once it has found
' the InvoiceNum already entered in the "tblInvoice" table.
DoCmd.SetWarnings True
Exit Function
mcrImportProcess_Exit:
Exit Function
mcrImportProcess_Err:
MsgBox Error$
Resume mcrImportProcess_Exit
End Function
For simplicity sake, I will only refer to the three tables in my .mdb that will be affected. I have "tblInvoice," "tblProductDetails," & "Temp" tables. My process imports a .CSV file into the "Temp" table and from there cleans the data and then moves it via queries to the "tblInvoice" and "tblProductDetails" tables. The Invoice and ProductDetails tables have a primary key "InvoiceNum" with a one-to-many relationship. I want to be able to check and determine if a particular InvoiceNum has already been imported and if so, stop the entire process and alert the user without continuing the import process. I currently use MACROS to import each Invoice, "clean" the data and perform calculations, and then move the data to the appropriate tables.
I know that if I have warnings turned ON that the user will be alerted that there is a duplicate key but I would rather keep the warnings OFF and alert the user some other way.
I have converted the macro to code if that will help anyone who is willing to help me. The code is:
'------------------------------------------------------------
' mcrImportProcess
'
'------------------------------------------------------------
Function mcrImportProcess()
On Error GoTo mcrImportProcess_Err
DoCmd.SetWarnings False
' Imports Invoice Text File in tblTemp
DoCmd.RunMacro "mcrImportInvoice", , ""
' Runs query to populate the tblInvoice table
DoCmd.OpenQuery "qryInvoice,Date,& CustomerID", acNormal, acEdit
' Runs query to populate tblProduct Details table
DoCmd.OpenQuery "qryProductDetails", acNormal, acEdit
'There are a series of other queries that run here but I did not include them in this example.
'I want the code to stop the processing without populating any tables, via queries, once it has found
' the InvoiceNum already entered in the "tblInvoice" table.
DoCmd.SetWarnings True
Exit Function
mcrImportProcess_Exit:
Exit Function
mcrImportProcess_Err:
MsgBox Error$
Resume mcrImportProcess_Exit
End Function