C
chickalina
I have a search form with a combo button. There are 6 different combo boxes
to choose from for criteria. Below is the code... it stops working after the
program closes and re-opens. If I cut and paste it works again until I close.
Private Sub cmdRunReport_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strCountry As String
Dim strStructural As String
Dim strCategory As String
Dim strJurisdiction As String
Dim strBenefitType As String
Dim strCurrentStatus As String
Dim strHWContact As String
Dim strExternalContact As String
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("qry_planninglookup")
'THIS CODE IS TO BE USED IF YOU MUST HAVE ALL FIELDS SATISFIED
' If IsNull(cbocategory) Or IsNull(cbojurisdiction) Then
' MsgBox "You must choose both one of the criteria." _
' & vbCrLf & "Please try again.", vbExclamation, _
' "More information required."
' Exit Sub
' End If
' DoCmd.OpenQuery "qry_PlanningLookup", acViewNormal, acEdit
' DoCmd.Close acForm, "frm_ideas_bank"
'KEEP IN MIND THAT IF YOU OPEN THE QUERY THROUGH THE DATABASE, IT WILL USE THE
'CRITERIA PREVIOUSLY USED.
'THIS "IF ISNULL" CODE HERE IS USED IF THE USER LEAVES ONE OF THE FIELDS BLANK
'THE RETURN WOULD BE BASED ON ONLY THE OPTIONS CHOSEN, AND USE ALL OF THE
'CRITERIA MET ON THE THIRD.
If IsNull(Me.cboCountry.Value) Then
strCountry = " Like '*' "
Else
strCountry = "='" & Me.cboCountry.Value & "' "
End If
If IsNull(Me.cboCategory.Value) Then
strCategory = " Like '*' "
Else
strCategory = "='" & Me.cboCategory.Value & "' "
End If
If IsNull(Me.cboStructural.Value) Then
strStructural = " Like '*' "
Else
strStructural = "='" & Me.cboStructural.Value & "' "
End If
If IsNull(Me.cboCurrentStatus.Value) Then
strCurrentStatus = " Like '*' "
Else
strCurrentStatus = "='" & Me.cboCurrentStatus.Value & "' "
End If
If IsNull(Me.cboJurisdiction.Value) Then
strJurisdiction = " Like '*' "
Else
strJurisdiction = "='" & Me.cboJurisdiction.Value & "' "
End If
If IsNull(Me.cboBenefitType.Value) Then
strBenefitType = " Like '*' "
Else
strBenefitType = "='" & Me.cboBenefitType.Value & "' "
End If
If IsNull(Me.cboHWContact.Value) Then
strHWContact = " Like '*' "
Else
strHWContact = "='" & Me.cboHWContact.Value & "' "
End If
If IsNull(Me.cboExternalContact.Value) Then
strExternalContact = " Like '*' "
Else
strExternalContact = "='" & Me.cboExternalContact.Value & "' "
End If
strSQL = "SELECT tbl_ideas_bank.* " & _
"FROM tbl_ideas_bank " & _
"WHERE tbl_ideas_bank.countryid " & strCountry & _
"AND tbl_ideas_bank.ideacategory " & strCategory & _
"AND tbl_ideas_bank.structural " & strStructural & _
"AND tbl_ideas_bank.currentstatus " & strCurrentStatus & _
"AND tbl_ideas_bank.ideajurisdiction " & strJurisdiction & _
"AND tbl_ideas_bank.benefittype " & strBenefitType & _
"AND tbl_ideas_bank.externalcontact " & strExternalContact
& _
"ORDER BY tbl_ideas_bank.ideadescription;"
qdf.SQL = strSQL
DoCmd.OpenQuery "qry_PlanningLookup"
DoCmd.Close acForm, Me.Name
Set qdr = Nothing
Set db = Nothing
End Sub
Thanks for your help!
to choose from for criteria. Below is the code... it stops working after the
program closes and re-opens. If I cut and paste it works again until I close.
Private Sub cmdRunReport_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strCountry As String
Dim strStructural As String
Dim strCategory As String
Dim strJurisdiction As String
Dim strBenefitType As String
Dim strCurrentStatus As String
Dim strHWContact As String
Dim strExternalContact As String
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("qry_planninglookup")
'THIS CODE IS TO BE USED IF YOU MUST HAVE ALL FIELDS SATISFIED
' If IsNull(cbocategory) Or IsNull(cbojurisdiction) Then
' MsgBox "You must choose both one of the criteria." _
' & vbCrLf & "Please try again.", vbExclamation, _
' "More information required."
' Exit Sub
' End If
' DoCmd.OpenQuery "qry_PlanningLookup", acViewNormal, acEdit
' DoCmd.Close acForm, "frm_ideas_bank"
'KEEP IN MIND THAT IF YOU OPEN THE QUERY THROUGH THE DATABASE, IT WILL USE THE
'CRITERIA PREVIOUSLY USED.
'THIS "IF ISNULL" CODE HERE IS USED IF THE USER LEAVES ONE OF THE FIELDS BLANK
'THE RETURN WOULD BE BASED ON ONLY THE OPTIONS CHOSEN, AND USE ALL OF THE
'CRITERIA MET ON THE THIRD.
If IsNull(Me.cboCountry.Value) Then
strCountry = " Like '*' "
Else
strCountry = "='" & Me.cboCountry.Value & "' "
End If
If IsNull(Me.cboCategory.Value) Then
strCategory = " Like '*' "
Else
strCategory = "='" & Me.cboCategory.Value & "' "
End If
If IsNull(Me.cboStructural.Value) Then
strStructural = " Like '*' "
Else
strStructural = "='" & Me.cboStructural.Value & "' "
End If
If IsNull(Me.cboCurrentStatus.Value) Then
strCurrentStatus = " Like '*' "
Else
strCurrentStatus = "='" & Me.cboCurrentStatus.Value & "' "
End If
If IsNull(Me.cboJurisdiction.Value) Then
strJurisdiction = " Like '*' "
Else
strJurisdiction = "='" & Me.cboJurisdiction.Value & "' "
End If
If IsNull(Me.cboBenefitType.Value) Then
strBenefitType = " Like '*' "
Else
strBenefitType = "='" & Me.cboBenefitType.Value & "' "
End If
If IsNull(Me.cboHWContact.Value) Then
strHWContact = " Like '*' "
Else
strHWContact = "='" & Me.cboHWContact.Value & "' "
End If
If IsNull(Me.cboExternalContact.Value) Then
strExternalContact = " Like '*' "
Else
strExternalContact = "='" & Me.cboExternalContact.Value & "' "
End If
strSQL = "SELECT tbl_ideas_bank.* " & _
"FROM tbl_ideas_bank " & _
"WHERE tbl_ideas_bank.countryid " & strCountry & _
"AND tbl_ideas_bank.ideacategory " & strCategory & _
"AND tbl_ideas_bank.structural " & strStructural & _
"AND tbl_ideas_bank.currentstatus " & strCurrentStatus & _
"AND tbl_ideas_bank.ideajurisdiction " & strJurisdiction & _
"AND tbl_ideas_bank.benefittype " & strBenefitType & _
"AND tbl_ideas_bank.externalcontact " & strExternalContact
& _
"ORDER BY tbl_ideas_bank.ideadescription;"
qdf.SQL = strSQL
DoCmd.OpenQuery "qry_PlanningLookup"
DoCmd.Close acForm, Me.Name
Set qdr = Nothing
Set db = Nothing
End Sub
Thanks for your help!