473,385 Members | 1,449 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,385 software developers and data experts.

Search method for a form

I have a search form that searches for the criteria selected. I'm want to get the search method to look into multiple textboxes instead of just one. Please help I'm fairly new to Access/VBA. My code is the following but it does not work when a certain tool which is in [tool2] or {tool3] etc. It just seems like it only looks at [tool1] and if it does not find it there it just stops there.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFind_Click()
  2. Dim varwhere As Variant
  3. Dim rst As DAO.Recordset
  4.     'Initialize to Null
  5.     varwhere = Null
  6.  
  7. If IsNull(Me![Keyword]) Or (Me![Keyword]) = "" Then
  8.         MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criteria!"
  9.         Me![Keyword].SetFocus
  10.         Exit Sub
  11. End If
  12.  
  13.             If SortBy = "Tools" Then
  14.              varwhere = (varwhere + " AND ") & "[tools1] & [tool2] & [tool3] LIKE '" & Me.Keyword & "*'"
  15.                     Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM customers WHERE " & varwhere)
  16.                             If rst.RecordCount = 0 Then
  17.                                 Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM customers WHERE " & varwhere)
  18.                                 MsgBox "No Access Card Found.", vbInformation, gstrAppTitle
  19.                                 rst.Close
  20.                                 Set rst = Nothing
  21.                                 Exit Sub
  22.                             End If
  23.  
  24.                     rst.MoveLast
  25.                     DoCmd.OpenForm "searchresults", WhereCondition:=varwhere
  26.                     Forms!Searchresults.SetFocus
  27.                     DoCmd.Close acForm, Me.Name
  28.                     'Clean up recordset
  29.                     rst.Close
  30.                     Set rst = Nothing
  31.                     Exit Sub
  32.             End If
Mar 22 '08 #1
6 1776
Scott Price
1,384 Expert 1GB
First of all, please use the [code] tags provided! Simply select your code and click the # icon on the top of the reply window. You are unlikely to get any help at all if you simply post a block of code without the tags.

Second: take a careful look at line 14. If you notice, you have spelled [tools1] and [tool2] and [tool3]. Check to make absolutely sure that you have spelled them correctly, also, what are they? Text box controls, variables?? If they are text box controls, you will notice that the code highlighter indicates they are being treated as part of a string... You'll need to split them out from the string if you are trying to capture values from text boxes on the form.

Regards,
Scott
Mar 22 '08 #2
NeoPa
32,556 Expert Mod 16PB
Subscribing ...
Mar 25 '08 #3
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFind_Click()
  2. Dim varwhere As Variant
  3. Dim rst As DAO.Recordset
  4.     'Initialize to Null
  5.     varwhere = Null
  6.  
  7. If IsNull(Me![Keyword]) Or (Me![Keyword]) = "" Then
  8.         MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criteria!"
  9.         Me![Keyword].SetFocus
  10.         Exit Sub
  11. End If
  12.  
  13. If SortBy = "Access Card" Then
  14.                     varwhere = (varwhere + " AND ") & "[tool1] & [tool2] & [tool3] LIKE '" & Me.Keyword & "*'"
  15.                     Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM customers WHERE " & varwhere)
  16.                             If rst.RecordCount = 0 Then
  17.                                 Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM customers WHERE " & varwhere)
  18.                                 MsgBox "No tools found.", vbInformation, gstrAppTitle
  19.                                 rst.Close
  20.                                 Set rst = Nothing
  21.                                 Exit Sub
  22.                             End If
  23.  
  24.                     rst.MoveLast
  25.                     DoCmd.OpenForm "searchresults", WhereCondition:=varwhere
  26.                     Forms!Searchresults.SetFocus
  27.                     DoCmd.Close acForm, Me.Name
  28.                     'Clean up recordset
  29.                     rst.Close
  30.                     Set rst = Nothing
  31.                     Exit Sub
  32.             End If
Sorry about that im new to this i don't really have much knowledge of how to post. Regarding the textboxes they are in a table called customers and basically we enter what kind of tool the customer has. If i just search for just one variable like [tool1] then the search works perfectly but not always is going to be in [tool1] it could be in [tool2] or [tool3] etc. What i would is for the search method to look at all of these fields if possible and not just one textbox.
Mar 26 '08 #4
NeoPa
32,556 Expert Mod 16PB
Your WHERE clause needs to be of the form :
Expand|Select|Wrap|Line Numbers
  1. [Tools1] Like 'XYZ*' AND [Tools2] Like 'XYZ*' AND [Tools3] Like 'XYZ*'
& is a string concatenation character.
Mar 26 '08 #5
Your WHERE clause needs to be of the form :
Expand|Select|Wrap|Line Numbers
  1. [Tools1] Like 'XYZ*' AND [Tools2] Like 'XYZ*' AND [Tools3] Like 'XYZ*'
& is a string concatenation character.

Thank you so much for your help. I really appreciate it, to think that i have spent hours and hours trying to figure this out. Finally it works. Once again i thank you so much.
Mar 26 '08 #6
NeoPa
32,556 Expert Mod 16PB
No worries.

There is an article (Finding Jet SQL Help) that you may find useful for similar issues. The Help's not easy to find but it does have a lot of very useful information in it.
Mar 26 '08 #7

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

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
2
by: CharitiesOnline | last post by:
Hello, I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be depending on the number of results returned...
1
by: N. Graves | last post by:
Hi, I want to have a Search Dialog box that has several text box and fields to build a search and display the results in a form. I can do everything that I need to if I us a report but I would...
9
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
8
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled...
32
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...
0
by: Arun Nair | last post by:
Hi, I have an Employerregistration pagethat is used to add new Empoyers to a table. I also have another form search.aspx used to search the employers(based on their id) and edit the employer...
4
by: Dica | last post by:
i apologize for what is no doubt a very rudimentary question, but i'm still trying to wrap my brain around .net coding habits. in classic asp, if i wanted to show search results, i'd just post the...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
12
by: iahamed | last post by:
Hi Everyone, I got two parts of my advance search to work, I am running out of Logic to connect the third. My mind is in swing! Pleaseeeeeeeee Help me. I have 3 Fiels to search, the First two...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.