473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search multiple fields simultaneously for values within a certain range

13 New Member
Hi there, I'm returning to this forum for the second time and I would like to say thanks for the great help provided. I've encountered a new problem now and hope that you will be able to help me again.

I'm currently implementing a container monitoring system in MSAccess07 and I'm using a multiple search function to sort containers. I use several unbound text boxes and a list box.The user type's in partial search criteria in one or several text boxes. Or if they don't key in anything it returns all records. The matches are returned in a listbox.

I will show the code for 1 text box just to show how it works.

This is the code for the search button:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdShowCont_Click()
  2. Dim MySQL As String, mycriteria As String, MyRecordSource As String
  3. Dim argcount As Integer
  4. Dim Tmp As Variant
  5. mycriteria = " "
  6. argcount = 0
  7. MySQL = "SELECT * FROM qryContLife  WHERE "
  8.  
  9. Addwtf [textbox], "[Arrival Date]", mycriteria, argcount, "textbox"
  10.  
  11. Debug.Print mycriteria
  12. If mycriteria = " " Then
  13. mycriteria = "True"
  14. End If
  15.  
  16. MyRecordSource = MySQL & mycriteria
  17. Me![listbox].RowSource = MyRecordSource
  18. If Me![listbox].ListCount = 0 Then
  19. MsgBox " There are no containers with this criteria. Sorry ", 48
  20. Me!cmdClear.SetFocus
  21. Else
  22. Me![listbox].SetFocus
  23. End If
  24.  
  25. End Sub
  26.  
This is the code for the sub which builds the SQL statement and through which the values are returned to the listbox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Addwtf(fieldvalue As Variant, fieldname As String, mycriteria As String, argcount As Integer, fieldo As String)
  2. Dim wtf1 As String, wtf2, wtf3 As String
  3.  
  4.  If fieldvalue <> "" Then
  5.  If argcount > 0 Then
  6.      mycriteria = mycriteria & " and "
  7.  
  8.  End If
  9.  
  10.  Select Case fieldo
  11.  
  12. Case "textbox"
  13.        wtf2 = Mid$(fieldvalue, 4, 2) & "/" & Mid$(fieldvalue, 1, 2) & "/" & Mid$(fieldvalue, 7, 4)
  14.        mycriteria = (mycriteria & fieldname & " = " & Chr(35) & wtf2 & Chr(35))
  15.  
  16.   argcount = argcount + 1
  17.  End If
  18. End Sub
  19.  
The question is whether it is possible to make the search not for a specific date like the "Arrival Date" in this case (e.g. 01/12/2008) but for a specified range of dates like all values between 01/12/2008 and 01/01/2009. So that the code returns not just one value but all values in a specified range.

I hope I explained everything in a clear matter, if not please ask questions and I will try and make it more clear.

Thank you for spending your time of somebody else's problems.
Best regards, Eugene.
Jul 8 '08 #1
10 3972
ADezii
8,834 Recognized Expert Expert
To search for Records falling within a specified Date Range, use the BETWEEN operator making sure to delimit the Date Fields with the '#' qualifier. Assuming entry Fields of [txtBeginDate] and [txtEndDate], the general logic would be:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim MyRS As DAO.Recordset
  3. Dim strSQL As String
  4.  
  5. Set MyDB = CurrentDb()
  6.  
  7. If IsDate(Me![txtBeginDate]) And IsDate(Me![txtEndDate]) Then
  8.   If CDate(Me![txtBeginDate]) < CDate(Me![txtEndDate]) Then
  9.     strSQL = "Select * From tblTest Where [BirthDate] Between #" & Me![txtBeginDate] & _
  10.              "# And #" & Me![txtEndDate] & "# Order By [BirthDate];"
  11.     Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  12.       If Not MyRS.BOF And Not MyRS.EOF Then     'valid Records
  13.         Do While Not MyRS.EOF
  14.           Debug.Print MyRS![LastName] & " ==> " & MyRS![BirthDate]  'Test Loop
  15.          MyRS.MoveNext
  16.         Loop
  17.       Else
  18.         MsgBox "No Records meet your criteria", vbExclamation, "No Records"
  19.       End If
  20.   Else
  21.     MsgBox "End Date must be > Begin Date", vbExclamation, "Invalid Date"
  22.       Exit Sub
  23.   End If
  24. Else
  25.   'simply fall through, don't process
  26.   Exit Sub
  27. End If
  28.  
  29. MyRS.Close
  30. Set MyRS = Nothing
P.S. - Any questions, feel free to ask.
Jul 8 '08 #2
Eugenio
13 New Member
Thanks for your attention. Right, is it possible to incorporate the example u gave me into the existing code? I mean so that I would be able to make searches using various criteria e.g. sort the data by the date range between 01/01/2008 and 01/02/2008 and some other piece of data like container size for instance.

I really hope I'm being clear. Unfortunately I don't have a chance to test ur example right now, but I'm going to do that first thing in the morning and inform u of the results.

thank u again ;)
Jul 8 '08 #3
ADezii
8,834 Recognized Expert Expert
Thanks for your attention. Right, is it possible to incorporate the example u gave me into the existing code? I mean so that I would be able to make searches using various criteria e.g. sort the data by the date range between 01/01/2008 and 01/02/2008 and some other piece of data like container size for instance.

I really hope I'm being clear. Unfortunately I don't have a chance to test ur example right now, but I'm going to do that first thing in the morning and inform u of the results.

thank u again ;)
Code has been tested, and seems to work quite well, given the existing parameters.
Jul 8 '08 #4
Eugenio
13 New Member
ADezii, I never used the Recordset function so I don't fully understand how the code works. In your example you have a table with birthdates and a form with 2 text boxes, this part I get, but this one scares me:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  3.  
  4.  If Not MyRS.BOF And Not MyRS.EOF Then     'valid Records
  5.           Do While Not MyRS.EOF
  6.           Debug.Print MyRS![LastName] & " ==> " & MyRS![BirthDate]  'Test Loop
  7.           MyRS.MoveNext
  8.           Loop
  9.  
  10. Else
  11.  
  12. MsgBox "No Records meet your criteria", vbExclamation, "No Records"
  13.  
  14. End If
  15.  
The 5th line gives and error in the debugger, and how exactly it shows the retrieved data? does it open a new window or it requires a list box etc? And also where does the [Lastname] come from ?
Jul 9 '08 #5
NeoPa
32,579 Recognized Expert Moderator MVP
Debug.Print shows data in the Immediate Pane. Use Alt-F11 to switch to the debugger from your Access window, then Ctrl-G to open, and switch to, the Immediate Pane. Your original post includes similar code so one would have thought this was familiar to you.

MyRS![LastName] is a reference to a field (called LastName) from the recordset provided by the SQL in strSQL (See line #1).
If you have no such field then you need to adjust the code to match what you are working with.
Jul 9 '08 #6
ADezii
8,834 Recognized Expert Expert
Sorry Eugenio, but based on your original code, I made the false assumption that that you would fully comprehend the logic which I have posted. I completely apologize for this oversight. NeoPa provided the necessary explanations of my code in Post #6, and as far as a Recordset goes, in your case, it is a virtual representation of the SQL Statement represented by strSQL.

P.S. - Thanks NeoPa.
Jul 9 '08 #7
Eugenio
13 New Member
thanks guys, i will be working on this and will post if I encounter any difficulties. Sorry for the confusion I caused. Thank you.
Jul 9 '08 #8
Eugenio
13 New Member
right, i've managed to create a search for a sepcific date range using your example and it looks like this:
Expand|Select|Wrap|Line Numbers
  1. Dim MyRecordSource As String
  2.       Dim strSQL As String
  3.  
  4.  
  5.         If IsDate(Me![txtBeginDate]) And IsDate(Me![txtEndDate]) Then
  6.         If CDate(Me![txtBeginDate]) < CDate(Me![txtEndDate]) Then
  7.         strSQL = "Select * From tblTest Where [BirthDate] Between #" & Me![txtBeginDate] & _
  8.                    "# And #" & Me![txtEndDate] & "# Order By [BirthDate];"
  9.  
  10.  
  11.  
  12.         End If
  13.         End If
  14.  
  15.         MyRecordSource = strSQL
  16.         Me![lstContlist].RowSource = MyRecordSource
  17.         If Me![lstContlist].ListCount = 0 Then
  18.         MsgBox " There are no containers with this criteria. Sorry ", 48
  19.         Me!cmdClear.SetFocus
  20.         Else
  21.         Me![lstContlist].SetFocus
  22.         End If
  23.  
It shows the found values in the listbox, but is it possible to incorporate it into the existing multiple search function:
Expand|Select|Wrap|Line Numbers
  1. Dim wtf1 As String, wtf2, wtf3 As String
  2.  
  3.  If fieldvalue <> "" Then
  4.  If argcount > 0 Then
  5.      mycriteria = mycriteria & " and "
  6.  
  7.  End If
  8.  
  9.  Select Case fieldo
  10.     Case "myd1"
  11.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  12.  
  13.     Case "myd2"
  14.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  15.  
  16.     Case "myd3"
  17.        wtf2 = Mid$(fieldvalue, 4, 2) & "/" & Mid$(fieldvalue, 1, 2) & "/" & Mid$(fieldvalue, 7, 4)
  18.        mycriteria = (mycriteria & fieldname & " = " & Chr(35) & wtf2 & Chr(35))
  19.  
  20.     Case "myd4"
  21.        wtf3 = Mid$(fieldvalue, 4, 2) & "/" & Mid$(fieldvalue, 1, 2) & "/" & Mid$(fieldvalue, 7, 4)
  22.        mycriteria = (mycriteria & fieldname & " <= " & Chr(35) & wtf3 & Chr(35))
  23.  
  24.     Case "myd5"
  25.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  26.  
  27.        Case Else
  28.  
  29.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & Chr(39))
  30.       End Select
  31.  
  32.   argcount = argcount + 1
  33.  End If
  34.  
and allow it to search for records with the date range given and a specific status for example. in other words allow multiple search criteria?

P.s. I'm sorry in advance because this may sound stupid, but I'm using access for less than a year and still have a lot to learn.
Jul 9 '08 #9
ADezii
8,834 Recognized Expert Expert
right, i've managed to create a search for a sepcific date range using your example and it looks like this:
Expand|Select|Wrap|Line Numbers
  1. Dim MyRecordSource As String
  2.       Dim strSQL As String
  3.  
  4.  
  5.         If IsDate(Me![txtBeginDate]) And IsDate(Me![txtEndDate]) Then
  6.         If CDate(Me![txtBeginDate]) < CDate(Me![txtEndDate]) Then
  7.         strSQL = "Select * From tblTest Where [BirthDate] Between #" & Me![txtBeginDate] & _
  8.                    "# And #" & Me![txtEndDate] & "# Order By [BirthDate];"
  9.  
  10.  
  11.  
  12.         End If
  13.         End If
  14.  
  15.         MyRecordSource = strSQL
  16.         Me![lstContlist].RowSource = MyRecordSource
  17.         If Me![lstContlist].ListCount = 0 Then
  18.         MsgBox " There are no containers with this criteria. Sorry ", 48
  19.         Me!cmdClear.SetFocus
  20.         Else
  21.         Me![lstContlist].SetFocus
  22.         End If
  23.  
It shows the found values in the listbox, but is it possible to incorporate it into the existing multiple search function:
Expand|Select|Wrap|Line Numbers
  1. Dim wtf1 As String, wtf2, wtf3 As String
  2.  
  3.  If fieldvalue <> "" Then
  4.  If argcount > 0 Then
  5.      mycriteria = mycriteria & " and "
  6.  
  7.  End If
  8.  
  9.  Select Case fieldo
  10.     Case "myd1"
  11.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  12.  
  13.     Case "myd2"
  14.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  15.  
  16.     Case "myd3"
  17.        wtf2 = Mid$(fieldvalue, 4, 2) & "/" & Mid$(fieldvalue, 1, 2) & "/" & Mid$(fieldvalue, 7, 4)
  18.        mycriteria = (mycriteria & fieldname & " = " & Chr(35) & wtf2 & Chr(35))
  19.  
  20.     Case "myd4"
  21.        wtf3 = Mid$(fieldvalue, 4, 2) & "/" & Mid$(fieldvalue, 1, 2) & "/" & Mid$(fieldvalue, 7, 4)
  22.        mycriteria = (mycriteria & fieldname & " <= " & Chr(35) & wtf3 & Chr(35))
  23.  
  24.     Case "myd5"
  25.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & "*" & Chr(39))
  26.  
  27.        Case Else
  28.  
  29.        mycriteria = (mycriteria & fieldname & " Like " & Chr(39) & fieldvalue & Chr(39))
  30.       End Select
  31.  
  32.   argcount = argcount + 1
  33.  End If
  34.  
and allow it to search for records with the date range given and a specific status for example. in other words allow multiple search criteria?

P.s. I'm sorry in advance because this may sound stupid, but I'm using access for less than a year and still have a lot to learn.
Here is where it would really become complicated.
  1. You could use the following as your base Criteria String:
    Expand|Select|Wrap|Line Numbers
    1. strSQL = "Select * From tblTest Where [BirthDate] Between #" & Me![txtBeginDate] &  "# And #" & Me![txtEndDate] & "# "
  2. then build on the strSQL, as in:
    Expand|Select|Wrap|Line Numbers
    1. strSQL = strSQL & "AND <specify other criteria here> AND <specify other criteria here> "
  3. Tack on a final Order By Clause, if needed:
    Expand|Select|Wrap|Line Numbers
    1. strSQL = strSQL & "Order By [BirthDate];"
  4. There are other Experts/Moderators who are much better than I in the SQL lingo, hopefully they will join the discussion, and provide you with a better response.
Jul 9 '08 #10

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

Similar topics

16
3073
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a certificate at the end of the year. I have the code working fine, except for the fact that when I want to restrict the entries to awards between certain dates, even though I can use the restriction in the query that shows the actual records, when the...
32
14903
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
5
5057
by: vonclausowitz | last post by:
Repost from an VB group. Hi All, I'm looking for a way to search for multiple words in a database. There is however one but. The words have to be within a certain range of each other. For example I would like to find records which contain the word MALE and within for example 10 words of it the word BROWN.
23
9075
blyxx86
by: blyxx86 | last post by:
I am trying to filter multiple entries with only one search box. That way the user can type whatever they want and find all the values that contain what they enter. Private Sub txtLookup_AfterUpdate() Me.Filter = " Like '*" & & "*'" OR " Like '*" & & "*'" Me.FilterOn = True End Sub It allows for variables and to filter within the form. I can't seem to find a way to make it actually search through multiple fields (Serial,...
2
3674
by: Kosmos | last post by:
Alright so I've got this Outlook code written in VBA in Access. The first part, which works, records information about appointment times based on the required days before notification of certain contracts and then it adds them to the outlook calendar of the current user. This code works and is nested within a bunch of if statements because it only needs to trap certain appointments. The table I create with this code is later used to attempt to...
1
1883
by: Rama Jayapal | last post by:
hi i am developing a web application where i have to read multiple XML feeds amd store their values to database but i require the same type of fields from multiple XML feeds like for example
14
2140
by: Thurston Manson | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference between C89 and C99, I would like to know. I have tried with different compilers, and I see some differences. Before I file a bug report with my C vendor, I would like to know what the correct behavior is. struct S { unsigned a:4;
8
1141
by: Miro | last post by:
Hi sorry for the quick question. I am on the right track but just cant seem to find what to search for in my books / online for the answer I am looking for. I have created a dataset within my project. In the dataset I created 2 tables - and set some fields on the tables. In each table I set one of the fields to be a primary key / unique auto incremenet. So now I create a form, add a datagrid view to it and add to the dataset.
1
4745
by: annemariearmour | last post by:
I am using Crystal reports version 11.2 to create reports. The data source is SQL Server, and I am using views rather than reporting directly from tables. I apply selection criteria to the incoming records at record level and at group level. For example, I may select all bill lines with a receipt date within a certain date range. This results in a list of bill lines within that date range. The records are grouped and values on the records...
0
9673
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
10216
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...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
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
9044
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
7543
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
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.