motscotland
Registered User.
- Local time
- Today, 22:26
- Joined
- Oct 29, 2003
- Messages
- 19
Hi,
I have a search form that takes values from 7 combo boxes and builds an SQL query which then outputs a filtered list.
The code is :
What I want to do is insert AND / OR option buttons between combo boxes on the form so the user can select different combinations for their query-
IE - Selection -
Qual1 AND/OR Qual2 AND/OR Qual3
AND/OR
Disc1 AND/OR Disc2 AND/OR Disc3
AND/OR
Work
I hope the problem is clearly explained - I am a relative newbie to Access and SQL/VBA so fairly explicit advice required please!
Thanks in advance for your help.
Cheers the now,
Myles

I have a search form that takes values from 7 combo boxes and builds an SQL query which then outputs a filtered list.
The code is :
Code:
Private Sub cmdOK_Click()
' Pointer to error handler
On Error GoTo cmdOK_Click_err
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strqual1 As String
Dim strqual2 As String
Dim strqual3 As String
Dim strDisc1 As String
Dim strDisc2 As String
Dim strDisc3 As String
Dim strWork As String
Dim strSQL As String
' Identify the database and assign it to the variable
Set db = CurrentDb
' Check for the existence of the query, create it if not found,
' and assign it to the variable
If Not QueryExists("qryStaffListQuery") Then
Set qdf = db.CreateQueryDef("qryStaffListQuery")
Else
Set qdf = db.QueryDefs("qryStaffListQuery")
End If
' Get the values from the combo boxes
If IsNull(Me.cboQual1.Value) Then
strqual1 = " Like '*' "
Else
strqual1 = "='" & Me.cboQual1.Value & "' "
End If
If IsNull(Me.cboQual2.Value) Then
strqual2 = " Like '*' "
Else
strqual2 = "='" & Me.cboQual2.Value & "' "
End If
If IsNull(Me.cboqual3.Value) Then
strqual3 = " Like '*' "
Else
strqual3 = "='" & Me.cboqual3.Value & "' "
End If
If IsNull(Me.cboDisc1.Value) Then
strDisc1 = " Like '*' "
Else
strDisc1 = "='" & Me.cboDisc1.Value & "' "
End If
If IsNull(Me.cboDisc2.Value) Then
strDisc2 = " Like '*' "
Else
strDisc2 = "='" & Me.cboDisc2.Value & "' "
End If
If IsNull(Me.cboDisc3.Value) Then
strDisc3 = " Like '*' "
Else
strDisc3 = "='" & Me.cboDisc3.Value & "' "
End If
If IsNull(Me.cboWork.Value) Then
strWork = " Like '*' "
Else
strWork = "='" & Me.cboWork.Value & "' "
End If
' Build the SQL string
strSQL = "SELECT tblCVq2.* " & _
"FROM tblCVq2 " & _
"WHERE tblCVq2.Qualifications" & strqual1 & _
"AND tblCVq2.Qualifications" & strqual2 & _
"AND tblCVq2.Qualifications" & strqual3 & _
"AND tblCVq2.Discipline" & strDisc1 & _
"OR tblCVq2.Discipline" & strDisc2 & _
"OR tblCVq2.Discipline" & strDisc3 & _
"AND tblCVq2.work" & strWork & _
"ORDER BY tblCVq2.Work;"
' Pass the SQL string to the query
qdf.sql = strSQL
' Turn off screen updating
DoCmd.Echo False
' Check the state of the query and close it if it is open
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryStaffListQuery") = acObjStateOpen Then
DoCmd.Close acQuery, "qryStaffListQuery"
End If
' Open the query
DoCmd.OpenQuery "qryStaffListQuery"
cmdOK_Click_exit:
' Turn on screen updating
DoCmd.Echo True
' Clear the object variables
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmdOK_Click_err:
' Handle errors
MsgBox "An unexpected error has occurred." & _
vbCrLf & "Please note of the following details:" & _
vbCrLf & "Error Number: " & Err.Number & _
vbCrLf & "Description: " & Err.Description _
, vbCritical, "Error"
Resume cmdOK_Click_exit
End Sub
What I want to do is insert AND / OR option buttons between combo boxes on the form so the user can select different combinations for their query-
IE - Selection -
Qual1 AND/OR Qual2 AND/OR Qual3
AND/OR
Disc1 AND/OR Disc2 AND/OR Disc3
AND/OR
Work
I hope the problem is clearly explained - I am a relative newbie to Access and SQL/VBA so fairly explicit advice required please!
Thanks in advance for your help.
Cheers the now,
Myles

Last edited: