473,288 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Search form multiple multi select list box's Error

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, 422 views)
Feb 25 '09 #1
6 4641
NeoPa
32,554 Expert Mod 16PB
I will only get involved as far as to suggest checking out Example Filtering on a Form.
Feb 25 '09 #2
ADezii
8,834 Expert 8TB
  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
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, 1630 views)
Attached Files
File Type: zip James sample databse .zip (62.4 KB, 165 views)
Feb 25 '09 #4
ADezii
8,834 Expert 8TB
@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
OldBirdman
675 512MB
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
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

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

Similar topics

4
by: JSMiami | last post by:
I have a form that is fed by a certain table. This table connects to an intermeddiate table and then a look up table. All of this is done to accomplish a many-to-many relationship. Imagine that the...
2
by: Sean | last post by:
Greetings all, I am attempting to make a form that will filter through several tables that (I believe) have refretial integrity. I am pulling data from several tables into the form and i would...
2
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can...
18
by: Alpha | last post by:
Hi, I'm working on a Windows applicaton with VS 2003 on windows 2000. I have a listbox that I have binded to a dataset table, "source" which has 3 columns. I would like to display 2 of those...
6
by: Dave | last post by:
On my form I have combo boxes. These combo boxes, after updating them, populate respective listboxes that are located below the combo boxes on the same form. I am trying to use a "generate...
1
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
36
by: aaronkmar | last post by:
Hello Bytes, I've been a long time lurker, there so much information here I always find my answers with ease. Until today... I'm hoping someone can exert a little brain power for me. I have...
11
by: woodey2002 | last post by:
This problem is driving me crazy. Hello there, i am trying to create a search form for records in my access database. The search form will contain text boxes and a multi select list box. The user...
2
by: woodey2002 | last post by:
Hi Guys and thanks for your time. I have a search form for my database that allows users to select multiple criteria from multi select list boxes. I successfully integrated a multi select...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.