Hi all,
I do not have much experience working with Error Handling code and therefore am running into quite a few problems in constructing this database. First off, I have a form (Form_GUI) where a user inputs information and then clicks "Ok" which executes a query (strQuery) and opens a continuous form (Form_GridDisplay1) to display the results. However, when the search doesn't match any records I get an error in vba. To resolve this I have tried to implement error handling, see below:
Form_GUI:
Form_GridDisplay1:
When I run a search for a record that does not exist, I get two error messages (first due to GridDisplay1 opening, and second from the GUI error handling because the open_form failed to complete). Two is annoying, one is much preferred. But the real problem lies in that after I receive this first error message no matter what I do I cannot run a search for a record that I know exists because I will just keep getting error msg box. I have tried using the DCOUNT function and RecordSetClone to specifically handle the problem of 0 query results however both have refused to work. Obviously that is preferred instead of a general error msg box just assuming the error is due to lack of results. Any help would be much appreciated, thank you.
I do not have much experience working with Error Handling code and therefore am running into quite a few problems in constructing this database. First off, I have a form (Form_GUI) where a user inputs information and then clicks "Ok" which executes a query (strQuery) and opens a continuous form (Form_GridDisplay1) to display the results. However, when the search doesn't match any records I get an error in vba. To resolve this I have tried to implement error handling, see below:
Form_GUI:
Code:
Option Compare Database
Option Explicit
Public strQuery As String
Private Sub ok_Click()
'Run query
Dim argCount As Integer
On Error GoTo Err_Handlerr
If IsNull(creatorcb.Value) And IsNull(assigncb.Value) And IsNull(Combo31) And IsNull(Text10) Then
MsgBox "You Need To Select Some Values", vbCritical, "Lead Tracking"
Exit Sub
End If
strQuery = "SELECT * From Lead Where "
If Not IsNull(creatorcb.Value) Then
strQuery = strQuery & "Creator Like '" & creatorcb.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(assigncb.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "AssignedTo Like '" & assigncb.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Text8.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "BorrowerFirstName Like '" & Text8.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Text10.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "BorrowerLastName Like '" & Text10.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Text12.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "Company Like '" & Text12.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Text16.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "PropertyCity Like '" & Text16.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Text14.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "PropertyState Like '" & Text14.Value & "'"
argCount = argCount + 1
End If
If Not IsNull(Combo31.Value) Then
If argCount > 0 Then strQuery = strQuery & " AND "
strQuery = strQuery & "Status Like '" & Combo31.Value & "'"
argCount = argCount + 1
End If
DoCmd.OpenForm "GridDisplay1", acNormal, , , acFormReadOnly, acDialog
Exit_Handlerr:
Exit Sub
Err_Handlerr:
MsgBox "Please Modify Your Search And Try Again", vbOKOnly, "Lead Tracking - Error"
Resume Exit_Handlerr
End Sub
Form_GridDisplay1:
Code:
Private Sub Form_Open(Cancel As Integer)
'---- SQL comes from Form_GUI.strQuery ----
On Error GoTo Err_Handlerrr
Me.RecordSource = Form_GUI.strQuery
Me.cursorlock.SetFocus
Exit_Handlerrr:
Cancel = True
Exit Sub
Err_Handlerrr:
MsgBox "Your Search Returned No Results Or An Unknown Error Occurred", vbOKOnly, "Lead Tracking"
Resume Exit_Handlerrr
End Sub
When I run a search for a record that does not exist, I get two error messages (first due to GridDisplay1 opening, and second from the GUI error handling because the open_form failed to complete). Two is annoying, one is much preferred. But the real problem lies in that after I receive this first error message no matter what I do I cannot run a search for a record that I know exists because I will just keep getting error msg box. I have tried using the DCOUNT function and RecordSetClone to specifically handle the problem of 0 query results however both have refused to work. Obviously that is preferred instead of a general error msg box just assuming the error is due to lack of results. Any help would be much appreciated, thank you.