Just to give you an idea = ):
Private Sub GenericImport(cobdt As Date, sDataType As String, sFileLocation As String, Optional sRange As String)
On Error GoTo Err_Import_Data
Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
Dim sSQL As String, sQSLoc As String, sFullName As String, sLoadTable As String, sTargetTable As String
DoCmd.SetWarnings False
If FileExist(sFileLocation) = False Then
MsgBox "I can't find your " & sDataType & " import file." & Chr(10) & "Please check the filename and path.", vbOKOnly, ProjName
Exit Sub
End If
sLoadTable = Me.cbo_FileType.Column(1)
sTargetTable = Me.cbo_FileType.Column(2)
'CLEAR LOADING TABLE
CurrentProject.Connection.Execute "DELETE * FROM " & sLoadTable
'IMPORT INTO LOADING TABLE
If sRange = "" Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, sLoadTable, sFileLocation, True
Else
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, sLoadTable, sFileLocation, True, sRange
End If
Select Case sDataType
Case "myFile"
sSQL = "DELETE * FROM myFile " & _
" Where COB = #" & cobdt & "# AND PeriodType = " & Me.cbo_PeriodTypes & " AND Vendor_Num = '" & Me.cbo_Vendor & "';"
CurrentProject.Connection.Execute sSQL
sSQL = "Update tbl_MyFile_Load " & _
"SET COB = #" & cobdt & "#,PeriodType = " & Me.cbo_PeriodTypes & ", Vendor_Num = '" & Me.cbo_Vendor & "', AddedBy = '" & ReturnUserName & "';"
CurrentProject.Connection.Execute sSQL
'MOVE FROM LOADING TABLE TO DATA TABLES
DoCmd.OpenQuery "qryApp_" & sTargetTable
End Select
'CLEAR LOADING TABLE
'CurrentProject.Connection.Execute "DELETE * FROM " & sLoadTable
MsgBox sDataType & " data imported.", vbInformation, ProjName
Exit_Import_Data:
DoCmd.SetWarnings True
Exit Sub
Err_Import_Data:
MsgBox Err.Description & Chr(10) & "Import may not be successful.", vbCritical, ProjName
Resume Exit_Import_Data
End Sub