By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,107 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,107 IT Pros & Developers. It's quick & easy.

Search form multiple multi select list box's Error

P: 15
Hi Everyone. Thanks for your time.

I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option for 2 list boxes:- county and nationality, while trying to add a third multi select list box for qualifications search is where i encounter my problem.

I've copied the working code from my working list boxes, however it cant seem to pick up the search value from the qualification list box.

I think this error may stem from the relationships but i am not sure.

This is the record source property of one of my working list boxes:

lstNationality :SELECT [tblNationality].[NationalityCode], [tblNationality].[Nationality] FROM tblNationality;

This is the record source property of my new troublesom qualification list box:

SELECT [tblQualifications].[QualCode], [tblQualifications].[ShortQual] FROM tblQualifications ORDER BY [ShortQual];

Am i correct in asuming that it's a relationship issue?

I have attached a screnshot of my relationships, and included a copy of my code in this post, if any body has any ideas they would be greatly appericated.
Many thanks once again for your time Kind regards, JAMES

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnSearch_Click()
  2.  
  3.     ' Update the record source
  4.     'Me.sbfrmSearchResults1.Form.RecordSource = "SELECT * FROM qryNew " & BuildFilter
  5.  
  6.     ' Update the record source
  7.     If BuildFilter = "" Then
  8.         Me.sbfrmSearchResults1.Form.RecordSource = "SELECT * FROM qryNew " & BuildFilter
  9.     Else
  10.         Me.sbfrmSearchResults1.Form.RecordSource = "SELECT * FROM qryNew WHERE " & BuildFilter
  11.     End If
  12.  
  13.     'Requery the subform
  14.     Me.sbfrmSearchResults1.Requery
  15.  
  16.  
  17. End Sub
  18.  
  19.  
  20. Private Function BuildFilter() As Variant
  21.  
  22.     Dim varWhere As Variant
  23.     Dim varItem As Variant
  24.     Dim intIndex As Integer
  25.     Dim CountyCode As Variant
  26.     Dim NationalityCode As Variant
  27.     Dim qualCode As Variant
  28.  
  29.     varWhere = Null  ' Main filter
  30.     CountyCode = Null  ' Subfilter used for CountyCode
  31.     NationalityCode = Null ' Subfilter used for NationalityCode
  32.     qualCode = Null ' Subfilter used for qualCode
  33.  
  34.  
  35.     ' Check for LIKE First Name
  36.     If Me.txtFirstName > "" Then
  37.         varWhere = varWhere & "[FirstName] LIKE """ & Me.txtFirstName & "*"" AND "
  38.     End If
  39.  
  40.     ' Check for LIKE Last Name
  41.     If Me.txtSurname > "" Then
  42.         varWhere = varWhere & "[surname] LIKE """ & Me.txtSurname & "*"" AND "
  43.     End If
  44.      ' Check for LIKE regNumber
  45.     If Me.txtRegNumber > "" Then
  46.         varWhere = varWhere & "[regnumber] like """ & Me.txtRegNumber & """  And "
  47.    End If
  48.  
  49.  
  50.  
  51.     ' Check for counties in multiselect list
  52.     For Each varItem In Me.lstCountyCode.ItemsSelected
  53.         CountyCode = CountyCode & " [tblMemberDetails_CountyCode] = """ & _
  54.                     Me.lstCountyCode.ItemData(varItem) & """ OR "
  55.          Next
  56.  
  57.     'Test to see if we have subfilter for colors...
  58.     If IsNull(CountyCode) Then
  59.         ' do nothing
  60.     Else
  61.         ' strip off last "OR" in the filter
  62.         If Right(CountyCode, 4) = " OR " Then
  63.             CountyCode = Left(CountyCode, Len(CountyCode) - 4)
  64.         End If
  65.  
  66.         'Add some parentheses around the subfilter
  67.         varWhere = varWhere & "( " & CountyCode & " ) AND "
  68.     End If
  69.  
  70.  
  71.   'NationalityCode
  72.  
  73.         ' Check for Nationality in multiselect list
  74.     For Each varItem In Me.lstNationality.ItemsSelected
  75.         NationalityCode = NationalityCode & " [tblmemberdetails.NationalityCode] = """ & _
  76.                     Me.lstNationality.ItemData(varItem) & """ OR "
  77.  
  78.     Next
  79.  
  80.     'Test to see if we have subfilter for colors...
  81.     If IsNull(NationalityCode) Then
  82.         ' do nothing
  83.     Else
  84.         ' strip off last "OR" in the filter
  85.         If Right(NationalityCode, 4) = " OR " Then
  86.             NationalityCode = Left(NationalityCode, Len(NationalityCode) - 4)
  87.         End If
  88.  
  89.         'Add some parentheses around the subfilter
  90.         varWhere = varWhere & "( " & NationalityCode & " ) AND "
  91.     End If
  92.  
  93.  
  94.       'qualCode
  95.  
  96.         ' Check for qualCode in multiselect list
  97.     For Each varItem In Me.lstqual1.ItemsSelected
  98.         qualCode = qualCode & " [tblqualifications_qualCode] = """ & _
  99.                     Me.lstqual1.ItemData(varItem) & """ OR "
  100.  
  101.     Next
  102.  
  103.     'Test to see if we have subfilter for colors...
  104.     If IsNull(qualCode) Then
  105.         ' do nothing
  106.     Else
  107.         ' strip off last "OR" in the filter
  108.         If Right(qualCode, 4) = " OR " Then
  109.             qualCode = Left(qualCode, Len(qualCode) - 4)
  110.         End If
  111.  
  112.         'Add some parentheses around the subfilter
  113.         varWhere = varWhere & "( " & qualCode & " )  "
  114.     End If
  115.  
  116.  
  117.  
  118.      'Check if there is a filter to return...
  119.     If IsNull(varWhere) Then
  120.         varWhere = "''"
  121.     Else
  122.  
  123.         ' strip off last "AND" in the filter
  124.         If Right(varWhere, 5) = " AND " Then
  125.             varWhere = Left(varWhere, Len(varWhere) - 5)
  126.         End If
  127.  
  128.     End If
  129.  
  130.  
  131.     BuildFilter = varWhere
  132.  
  133.     End Function
  134.  
Thanks again.
Attached Images
File Type: jpg Relationship screenshot.jpg (8.7 KB, 373 views)
Feb 25 '09 #1
Share this Question
Share on Google+
6 Replies


NeoPa
Expert Mod 15k+
P: 31,419
I will only get involved as far as to suggest checking out Example Filtering on a Form.
Feb 25 '09 #2

ADezii
Expert 5K+
P: 8,623
  1. Could it be as smple as a Syntax Error, I am referring to Code Line #98
    Expand|Select|Wrap|Line Numbers
    1. 'Check for qualCode in multiselect list
    2. For Each varItem In Me.lstqual1.ItemsSelected
    3.   qualCode = qualCode & " [tblqualifications].[qualCode] = """ & _
    4.              Me.lstqual1.ItemData(varItem) & """ OR "
    5. Next
  2. You are treating the [qualCode] Field as if it were a String, is this Field, in fact, a String?
Feb 25 '09 #3

P: 15
Thanks for the reply hugely appreciated.

I have tried your suggestion and its giving me a runtime error
"data type mismatch in criteria expression"

If i try [tblqualifications]_[qualCode] i get a syntax error in query expression
'( [tblqualifications]_[qualCode] = "3") this error seems somewhat correct because its recognising the qualCode from the listbox.

In answer to your question i am confident that its a string because the two working search options county and nationality seem to work following this principle.

Its strange because my code works for county search with
[tblMemberDetails_CountyCode] where as my code for nationality search has to be [tblNationality.nationalityCode].

I dont know why this is but i am assuming that this might be giving me issues with qualification searching via the multi select list box.

having reviewed my database i discovered that the fields for my successful searches are stored in tblMemberDetails where as the field for qualification is located in another table. i think i need to reference tblMemberQualifications.qualCode as opposed to tblQualifications.qualCode but yet again i am not sure.

I have attached a sample version of my database, if anybody could have a look at it i would greatly appericate it as this is driving Me CRAZY.

Thanks again for all the support.

Regards James.
Attached Images
File Type: jpg james database relationship screenshot.jpg (12.5 KB, 1580 views)
Attached Files
File Type: zip James sample databse .zip (62.4 KB, 120 views)
Feb 25 '09 #4

ADezii
Expert 5K+
P: 8,623
@woodey2002
Right now, I'm on vacation and working with an older Version of Access (2000), which is essentially useless in this case. When I return home on Friday, and if this Thread has not yet been resolved, I'll have a good look at it and get back to you during the weekend.
Feb 26 '09 #5

100+
P: 675
If line 12 were Debug.Print Me.sbfrmSearchResults1.Form.RecordSource, you would have the actual SQL string used. If you paste that into the SQL window in query design, then switch to DataSheet view, what happens? Switching to Design view and looking at the grid, are there any obvious errors? Does removing some of the criteria get the results it should? This is one quick way to narrow the problem down, and you should quickly find the error.

If not, using the Design Query, design a new query which exactly mimics your problem. Enter the criteria that you are using in the various controls on your form. Switch to DataSheet view to be sure you got it right. Switch to SQL view and compare the generated SQL statement to the Debug.Print you inserted in line 12.
Feb 26 '09 #6

P: 15
Hi
thanks for the responce.

I figured is out it was finally with some advice from my friend.

qualCode a numeric value in the table (the data type) so, i don't want quotes around the value.

Thanks so much for all the assistance.

All the best and thanks again.

Regards

JAMES.
Feb 26 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.