473,385 Members | 1,821 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 a string and display all items containing that string

Richard Alvarez
i put a search on my program and i use SQL. I want my search function to search even just the part of the string in my access database. how would i do that?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     Dim rsData As ADODB.Recordset
  4.  
  5.     ' load the database
  6.     Set DBConn = LoadDatabase(App.Path & "\dbase\dbmain.mdb")
  7.  
  8.     ListEmployees
  9.     Me.Timer2.Enabled = False
  10. End Sub
  11.  
  12. Private Sub Form_Resize()
  13.     Me.proOne.Width = Me.ScaleWidth - 9100
  14.     Me.txtSearch.Width = Me.ScaleWidth - (Me.txtSearch.Left * 20)
  15.     Me.lstMaster.Width = Me.ScaleWidth - (Me.lstMaster.Left * 2)
  16.     Me.lstMaster.Height = Me.ScaleHeight - 1600
  17. End Sub
  18.  
  19.  
  20.  
  21. Private Sub lstMaster_DblClick()
  22.     If Not (Me.lstMaster.SelectedItem Is Nothing) Then
  23.         frmNew.txtID.Text = Me.lstMaster.SelectedItem.SubItems(0)
  24.         frmNew.txtFName.Text = Me.lstMaster.SelectedItem.SubItems(1)
  25.         frmNew.cboPosition.Text = Me.lstMaster.SelectedItem.SubItems(2)
  26.         frmNew.txtStatus.Text = Me.lstMaster.SelectedItem.SubItems(3)
  27.         frmNew.txtRate.Text = Me.lstMaster.SelectedItem.SubItems(4)
  28.         frmNew.dtpStart.value = Me.lstMaster.SelectedItem.SubItems(5)
  29.         frmNew.dtpEnd.value = Me.lstMaster.SelectedItem.SubItems(6)
  30.         frmNew.txtAge.Text = Me.lstMaster.SelectedItem.SubItems(7)
  31.         frmNew.txtAddress.Text = Me.lstMaster.SelectedItem.SubItems(8)
  32.  
  33.         frmNew.cmdUpdate.Caption = "&Update"
  34.  
  35.         frmNew.Show
  36.         Unload Me
  37.     End If
  38. End Sub
  39.  
  40. Private Sub mnuExit_Click()
  41.     frmMain.Show
  42.     Unload Me
  43. End Sub
  44.  
  45. Private Sub mnuManageUsers_Click()
  46.     frmManageUsers.Show
  47.     Unload Me
  48. End Sub
  49.  
  50. Private Sub mnuNew_Click()
  51.     frmNew.Show
  52.     Unload Me
  53. End Sub
  54.  
  55. Private Sub Timer1_Timer()
  56. If Me.proOne.value = "100" Then
  57.     Call SearchAll
  58.     Me.Timer1.Enabled = False
  59.     Me.Timer2.Enabled = True
  60. Else
  61.     Me.proOne.value = Me.proOne.value + Val(1)
  62. End If
  63. End Sub
  64.  
  65. Private Sub Timer2_Timer()
  66.     If Me.Label1.Visible = True Then
  67.         Me.Label1.Visible = False
  68.     ElseIf Me.Label1.Visible = False Then
  69.         Me.Label1.Visible = True
  70.     End If
  71. End Sub
  72.  
  73. Private Sub txtSearch_Click()
  74.     Me.txtSearch.Text = ""
  75.     Me.Timer2.Enabled = False
  76. End Sub
  77.  
  78. Private Sub txtSearch_GotFocus()
  79.     Me.txtSearch.Text = ""
  80. End Sub
  81.  
  82. Private Sub ListEmployees()
  83. Set rsData = DBConn.Execute("SELECT EmpID, EmpName, EmpPosition, EmpStatus, EmpRate, DateHired, Endo, EmpAge, EmpAddress FROM tblMasterData")
  84.  
  85.     Me.lstMaster.ListItems.Clear
  86.     If rsData.RecordCount > 0 Then
  87.         rsData.MoveFirst
  88.  
  89.         Do Until rsData.EOF Or rsData.BOF
  90.             With Me.lstMaster.ListItems.Add(, , rsData("EmpID").value & "")
  91.                 .SubItems(1) = rsData("EmpName").value & ""
  92.                 .SubItems(2) = rsData("EmpPosition").value & ""
  93.                 .SubItems(3) = rsData("EmpStatus").value & ""
  94.                 .SubItems(4) = rsData("EmpRate").value & ""
  95.                 .SubItems(5) = rsData("DateHired").value & ""
  96.                 .SubItems(6) = rsData("Endo").value & ""
  97.                 .SubItems(7) = rsData("EmpAge").value & ""
  98.                 .SubItems(8) = rsData("EmpAddress").value & ""
  99.             End With
  100.             rsData.MoveNext
  101.         Loop
  102.     End If
  103. End Sub
  104.  
  105. Private Sub txtSearch_KeyPress(KeyAscii As Integer)
  106.     If KeyAscii = 13 Then
  107.         If Me.txtSearch.Text <> "" Then
  108.             Me.proOne.Visible = True
  109.             Me.proOne.value = 0
  110.             Me.Timer1.Enabled = True
  111.             Me.Timer2.Interval = 400
  112.         Else
  113.             Me.Label1.Caption = "Type in a name to search."
  114.             Me.Label1.ForeColor = vbRed
  115.             Me.txtSearch.SetFocus
  116.         End If
  117.     End If
  118. End Sub
  119.  
  120. Private Sub SearchAll()
  121. Set rsData = DBConn.Execute("SELECT * FROM tblMasterData WHERE EmpName = '" & Me.txtSearch.Text & "'")
  122.  
  123. Me.lstMaster.ListItems.Clear
  124.     If rsData.RecordCount > 0 Then
  125.         rsData.MoveFirst
  126.  
  127.         Do Until rsData.EOF Or rsData.BOF
  128.             With Me.lstMaster.ListItems.Add(, , rsData("EmpID").value & "")
  129.                 .SubItems(1) = rsData("EmpName").value & ""
  130.                 .SubItems(2) = rsData("EmpPosition").value & ""
  131.                 .SubItems(3) = rsData("EmpStatus").value & ""
  132.                 .SubItems(4) = rsData("EmpRate").value & ""
  133.                 .SubItems(5) = rsData("DateHired").value & ""
  134.                 .SubItems(6) = rsData("Endo").value & ""
  135.                 .SubItems(7) = rsData("EmpAge").value & ""
  136.                 .SubItems(8) = rsData("EmpAddress").value & ""
  137.             End With
  138.             Me.Label1.Caption = "Found Existing Record(s)."
  139.             rsData.MoveNext
  140.         Loop
  141.     Else
  142.         Me.Label1.Caption = "No Record(s) Found!"
  143.     End If
  144. End Sub
  145.  
the problem is that it searches for the whole match of the string. i want it to be like this
the field contains john Castro, Robert So, Rick Williams ....
and if i type "john" it will display all records with the word "john".

please help me with.
much appreciated.
Apr 17 '11 #1

✓ answered by Rabbit

The exact syntax and wildcards depend on the DBMS that you're using.
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM SomeTable
  3. WHERE SomeField LIKE '%hello%'

6 2007
Rabbit
12,516 Expert Mod 8TB
Instead of the = operator, use the like operator with wildcards.
Apr 18 '11 #2
That "LIKE" thing, is that something like this;
Expand|Select|Wrap|Line Numbers
  1. Set rsData = DBConn.Execute("SELECT * FROM tblMasterData WHERE EmpName LIKE '" & Me.txtSearch.Text & "'")
Sir i'm sorry to admit that i'm new with these SQL lines.
Culd you give me some example please?
Apr 19 '11 #3
Rabbit
12,516 Expert Mod 8TB
The exact syntax and wildcards depend on the DBMS that you're using.
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM SomeTable
  3. WHERE SomeField LIKE '%hello%'
Apr 19 '11 #4
In my program, the user will input his/her query in a txtbox, so how would I going to replace that '%hello%' with my txtbox like '"%me.txtFName.txt%"'?
Apr 20 '11 #5
Sir here is what I did but still don't know if it's correct.
Expand|Select|Wrap|Line Numbers
  1. Set rsData = DBConn.Execute("SELECT * FROM tblMasterData WHERE EmpName LIKE" & "*" & Val(Me.txtSearch.Text) & "*")
Apr 20 '11 #6
Rabbit
12,516 Expert Mod 8TB
You do it the same way you have it in your original code. Except now you're using Like and wildcards as part of the string.
Apr 20 '11 #7

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

Similar topics

13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: AMT2K5 | last post by:
Hello, I have an assignment to do in class which requires me to create a single line editor not a multi-line editor. I am confused about what this means, I would appreciate any clarification....
3
by: Jozef Jarosciak | last post by:
Quickest way to find the string in 1 dimensional string array! I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need...
1
by: vinayg | last post by:
how can i search subcategory wise for a particular category,i want to display items.suppose user selected a particular category.I want to display all items in that category + its subcategories. ...
0
by: daisies | last post by:
Hi Everyone, I'm new to java and to web development. I need to know how to display items of an arraylist in a h:datatable to look like this: ---------------------------- | item1 | item2 |...
9
by: Jeremy Kitchen | last post by:
Are there any library functions that can help me to do this? If necessary I can convert the string to a byte array. I don't want to have to write my own Hex conversion if it isn't necessary. ...
16
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a...
20
by: Xcriber51 | last post by:
Hi -- I'm not entirely familiar with the norms and standard libraries of JavaScript so if the answer to this is yesterday's news, please ignore. I'm trying to write a simple text formatting...
0
by: anoop s | last post by:
Hi, How to display items of completion list of ajax autocompleteextender one at a row. Now it is displaying two or three according to the size of items in a row. thanks
0
by: phpuser123 | last post by:
I want to display my string str as the defaultinput value and when I run ,oly the first part of the word is displayed. It skips everything after the blank space.How do I sort this out? <% ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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?

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.