AccessNovice123
Registered User.
- Local time
- Today, 10:23
- Joined
- Aug 16, 2017
- Messages
- 13
I am looking for VBA to do the following things for btn_click () on my form in my Access 2016 database:
I have the following code but it does not Update the existing records, it just adds them as new records.
- Import my excel file (import.xlsx) into an existing table (tblCustomers) where Cust_ID is the common field in the import file and table. This import will occur monthly so the table needs to be an accumulation of records. Would a temp table be a better practice?
- Add new records from import file to tblCustomers
- Update existing records from import file to tblCustomers where data from tblCustomers is different from the Import file for the same Cust_ID (i.e. last name changed due to marriage)
- Populate "Modified" date field in tblCustomers with date the import was run.
I have the following code but it does not Update the existing records, it just adds them as new records.
Code:
Private Sub btnUpdateAppend_Click() Dim filepath As String Dim User As String User = Environ("username") filepath = "C:\Users\" & User & "\Desktop\import.xlsx" If FileExist(filepath) Then DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblCustomers", filepath, True If IsNull(DLookup("[Cust_ID]", "NewData")) Then MsgBox "No new data to import!" Else DoCmd.OpenQuery "qryAppend2", acViewNormal MsgBox "New data imported!" End If If IsNull(DLookup("[Cust_ID]", "NewData1")) Then MsgBox "No new data to update!" Else DoCmd.OpenQuery "qryUPDATE", acViewNormal MsgBox "Data updated!" End If Else MsgBox "File not found. Please check filename or file location." End If End Sub