I have a search button, that searches for a job number (tbjobid) yesterday i spotted a issue where that if people either left the search term null, or pressed cancel, or input text into the field i got a error message, SO i amended the code, but i am not convinced it is written in the best way.
Can someone review and advise.
thanks
G
Can someone review and advise.
thanks
G
Code:
Private Sub btnButton_Click()
On Error GoTo handler
Dim SearchJob As String
Dim JobHold As Long
Dim LResponse As Integer
Dim deljob As Long
Dim strsql As String
If (Nz(tbJobID, 0) > 0) And ((Nz(cboxQueryType, "") = "") Or ((lblTitle.Caption <> "Rectification") And (Nz(cboxRequestArea, "") = "")) Or (Nz(cboxStockLoc, "") = "")) And cboxStatus <> 13 Then
If (tbRectification = "Rectification") And (Nz(cboxRequestArea, "") = "") And (Nz(cboxStockLoc, "") = "") Then
RectificationSearch = True
GoTo PerformSearch
Else
MsgBox "You can't do a search while you are entering a partially created job!" & vbNewLine & "To continue, you need to have a Query Type" & IIf((lblTitle.Caption <> "Rectification"), ", Requester Area", "") & " and Stock Location!", vbOKOnly + vbExclamation, "Missing"
ESCMessage
Cancel = True
End If
Else
PerformSearch:
AttemptSave
SearchJob = InputBox("Enter Job Number to Search . . . ", "Search Job")
'If (Nz(searchjob, 0) = 0) Or (Nz(DCount("*", "tblQCJobs", "Job_ID=" & searchjob), 0) = 0) Then
If (Nz(DCount("*", "tblQCJobs", "Job_ID=" & SearchJob), 0) < 1) Then
If Nz(SearchJob, 0) <> 0 Then
MsgBox "Job " & Format(SearchJob, "0000000") & " does not exist!", vbExclamation + vbOKOnly, "Not Found"
End If
SearchJob = JobHold
End If
If SearchJob <> JobHold Then
strsql = "SELECT tblQCJobs.Job_ID, tblQCJobs.Query_Type, tblQCJobs.Raised_Date, tblQCJobs.Raised_By, tblQCJobs.Cat_No, tblQCJobs.Stock_Location, tblQCJobs.Request_Area, tblQCJobs.Sup_Code, TblQCJobCategories.Root_Cause, TblQCJobCategories.Reason, TblQCJobCategories.Dealt_With, TblQCJobCategories.Outcome, TblQCJobCategories.Changed_By, TblQCJobCategories.Date_Changed " & vbCrLf & _
"FROM tblQCJobs LEFT JOIN TblQCJobCategories ON tblQCJobs.Job_ID = TblQCJobCategories.Job_ID " & vbCrLf & _
"WHERE (((tblQCJobs.Job_ID)= " & SearchJob & "));"
'Debug.Print strsql
Me.RecordSource = strsql
Me.Requery
cboxStatus = Nz(DLookup("Status_ID", "tblQCJobStatus", "Job_ID=" & Nz(tbJobID, 0) & " AND Status_Change=" & SQLDate(Nz(DMax("Status_Change", "tblQCJobStatus", "Job_ID=" & Nz(tbJobID, 0)), "1/1/1"))), 0)
End If
DoEvents
SetFormState "Normal"
End If
check_Not_closed
Exit Sub
handler:
If Err.Number = 13 Or Err.Number = 2471 Then
MsgBox "Invalid Job Number " & SearchJob
Err.Clear
ElseIf Err.Number = 3167 Then
Resume Next
ElseIf Err.Number = 3075 Then
Resume Next
ElseIf Err.Number = 0 Then
Debug.Print Err.Number
Err.Clear
Exit Sub
End If
btnQuit.Enabled = True
Exit Sub
Call LogError(Err.Number, Err.Description, "11", tbJobID)
Forms!frmMain.Visible = True
Exit Sub
End Sub