dkmoreland
Registered User.
- Local time
- Today, 10:15
- Joined
- Dec 6, 2017
- Messages
- 129
For the life of me, I cannot tell why the if statement below (highlighted in red) will not work. I have been running it through the debugger all day but I just am not seeing it.
I am trying to add records from a spreadsheet to a table. I need to exclude records with a sales class value of 25 or 26. The code works - it creates the records - but does not exclude those defined in the if statement. It grabs everything.
I have said many, many bad words trying to make this work. Suggestions, please?
I am trying to add records from a spreadsheet to a table. I need to exclude records with a sales class value of 25 or 26. The code works - it creates the records - but does not exclude those defined in the if statement. It grabs everything.
I have said many, many bad words trying to make this work. Suggestions, please?
Code:
Private Sub CmdPrepKPI_Click()
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim FirstSeparator As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsDefaults As DAO.Recordset
Dim strMsg As String
Dim strSQL As String
Dim strJob As String
Dim strImportFile As String
Dim strJobName As String
Dim dStart_date As Date
Dim dEnd_date As Date
Dim strDateText As String
Dim strSalesClass As String
On Error GoTo ErrProc
Set db = CurrentDb()
strImportFile = Me.TextImport
If Me.TextImport = "" Then
Exit Sub
MsgBox "Please select a file.", vbOKOnly
End If
Set rs = db.OpenRecordset("Production Raw Data")
'open spreadsheet (Input Sheet) page, get data from specific cells
Set appExcel = Excel.Application
Set wbk = appExcel.Workbooks.Open(Me.TextImport)
'populate the date fields
strDateText = Range("A7").Value
i = InStr(strDateText, "To:")
dStart_date = Trim(Mid(strDateText, 6, i - 6 - 1))
dEnd_date = Trim(Mid(strDateText, i + 3))
'populate the job number and number
intRow = 11
Debug.Print intRow
strSalesClass = Right(Range("J" & intRow).Value, 2)
Debug.Print strSalesClass
Do While Len(Range("A" & intRow).Formula) > 0
' repeat until first empty cell in column A
With rs
If Not IsEmpty(Range("B" & intRow)) Then
[COLOR="Red"][B]If strSalesClass <> "25" Or strSalesClass <>"26" Then
.AddNew ' create a new record
' add values to each field in the record
.Fields("JobNum") = Range("B" & intRow).Value
.Fields("JobName") = Range("E" & intRow).Value
.Fields("sales_class") = Range("J" & intRow).Value
.Fields("start_date") = Format(dStart_date, "Short Date")
.Fields("end_date") = Format(dEnd_date, "Short Date")
End If[/B][/COLOR]
.Update ' stores the new record
End If
End With
intRow = intRow + 1 ' next row
Loop
rs.Close
'close excel
wbk.Close (False) 'close without saving changes
appExcel.Quit
Set wbk = Nothing
Set appExcel = Nothing
MsgBox "File " + strImportFile + " has been imported", vbOKOnly
DoCmd.Close
ExitProc:
Set db = Nothing
Exit Sub
ErrProc:
Select Case Err.Number
Case 462 'excel not open
Set appExcel = New Excel.Application
Resume Next
Case 3125
MsgBox "The selected workbook is not the correct format.", vbOKOnly
Resume ExitProc
Case 3201
MsgBox "Job Prefix is not valid. Please add the new prefix to the prefix list. Import was cancelled.", vbOKOnly
Resume ExitProc
Case Else
MsgBox Err.Number & "--" & Err.Description
Resume ExitProc
Resume Next
End Select
End Sub