Code:
Public Function LoadMonthSales(commdlg As Object) As Boolean Dim strSQL As String
Dim strFileName As String
Dim strpath As String
Dim strPathFileName As String
On Error GoTo Load_Error
'strSQL = "INSERT INTO Download ( [ShipTo Customer], [Total Sales], [Profit], [State (s)], [Salesman Code (s)], [Class (s)], [ShipTo Name], Year, Category ) "
'strSQL = "INSERT INTO Download ( CID, Sales, [GP$], PROV, Salesman, IND, CustomerName, Year, Category ) "
With commdlg
.CancelError = True
.Flags = cdlOFNExplorer + cdlOFNFileMustExist + cdlOFNPathMustExist
.Filter = "Text Files (*.xls;*.txt)|*.xls;*.txt"
.FilterIndex = 0
.DialogTitle = "Load Monthly Sales"
.ShowOpen
If MsgBox("Load " & strReportCategory & " sales for " & Format(intReportMonth) & "/" & Format(intReportYear) & " from file " & .FileName & "?", vbOKCancel, "Load Adjustments From Excel") = vbCancel Then Exit Function
strFileName = .FileTitle
If InStr(strFileName, "#") > 0 Then
Mid(strFileName, InStr(strFileName, "#"), 1) = "."
End If
strpath = Left(.FileName, Len(.FileName) - Len(.FileTitle) - 1)
strPathFileName = strpath & "\" & strFileName
DoCmd.DeleteObject acTable, "tblTest"
DoCmd.TransferSpreadsheet acImport, , "tblTest", strPathFileName
'Excel 8.0;DSN=DownloadLinkSpecification;HDR=NO;IMEX=2;DATABASE
'strSQL = strSQL & " SELECT CID, Sales, [GP$], PROV, Salesman, IND, CustomerName, " & Format(ReportYear()) & " AS Year, """ & strReportCategory & """ AS Category FROM [" & strFileName & "] IN '' [Excel 8.0;HDR=Yes;IMEX=1;DATABASE=" & Left(.FileName, Len(.FileName) - Len(.FileTitle) - 1) & "] "
'strSQL = strSQL & " SELECT [ShipTo Customer], [Total Sales], [Profit], [State (s)], [Salesman Code (s)], [Class (s)], [ShipTo Name], " & Format(ReportYear()) & " AS Year, """ & strReportCategory & """ AS Category FROM [" & strFileName & "] IN '' [Text;DSN=DownloadLinkSpecification;FMT=Fixed;HDR=NO;IMEX=2;DATABASE=" & Left(.FileName, Len(.FileName) - Len(.FileTitle) - 1) & "] "
'strSQL = strSQL & " WHERE ((([ShipTo Customer]) Is Not Null) AND (([State (s)]) Is Not Null) AND (([Salesman Code (s)]) Is Not Null) AND (([Class (s)]) Is Not Null) AND (([ShipTo Name]) Is Not Null));"
'strSQL = strSQL & " WHERE (((CID) Is Not Null) AND ((PROV) Is Not Null) AND ((Salesman) Is Not Null) AND ((IND) Is Not Null) AND ((CustomerName) Is Not Null));"
End With
With CurrentDb()
'.QueryDefs!appqImportSales.SQL = strSQL 'cw old code replaced with transfer sheet above
.Execute "Delete * FROM Download", dbFailOnError
'.Execute "appqImportSales", dbFailOnError 'cw old code replaced with transfer sheet above as could not get SQL to import excel
.Execute "qryDeleteRow1", dbFailOnError 'first row contains file names
If strReportCategory = "Tools" Then
.Execute "qryAppendExcelToolsToDownload", dbFailOnError
Else
.Execute "qryAppendExcelBearingsToDownload", dbFailOnError
End If
'.Execute "Add_MonthlyDownload", dbFailOnError
End With
'cw update Category and Year using DAO
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Download")
Do While Not rs.EOF
rs.Edit
rs!YEAR = ReportYear
rs!CATEGORY = strReportCategory
rs.Update
rs.MoveNext
Loop
With CurrentDb()
.Execute "Update_MonthlyDownload", dbFailOnError 'cw - links download to tablSales and updates - created by former developer
.Execute "Add_MonthlyDownload", dbFailOnError 'cw - Updates Sales data - created by former developer
End With
LoadMonthSales = True
Load_Exit:
Exit Function
Load_Error:
MsgBox "Sales Load Failed.", vbExclamation
Resume Load_Exit
End Function