473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search Form Code

f430
43 New Member
hi, i have a master table with all my part information, and it has columns with part number, date, defects,...
and i am trying to write a code for my search form.
i have created a search form on Access, and it has 5 combo boxes for:
part number
defects
assembled by
and the other 2 combo boxes are for "from" and "to" date range. i have made the combo boxes for the date range to display a calendar when clicked and choose a date.

i have been trying to write a code that only requires one of these fields to be filled out and it can filter my master table.
i would appreciate any help on this
Thanks
Aug 9 '10
61 3733
NeoPa
32,569 Recognized Expert Moderator MVP
f430: i am not really sure what you mean by explicitly formatting these two boxes.
I suggest you look at the article I linked you to in post #8 then. It is explained fully there.
f430: i was also wondering what kind of code i should use before the where statement.
That would be a standard SQL query string. If this is something you're not familiar with then we may need a crash course on the very basics.

Queries in Access are actually QueryDef objects. These can be saved as you know, and run, and edited etc.

At the heart of every QueryDef there is the fundamental query instructions. These are written in SQL code and can be found, when editing a QueryDef, by selecting SQL View from the View menu. Such SQL code can form the basis of a QueryDef object, but there are also ways of executing SQL code from a simple string variable (or literal) from within VBA.

To learn more about what SQL can do for you look in the Help system (Finding Jet SQL Help).

The (almost) most basic form of a SQL instruction would be to display the contents of a table and goes like :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM   [TableName]
The SQL of a QueryDef is updatable. This can prove important.
Aug 10 '10 #11
NeoPa
32,569 Recognized Expert Moderator MVP
Having seen your code some things become clearer to me.

Firstly, you are working on a filter string. Filter strings are essentially the WHERE clause of a full SQL statement, but without the word "WHERE ". The SELECT clause is equally unnecessary when dealing with filters (By the way, SELECT ... was just my use of the ellipsis (...) to indicate whatever you had before here. I wasn't suggesting the literal use of the dots).

You still have an underline character (_) on your line #13 which is an error (See post #8).

When building a filter string, it is advisable (generally) to start from scratch unless you know what you can expect it to contain already. It appears that your code would go around and around making the filter string longer and longer re-applying filtering on the same set of fields. Not good for you.
Aug 10 '10 #12
f430
43 New Member
NeoPa, i appreciate all the help.
i apologize for not keeping up with you, but my skills in access/vba are very basic. i am having some problems building a query string, would you be able to provide more information about building a standard sql string.

i fixed the minor errors and the here is what it looks like
Expand|Select|Wrap|Line Numbers
  1. If Me!productnum > "" Then _
  2.      strFilter = strFilter & _
  3.      " AND ([Part Number]=" & _
  4.      Me!productnum & ")"
  5.  
  6.  
  7. strFilter = "If Me!defect > "" Then strFilter = strFilter & " And ([Defectcode] = " & Me!Defect & ") & "WHERE '" & Me.Defect & "' In([Defect Code 1], [Defect Code 2], [Defect Code 3])"
  8.  
  9.  
  10. strFilter = "If Me!associateid > "" Then strFilter = strFilter & " And ([associates] = " & Me!associateid & ") & "WHERE '" & Me.associateid & "' In([associate], [associate 2])"
Thanks
Aug 10 '10 #13
NeoPa
32,569 Recognized Expert Moderator MVP
I must rush for now, but try looking more carefully at the code in the article linked in post #2. I can go into more detail later, but trust me, your understanding will benefit from a bit more looking at it. Even if you still need further explanation, at least when that comes you'll be able to put what you read into context. Those "aaaaaah" moments are really quite precious. I'd hate you to miss out ;)

PS. I know it probably all looks like a foreign language at the moment, but I expect it will become clearer soon.
Aug 10 '10 #14
f430
43 New Member
my date1 and date2 comboboxes are formatted as short dates in the properties of the combo box, so i guess they are explicitly formatted, and the code im trying to use is :
Expand|Select|Wrap|Line Numbers
  1. strFilter = "SELECT [CreationDate]" & "FROM Master Table" & "WHERE [CreationDate] Between #date1# And #date2#"
however, i am a little unsure about the codes on line 7 and 10 above, because i have not really defined what "defectcode " and "associates " are. and i keep getting them highlighted when i debug
Aug 11 '10 #15
NeoPa
32,569 Recognized Expert Moderator MVP
f430: my date1 and date2 comboboxes are formatted as short dates in the properties of the combo box, so i guess they are explicitly formatted, and the code im trying to use is :
Expand|Select|Wrap|Line Numbers
  1. strFilter = "SELECT [CreationDate]" & "FROM Master Table" & "WHERE [CreationDate] Between #date1# And #date2#"
Absolutely not! That is not only implicitly formatted but it will certainly start to cause problems if you ever try to run it on a machine set up other than for North America, which almost coincidentally has a short date format similar to the SQL standard. I can't see how you could say that if you'd read the article I linked you to.
f430: however, i am a little unsure about the codes on line 7 and 10 above, because i have not really defined what "defectcode " and "associates " are. and i keep getting them highlighted when i debug
This sounds like a joke. Why would you think it could possibly work if you have references to non-existent items?

Have you even looked at the articles I linked you to? There is no indication of it in your comments. I feel like a man trying to help someone drowning but every time I throw the rope in the water you throw it back. You want to be rescued by a boat.

I'm happy to continue trying if you are, but we need to get past asking questions that are this obvious. References to non-existent items are going to cause a problem. Don't ask me to save you from that. Create the items before doing your test. Dates need to be formatted explicitly for use within SQL. This is a SQL standard. You can write code that doesn't do this. It will probably work for you if North America is your whole world, but it is not what I suggest for reasons of portability and the difficulty of finding the problem when it does happen.
Aug 11 '10 #16
NeoPa
32,569 Recognized Expert Moderator MVP
Try this. It depends on the items being correctly named and, above all, existing, but the basic format should be about right :
Expand|Select|Wrap|Line Numbers
  1. strFilter = ""
  2. If Not IsNull(Me.ProductNum) Then _
  3.     strFilter = strFilter & " AND " & _
  4.                 "([Part Number]=" & Me.ProductNum & ")"
  5.  
  6. If Not IsNull(Me.Defect) Then _
  7.     strFilter = strFilter & " AND " & _
  8.                 "(" & Me.Defect & " In([Defect Code 1]," & _
  9.                                       "[Defect Code 2]," & _
  10.                                       "[Defect Code 3])"
  11.  
  12. If Not IsNull(Me.AssociateID) Then _
  13.     strFilter = strFilter & " AND " & _
  14.                 "(" & Me.AssociateID & " In([Associate]," & _
  15.                                            "[Associate 2])"
  16.  
  17. If strFilter > "" Then strFilter = Mid(strFilter, 6)
  18. Me.Filter = strFilter
  19. Me.FilterOn = (strFilter > "")
PS. It assumes that Me.ProductNum, Me.Defect and Me.AssociateID are numeric (as indicated by your code). If this is not the case then we need to know.
Aug 11 '10 #17
f430
43 New Member
i did read your articles, i just didn't know how to define these variables since they were not in any of my fields in my master table. i apologize for the trouble.
i did make some modifications to my code, because some were text fields and i accidentally put in a code for a number. i have also changed the date code to something hopefully better.
however i still wasn't able to get it to work.
here is my code for your review:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdfindrecords_Click()
  2.    Dim strFilter As String, strOldFilter As String
  3.  
  4.    strOldFilter = Me.Filter
  5.    strFilter = "1=1 "
  6.  
  7.    If Me!productnum & "" > "" Then
  8.       strFilter = strFilter & " AND [Part Number]=" & Me!productnum & " "
  9.    End If
  10.  
  11.    If Me!Defect & "" > "" Then
  12.       strFilter = strFilter & " And """ & Me!Defect & _
  13.          """ In([Defect Code 1], [Defect Code 2], [Defect Code 3]) "
  14.    End If
  15.  
  16.    If Me!associateid & "" > "" Then
  17.       strFilter = strFilter & " And """ & Me!associateid & _
  18.          """ In([Associate], [Associate 2]) "
  19.     End If
  20.  
  21.     If Not IsNull(Me.date1) Then
  22.       strFilter = strFilter & " AND CreationDate >=#" & Format(Me!date1, "m/d/yyyy") & "# "
  23.    End If
  24.    If Not IsNull(Me.date2) Then
  25.       strFilter = strFilter & " AND CreationDate <=#" & Format(Me!date2, "m/d/yyyy") & "# "
  26.    End If
  27.  
  28.    If strFilter <> "1=1 " Then
  29.       Me.Filter = strFilter
  30.       Me.FilterOn = True
  31.     Else
  32.       Me.Filter = strOldFilter
  33.    End If
  34.  
  35. Debug.Print strFilter
  36. End Sub 
now whenever i try to search using the part number or the date range, nothing really happens.
but if i use the defect or the associate number, i get a message that asks me to enter a parameter value for defect code 3 and associate 1,2.
i tried debugging and only when the filter runs through the code below, that the search form asks me to enter the parameter values
Expand|Select|Wrap|Line Numbers
  1.  Me.Filter = strFilter
Thanks
Aug 11 '10 #18
NeoPa
32,569 Recognized Expert Moderator MVP
It's hard to work with you because you don't respond to what I post. You just keep starting again from where you are and I can never tell what you're talking about or where you're at in your head. I never know how much of what I've already said I need to repeat. Basically, it's very frustrating trying to communicate with someone who doesn't respond to you.

I get the feeling that you're doing your best, but you don't seem to understand simple communication.

Your latest code is wrong in so many ways. I do not wish to spend five minutes on every point as that will take up so much of my time. I spent time earlier providing a good basic set of code that would work for you assuming all you'd told me so far was reliable, but instead of going from there and explaining what is still not working, you ignore that and go back to your code which is so full of holes I can't even comment on them all.

Please stop ignoring what I post. It makes me feel like I'm wasting my time. I don't post it without reason. This can help you a long way forward if you'll let it. It won't if you ignore it though.

PS.
NeoPa: PS. It assumes that Me.ProductNum, Me.Defect and Me.AssociateID are numeric (as indicated by your code). If this is not the case then we need to know.
Posting a whole set of different and non-workable code is not letting me know.
Aug 11 '10 #19
NeoPa
32,569 Recognized Expert Moderator MVP
As I now have some information about another bit of the filtering I can add that into my suggested code :
Expand|Select|Wrap|Line Numbers
  1. strFilter = ""
  2. If Not IsNull(Me.ProductNum) Then _
  3.     strFilter = strFilter & " AND " & _
  4.                 "([Part Number]=" & Me.ProductNum & ")"
  5.  
  6. If Not IsNull(Me.Defect) Then _
  7.     strFilter = strFilter & " AND " & _
  8.                 "('" & Me.Defect & "' In([Defect Code 1]," & _
  9.                                         "[Defect Code 2]," & _
  10.                                         "[Defect Code 3])"
  11.  
  12. If Not IsNull(Me.AssociateID) Then _
  13.     strFilter = strFilter & " AND " & _
  14.                 "('" & Me.AssociateID & "' In([Associate]," & _
  15.                                              "[Associate 2])"
  16.  
  17. If Not IsNull(Me.Date1) Then
  18.     strFilter = strFilter & " AND ([CreationDate]"
  19.     If Not IsNull(Me.Date2) Then
  20.         strFilter = strFilter & _
  21.                     " Between " & Format(Me.Date1, "\#m/d/yyyy\#") & _
  22.                     " And " & Format(Me.Date2, "\#m/d/yyyy\#") & ")"
  23.     Else
  24.         strFilter = strFilter & _
  25.                     "=" & Format(Me.Date1, "\#m/d/yyyy\#") & ")"
  26.     End If
  27. End If
  28.  
  29. If strFilter > "" Then strFilter = Mid(strFilter, 6)
  30. Me.Filter = strFilter
  31. Me.FilterOn = (strFilter > "")
I have changed my assumption about the Defect Code and Associate ID fields from looking at your latest code posted. I'm guessing you have some reason for doing it that way. Again. I'm only guessing as the information I'm working with is so poor. Please let me know if what the actual types of the fields are. At least I can be reasonably sure that [CreationDate] is DateTime.

If you want help on how to deal with literal strings in VBA and SQL check out Quotes (') and Double-Quotes (") - Where and When to use them.
Aug 11 '10 #20

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

Similar topics

7
3177
by: jim Bob | last post by:
Hi, This is probably very simple to do so if anyone can point me to the right place for reading, it would be much appreciated. I just want to build a very basic search form where i can enter a name or part of a name into a text box, press a button, and the entered value gets inserted into a sql query and the results of the query gets displayed into a list or text box. (ie take the input from the text box and plug that variable in my...
3
2213
by: Sheau Wei | last post by:
This is the search engine code that i create, but it was error and didnt come out the result. Cn u help me to check what wrong with my code? Thanks <Table cellspacing=1 cellPadding=1 align=center> <td colspan=4> <h2 align=center>Bomba inventory searching system</h2> <tr> <td> <h4 Balai Bomba Inventory Searching System</H4> <form name="search" method="post" action="<?=$PHP_SELF?>">
31
2937
by: DWolff | last post by:
I'm somewhat new to Access, and have no VB experience at all (except for modifying existing code where obvious). I built a data entry form that works fine. Typically, a client will call in and the user has to find his record to add or modify existing data. Originally, I built the form based on a query with an input criteria for the last name and first name. The Last_Name critera is Like &"*" so that partial names will work. The...
9
16094
by: lightning | last post by:
Hi all, I'm not very conversant with the vocabulary of Access, so please ask for clarification if necessary... I am trying to build a search form that allows combinations of search terms. For example, let's say I have three controls on my form; year, keyword, location. Entering a valid value for all three gives expected results from the dataset. However, I'd like to interpret a blank field in the search form as ignoring that constraint....
6
2426
by: KiwiGenie | last post by:
Hi..I am trying to make a search form. I am fairly new to access and could well be looking at it completely wrong. I have an unbound form with textboxes in the header for entering different search criteria. I have a subform for displaying the results, which is bound to Query4. SQL for Query4 (taken from sql view in query): SELECT tblRecipes.RecipeName, tblRecipes.FoodCategory, Sum(Query3.IngredCost) AS SumOfIngredCost, Query3.RecipeID FROM...
2
2264
by: Mark | last post by:
Hi All, I am creating a music database for a friend and have run into a problem. Within the ALBUM table, I wanted to store the ARTIST_ID rather than the ARTIST_NAME. To do this, I intended to have have a command button on the album form which would open a search form (based on the artist table). This works as I wanted and allows me to get to one record. I then planned to have a button on the search form which when clicked, would updated...
2
2745
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 listbox for users to select and search for counties. On the same page however I would like to integrate a similar multiselect box for nationality. I would like the user to be able to search for nationality with county or individually. After...
6
4666
by: woodey2002 | last post by:
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...
6
3570
by: mercout | last post by:
Hey, I've been trying to create a search form in access for a while now, searching through books and emails. I have the search form set up with 11 combo box's, 3 text box's, a view button, and a clear button. Once the user enters certain fields, all of the boxes do not have to be filled and clicks the view button, a report will pop up with detail of what was found.I found this code...
8
4526
by: munkee | last post by:
Hi everyone, I am using the following code adapted from Allen Browne: 'Purpose: This module illustrates how to create a search form, _ where the user can enter as many or few criteria as they wish, _ and results are shown one per line. 'Note: Only records matching ALL of the criteria are returned. 'Author: Allen Browne (allen@allenbrowne.com), June 2006. Option Compare Database
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.