473,503 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search/Filter Working but has some error

spideynok
14 New Member
Hi! I have a code..
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command18_Click()
  2. 'Update the record source
  3.     Me.SMS_ValidateSubform.Form.RecordSource = "SELECT * FROM SMS " & BuildFilter
  4.  
  5.     ' Requery the subform
  6.     Me.SMS_ValidateSubform.Requery
  7. End Sub
  8.  
and..

Expand|Select|Wrap|Line Numbers
  1. Private Function BuildFilter() As Variant
  2.     Dim varWhere As Variant
  3.     Dim varColor As Variant
  4.     Dim varItem As Variant
  5.     Dim intIndex As Integer
  6.  
  7.     varWhere = Null  ' Main filter
  8.  
  9.     ' Check for LIKE First Name
  10.     If Me.Text16 > "" Then
  11.         varWhere = varWhere & "[Card_Number] LIKE """ & Me.Text16 & "*"" AND "
  12.     End If
  13.  
  14.  
  15.     ' Check if there is a filter to return...
  16.     If IsNull(varWhere) Then
  17.         varWhere = ""
  18.     Else
  19.         varWhere = "WHERE " & varWhere
  20.  
  21.         ' strip off last "AND" in the filter
  22.         If Right(varWhere, 5) = " AND " Then
  23.             varWhere = Left(varWhere, Len(varWhere) - 5)
  24.         End If
  25.     End If
  26.  
  27.     BuildFilter = varWhere
  28.  
  29. End Function
  30.  
Its working on my first project. but when i use it on my new project which is the same with my first project. It has a problem regarding on filtering. When I search/filter on my new project, it only show the first record either I put a "Card_Number" on that or it is Null.

I can't see any error on my code. but I'm confuse about what's happening when I'm searching/filtering a record.

Thanks in advanced (~.^)olll
Mar 15 '12 #1
1 1789
twinnyfo
3,653 Recognized Expert Moderator Specialist
spideynok,

I may have found some problem areas in your filter string generation, but I will also make a few recommendations for you, which may sound picky, but may also help in the long run.

First, I would suggest using appropriate names for your controls, such as naming the command buttons according to what they do, and naming text boxes according to what data they contain. This helps you (and perhaps anyone coming after you) understand what any particular control is for. See my minor changes below....

Second, I'm not sure why you are declaring your function results and variables as Variants. This sets aside undue resources for essentially an "unknown" type of variable, then changes the data types once it determines what is going into that variable. Since filters are strings, set your variable types for the filter text as strings.

Also, "it appears" that this code is just looking at the card number on the form, then filtering the records by that card number (i.e. in a search box???) plus additional variables from the form? If this is the case, I have modified the code below based on that presumption. If this is not the case, please clarify, because you have your filter appending an additional "AND" that never needs to be there in the first place, then removing it again. However, I do notice several other variables listed, so I've also inferred some code to assist.

Your Filter was missing the "WHERE" required for the SELECT statement. See below for how this was added. It was also missing the final semicolon ";", which is required for every SELECT statement.

I think I've captured the essence of what you need below, but please clarify if I've missed the mark.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdFilterRecords_Click() 
  5.     'Update the record source
  6.     Dim strFilter as String  'I prefer to establish a string for this, then set its value based on the function, although it's not required, you get the same results
  7.     strFilter = BuildFilter
  8.     'Keep in mind that this will return ALL records if the form is blank....
  9.  
  10.     Me.SMS_ValidateSubform.Form.RecordSource = "SELECT * FROM SMS " & strfilter & ";"
  11.     ' Requery the subform -- this is not required to requery because you have changed the Record Source...
  12.     Me.SMS_ValidateSubform.Requery 
  13. End Sub
  14.  
  15. Private Function BuildFilter() As String
  16.     'I would remove all these variables, unless you somehow use them elsewhere in the code
  17.     Dim varWhere As String
  18.     Dim strColor As String 'as an example
  19.     Dim intItem As Integer 'as an example
  20.     Dim intIndex As Integer 
  21.  
  22.     BuildFilter = ""  ' Reset the Main filter--I don't like setting to Null 
  23.  
  24.     ' Check for LIKE First Name - card number must be TEXT, this assumes you only have the beginning characters of the card in the form.  This should probably use an "=" instead of "LIKE" and remove the "*"
  25.     If Me.txtCardNumber <> "" Then 
  26.         BuildFilter = " WHERE [Card_Number] LIKE '" & Me.txtCardNumber & "*'" 
  27.     End If 
  28.  
  29.      ' Check for LIKE Second Name -- MUST BE TEXT
  30.     If Me.txtColor <> "" Then
  31.         'This nested If makes sure we always have a WHERE in the statement or just adds to it
  32.         If BuildFilter = "" Then
  33.             BuildFilter = " WHERE [Color] = '" & Me.txtColor & "'" 
  34.         Else
  35.             BuildFilter = BuildFilter & " AND [Color] = '" & Me.txtColor & "'" 
  36.         End If
  37.     End If
  38.  
  39.      ' Check for LIKE Third Name -- for INTEGER variable types
  40.     If Me.txtItem <> "" Then 
  41.         If BuildFilter = "" Then
  42.             BuildFilter = " WHERE [Item] = " & Me.txtItem
  43.             'Notice no single quote in this expression 
  44.         Else
  45.             BuildFilter = BuildFilter & " AND [Item] = " & Me.txtItem 
  46.         End If
  47.     End If 
  48.  
  49.     'I would remove this entire section of code below....
  50.     ' Check if there is a filter to return...  
  51.     If IsNull(varWhere) Then  
  52.         varWhere = ""  
  53.     Else  
  54.         varWhere = "WHERE " & varWhere  
  55.  
  56.         ' strip off last "AND" in the filter  
  57.         If Right(varWhere, 5) = " AND " Then  
  58.             varWhere = Left(varWhere, Len(varWhere) - 5)  
  59.         End If  
  60.     End If  
  61. End Function
  62.  
I hope this helps. SELECT statements based on variables can be tricky, so it might be wise to cycle through the teesting, one variable at a time, and use Debug.Print to see what your Filter String looks like throughout the stages of its development until you achieve your desired results.

As always, glad to offer additional help if you hit snags.
Jul 26 '12 #2

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

Similar topics

0
1433
by: Dirk Hagemann | last post by:
Hi! I want to get the properties of all the computer-accounts of an ActiveDirectory structure (Microsoft). I know that could be done by using "Search Filter Syntax" with LDAP-Dialect or...
0
1721
by: jeffreycope | last post by:
Let's say I have a datagrid displaying data that's been filtered via a search page. Now I bring up a maintenance page and add a new record and this new record doesn't match the search / filter...
7
1677
by: Frankie | last post by:
I'm trying to run the following search query: $query = sprintf ("SELECT itemNumber, thumbnailURL, title, description, price FROM apparel,hats WHERE apparel.title OR apparel.description OR...
3
1434
by: tshad | last post by:
I have the following page I found on the net, but it doesn't work. I get an error on page. ****************************************************************** <html> <head> <SCRIPT...
5
2115
by: keithsimpson3973 | last post by:
Hi, I have an Access 2003 database I am running with vb 6. I have one report that I want to filter on the Start_Date and Stop_Date in a scheduling database. I would like to enter a stop and stop...
4
2157
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
2
1816
by: redbenn | last post by:
I am trying to set up a filter form that will filter data on another in a certain field. Right now I have a text box (Text13) and a Command Button. The Button code is: Private Sub...
0
1714
by: Mark112 | last post by:
Hi I am attempting to restore the search feature that was created by an external company using the indexing service for our intranet. the intranet is located locally at each of our offices. The...
1
1874
by: JpjVB | last post by:
I'm having difficulty filtering a form using a multiselect list box when using some Allen Browne code. I get the error "Syntax Error (missing operator)in query expression '( IN (""Sydney"London)'. -...
9
2471
by: metube | last post by:
Hi, I found this code for a search filter. But I need it adapted to a single criteria. Im not exactly an expert in SQL...but I was hoping someone here would be kind enough to help me out. ...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.