[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Rough Search

How can I tell VB to search for only part of a string instead of the whole thing. So if somebody searched for 'da' they would find records 'dan karran', 'dan'

Any help appreciated...
0
dankarran
Asked:
dankarran
  • 3
  • 2
  • 2
1 Solution
 
vbWayneCommented:
this should work

Select * from yourtable where yourfield like('*da*')

'Wayne
0
 
dankarranAuthor Commented:
Ok, how would I integrate that into here:

    Open App.Path & "\contacts.dat" For Random As #1 Len = Len(Contact)
    ContactTotalRec = LOF(1) / Len(Contact)
    ContactCurrentRec = 1
    For i = 1 To ContactTotalRec
    Get #1, i, Contact
    If LCase(Trim$(Contact.ContactName)) = LCase(Trim$(TheString)) Then
        WFind.LResults.AddItem (Contact.ContactName)
        AnyResult = True
    End If
    Next i
    Close #1


NB: Contact is a file, TheString is a variable passed to a subroutine

I want to search through all the records in teh file to find a match (even just a few letters of it) then add it to a listbox
at the moment it only works if the exact same string is entered as TheString (ignoring case as both are converted to lowercase before searching)
0
 
Éric MoreauSenior .Net ConsultantCommented:
You can't use vbWayne solution since it is not a relational-database source.

So instead, use this (only one line of your own solution is changed):

Open App.Path & "\contacts.dat" For Random As #1 Len = Len(Contact)
ContactTotalRec = LOF(1) / Len(Contact)
ContactCurrentRec = 1
For i = 1 To ContactTotalRec
    Get #1, i, Contact
    'The next line is changed
    If InStr(1, LCase(Trim$(Contact.ContactName)) , LCase(Trim$(TheString)) ) <> 0 Then
        WFind.LResults.AddItem (Contact.ContactName)
        AnyResult = True
    End If
Next i
Close #1
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dankarranAuthor Commented:
Sorry vbWayne, but emoreau's answer was much more helpful.

Maybe I didn't make it clear when I first asked the question.
0
 
dankarranAuthor Commented:
Thanks emoreau... I've no idea how it works but it solves the problem
:)
0
 
Éric MoreauSenior .Net ConsultantCommented:
The Instr statements stands for "Inspect String". It returns the first position of the occurence starting at the position stated at the 1st parameter of the search string or substring (3rd paramater) in the source string (2nd parameter).

So if 0 is returned, that means that the substring is not found!
0
 
vbWayneCommented:
No Problem....I just assumed!
Have a good one.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now