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

SearchBox NoComboBox NoWizardSearchCmd

I am trying to put a text box on my form where I can just type in what I am searching for (I want to be able to type in information in the search box that may be on any field on the form; I want this text box to have the ability to search any field on the form without me using a combo box where I have to select the field then type in what I am searching for and I don't want to use the Wizard to create a search command that is basically CTRL+F). I am specific to what I want. So if you could help that would be awesome...Here are my field names it would be so helpful if I could just get the code and copy and paste it into Microsoft Visual Basic. Mind you I am working with Access 2003 if that makes a difference.


Search Command is called: cmdSearch
Unbound Search Field is called: txtSearchString

Field Names are as follows:
PhysicianName
PrimaryHospital
Address1
Address2
Address3
CityStateZipCode

Please help only if you can do what I am asking I already know how to do the combo box search and wizard search and don't like either. Plus if I see I have a response I will get excited and then be bummed when realizing that 2 years of trying to figure this out continues:(

Thank you soooooooo much if you can help me with this I wouldn't even know how to explain how awesome it is that I could do this!!!
Feb 28 '11 #1
10 1627
TheSmileyCoder
2,322 Expert Mod 2GB
It would look something like this I guess:
Expand|Select|Wrap|Line Numbers
  1. Private Sub tb_Search_KeyUp(KeyCode As Integer, Shift As Integer)
  2.     Dim strCrit As String
  3.     strCrit = Me.tb_Search.Text
  4.     Dim strFilter As String
  5.     Dim rsDao As DAO.Recordset
  6.     Set rsDao = CurrentDb.OpenRecordset(Me.RecordSource)
  7.     Dim myF As Field
  8.     For Each myF In rsDao.Fields
  9.         'You can expand the code by type if you want.
  10.         'Debug.Print myF.Name & ":" & myF.Type
  11.         strFilter = strFilter & "([" & myF.Name & "]='" & strCrit & "') OR "
  12.     Next
  13.     'Strip last " OR "
  14.     strFilter = Left(strFilter, Len(strFilter) - Len(" OR "))
  15.     Debug.Print strFilter
  16.     Set rsDao = Nothing
  17.     Me.Filter = strFilter
  18.     Me.FilterOn = True
  19. End Sub
Mar 1 '11 #2
The name of my table is USPhysicianDirectory and the name of the form that I want the search button on is USPhysicianDirectoryFM...where would I copy and paste this information you gave me? Would I go to the search button and select On Click:[Event Procedure] and then copy this in the MVB or would I go to the unbound text boxPOn Click:[Event Procedure] and then copy this in the MVB? Sorry don't really understand...also do I need to change anything in the code to fit my form or is it all set and ready to be copied? Thank you so much for helping me!!!
Mar 1 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
The intent is that the code runs each time a key is released in the textbox tb_Search. This is also shown in the code, if you read the first line.
Mar 2 '11 #4
I'm sorry this is new to me so I don't really understand where to paste this code and if I can just paste it directly somewhere or if I need to change some of the names so that it will recognize the fields in my database.
Mar 2 '11 #5
Okay so when I put this in VB it worked but made me ad on Line 18. Me.FilterOn=True=True
So I did that then it wanted me to chage Line 17.and add extra coding so here is what we have...the problem with this is that when I go to type in a letter it only allows me to type in one letter and then says it has filtered the information however the form is then blank...any suggestions?

Private Sub tb_Search_KeyUp(KeyCode As Integer, Shift As Integer)
Dim strCrit As String
strCrit = Me.tb_Search.Text
Dim strFilter As String
Dim rsDao As DAO.Recordset
Set rsDao = CurrentDb.OpenRecordset(Me.RecordSource)
Dim myF As Field
For Each myF In rsDao.Fields
'You can expand the code by type if you want.
'Debug.Print myF.Name & ":" & myF.Type
strFilter = strFilter & "([" & myF.Name & "]='" & strCrit & "') OR "
Next
'Strip last " OR "
strFilter = Left(strFilter, Len(strFilter) - Len(" OR "))
Debug.Print strFilter
Set rsDao = Nothing
Me.Filter = "([PhysiciansName]='*') OR ([PrimaryHospital]='*')OR ([Address1]='*')OR ([Address2]='*')OR ([Address1]='*')"
Me.FilterOn = True = True
Me.FilterOn = True = True
Me.FilterOn = True = True[/b]

End Sub
Mar 3 '11 #6
TheSmileyCoder
2,322 Expert Mod 2GB
Im sorry, I presumed from your original question that you hade some code experience since you claimed to know all about the other methods of filtering a form.

A few notes:
Expand|Select|Wrap|Line Numbers
  1. Me.FilterOn = True = True
Is equal to:
Expand|Select|Wrap|Line Numbers
  1. Me.FilterOn = (True = True)
Basicly the code will first evaluate whether true is equal to true, which it is, which in turn will return true, which is equal to :
Expand|Select|Wrap|Line Numbers
  1. Me.FilterOn = True
Your just messing up the code by adding the extra =true.

By adding the
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = "([PhysiciansName]='*') OR ([PrimaryHospital]='*')OR ([Address1]='*')OR ([Address2]='*')OR ([Address1]='*')"
Your overruling all the previous code intended to build the filter, by replacing it with a hardcoded filter.


The code is something I threw together as an example. Not something I have extensively tested and added error handling too. For instance I forgot to add the asterisk * to the search filter. I have tested the code and tweaked it a bit.

Expand|Select|Wrap|Line Numbers
  1. Private Sub tb_Search_KeyUp(KeyCode As Integer, Shift As Integer)
  2.     Dim strCrit As String
  3.     Dim lngSelStart As Long
  4.     lngSelStart = Me.tb_Search.SelStart
  5.     strCrit = Me.tb_Search.Text
  6.     Dim strFilter As String
  7.     Dim rsDao As DAO.Recordset
  8.     Set rsDao = CurrentDb.OpenRecordset(Me.RecordSource)
  9.     Dim myF As Field
  10.     For Each myF In rsDao.Fields
  11.         'You can expand the code by type if you want,
  12.         '  to have special code for numeric combobox
  13.         '  fields.
  14.         'Debug.Print myF.Name & ":" & myF.Type
  15.         strFilter = strFilter & "([" & myF.Name & "] LIKE '*" & strCrit & "*') OR "
  16.     Next
  17.     'Strip last " OR "
  18.     strFilter = Left(strFilter, Len(strFilter) - Len(" OR "))
  19.     Debug.Print strFilter
  20.     Set rsDao = Nothing
  21.  
  22.     Me.Filter = strFilter
  23.     Me.FilterOn = True
  24.  
  25.     'The filter causes the textbox to loose focus,
  26.     '  and when focus is returned automatically
  27.     '  the form looses the cursor position and
  28.     '  selectes all of the field. Next keypress will
  29.     '  then replace the current text. Return focus in
  30.     '  a more gracefull way, returning the cursor to
  31.     '  its last known position
  32.  
  33.     Me.tb_Search.SetFocus
  34.     Me.tb_Search.SelStart = lngSelStart
  35.  
  36. End Sub
  37.  
Mar 4 '11 #7
You might be the greatest person in the world!!! This code works and it is awesome! Thank you sooosoooosoooooooooo much!!!
Mar 4 '11 #8
Okay I have one more question. I want to place a label over the unbound text box that says search and when the label is clicked on I want it to become invisible so that I can type in the text I am searching for or is there a code where I can set the background of the unbound text box to say search until a key is entered in it?
Mar 4 '11 #9
Actually I was able to figure it out so thanks again!

Private Sub SearchLBL_Click()
' Sets focus on tb_search.
Me.[tb_Search].SetFocus
SearchLBL.Visible = False
End Sub
Mar 4 '11 #10
TheSmileyCoder
2,322 Expert Mod 2GB
Your welcome.
TheSmileyCoder
Mar 4 '11 #11

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

Similar topics

2
by: jimi_xyz | last post by:
Sorry if this isn't the correct group, i don't think there is a group for straight HTML. I am trying to create a type of search engine. There are two radio buttons at the top, in the middle there...
1
by: Will | last post by:
I have a combo box on a form which is based on table tblMachine. On that combo box I have four columns visible MachineNumber, description, location and type. The bound column is the MachineNumber...
0
by: panik | last post by:
Hi, I have a Control, a SearchBox class, that is derived from UserControl. It is placed inside another UserControl (a page structure) that is placed inside a custom page class (derived from...
5
by: Jeff Evans | last post by:
I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the CreateChildControls() method Here is the code for the...
0
by: dcallan | last post by:
Hello everyone, hope all is well. I looking to find out how to add a textbox to the windows taskbar permanently perhaps next to the quick launch group of items. Something similar to the screen shot...
1
by: cglewis03 | last post by:
Hello, I am trying to build a search form with several different options to choose from. Currently it is set up to open within the same window if a single option is selected and open within a...
2
by: stupidintern | last post by:
Hi, This is my first post here so please be patient. Here is my question: I am trying to include a chart in my VB form. I do it this way: I drop the Microsoft Graph Chart Control (from Unbound...
1
by: farisallil | last post by:
Hi I have a form called Invoice that has a subform called Invoice_datasheet. The subform has the field Customer as the child link and Customer_Name in the Invoice form (The master) as the master...
2
by: claudiathompson | last post by:
Hello, I have one other question. Yesterday I asked about how to tell a searchbox code what background color it should have and someone answered me and it works great! But the other question...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.