M
Melissa
Is it possible to manipulate this code to have more than one multi-select
list. I have a search criteria form created in 2003 Access. It works
perfectly until I try to add additional multi-select lists.
I get the message: "Compile Error: For control variable already in use" if I
try to add additional multi-select lists.
Here is the code I am using. I am not very familiar with code. I got this
code from an example database so as much explanation on the solution to this
problem would be very much appreciated.
Thanks,
Melissa
_______________________________________________________________________
Option Compare Database
Option Explicit
Private Sub btnClear_Click()
Dim intIndex As Integer
' Clear all search items
Me.txtFirstName = ""
Me.txtLastName = ""
Me.txtMaxAge = ""
Me.txtMinAge = ""
Me.cmbCompany = 0
Me.cmbCountry = 0
' De-select each item in Color List (multiselect list)
For intIndex = 0 To Me.lstFavColor.ListCount - 1
Me.lstFavColor.Selected(intIndex) = False
Next
End Sub
Private Sub btnSearch_Click()
' Update the record source
Me.frmsubClients.Form.RecordSource = "SELECT * FROM qryClientData " &
BuildFilter
' Requery the subform
Me.frmsubClients.Requery
End Sub
Private Sub Form_Load()
' Clear the search form
btnClear_Click
End Sub
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim varColor As Variant
Dim varItem As Variant
Dim intIndex As Integer
varWhere = Null ' Main filter
varColor = Null ' Subfilter used for colors
' Check for LIKE First Name
If Me.txtFirstName > "" Then
varWhere = varWhere & "[FirstName] LIKE """ & Me.txtFirstName & "*""
AND "
End If
' Check for LIKE Last Name
If Me.txtLastName > "" Then
varWhere = varWhere & "[LastName] LIKE """ & Me.txtLastName & "*""
AND "
End If
' Check for min Age
If Me.txtMinAge > "" Then
varWhere = varWhere & "[Age] > " & Me.txtMinAge & " AND "
End If
' Check for max Age
If Me.txtMaxAge > "" Then
varWhere = varWhere & "[Age] < " & Me.txtMaxAge & " AND "
End If
' Check for CompanyID
If Me.cmbCompany > 0 Then
varWhere = varWhere & "[CompanyID] = " & Me.cmbCompany & " AND "
End If
' Check for CountryID
If Me.cmbCountry > 0 Then
varWhere = varWhere & "[CountryID] = " & Me.cmbCountry & " AND "
End If
' Check for Colors in multiselect list
For Each varItem In Me.lstFavColor.ItemsSelected
varColor = varColor & "[FavColor] = """ & _
Me.lstFavColor.ItemData(varItem) & """ OR "
Next
' Test to see if we have subfilter for colors...
If IsNull(varColor) Then
' do nothing
Else
' strip off last "OR" in the filter
If Right(varColor, 4) = " OR " Then
varColor = Left(varColor, Len(varColor) - 4)
End If
' Add some parentheses around the subfilter
varWhere = varWhere & "( " & varColor & " )"
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function
list. I have a search criteria form created in 2003 Access. It works
perfectly until I try to add additional multi-select lists.
I get the message: "Compile Error: For control variable already in use" if I
try to add additional multi-select lists.
Here is the code I am using. I am not very familiar with code. I got this
code from an example database so as much explanation on the solution to this
problem would be very much appreciated.
Thanks,
Melissa
_______________________________________________________________________
Option Compare Database
Option Explicit
Private Sub btnClear_Click()
Dim intIndex As Integer
' Clear all search items
Me.txtFirstName = ""
Me.txtLastName = ""
Me.txtMaxAge = ""
Me.txtMinAge = ""
Me.cmbCompany = 0
Me.cmbCountry = 0
' De-select each item in Color List (multiselect list)
For intIndex = 0 To Me.lstFavColor.ListCount - 1
Me.lstFavColor.Selected(intIndex) = False
Next
End Sub
Private Sub btnSearch_Click()
' Update the record source
Me.frmsubClients.Form.RecordSource = "SELECT * FROM qryClientData " &
BuildFilter
' Requery the subform
Me.frmsubClients.Requery
End Sub
Private Sub Form_Load()
' Clear the search form
btnClear_Click
End Sub
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim varColor As Variant
Dim varItem As Variant
Dim intIndex As Integer
varWhere = Null ' Main filter
varColor = Null ' Subfilter used for colors
' Check for LIKE First Name
If Me.txtFirstName > "" Then
varWhere = varWhere & "[FirstName] LIKE """ & Me.txtFirstName & "*""
AND "
End If
' Check for LIKE Last Name
If Me.txtLastName > "" Then
varWhere = varWhere & "[LastName] LIKE """ & Me.txtLastName & "*""
AND "
End If
' Check for min Age
If Me.txtMinAge > "" Then
varWhere = varWhere & "[Age] > " & Me.txtMinAge & " AND "
End If
' Check for max Age
If Me.txtMaxAge > "" Then
varWhere = varWhere & "[Age] < " & Me.txtMaxAge & " AND "
End If
' Check for CompanyID
If Me.cmbCompany > 0 Then
varWhere = varWhere & "[CompanyID] = " & Me.cmbCompany & " AND "
End If
' Check for CountryID
If Me.cmbCountry > 0 Then
varWhere = varWhere & "[CountryID] = " & Me.cmbCountry & " AND "
End If
' Check for Colors in multiselect list
For Each varItem In Me.lstFavColor.ItemsSelected
varColor = varColor & "[FavColor] = """ & _
Me.lstFavColor.ItemData(varItem) & """ OR "
Next
' Test to see if we have subfilter for colors...
If IsNull(varColor) Then
' do nothing
Else
' strip off last "OR" in the filter
If Right(varColor, 4) = " OR " Then
varColor = Left(varColor, Len(varColor) - 4)
End If
' Add some parentheses around the subfilter
varWhere = varWhere & "( " & varColor & " )"
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function