Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Listview Searching Question

Posted on 2006-05-31
10
Medium Priority
?
246 Views
Last Modified: 2013-12-25
Hi Experts,

I have a listview with 100 records in it.

What I am trying to achieve here is that:

I have a text box and whenever I type a search string - my listview will only contain the search string contains record.. and when the user is clear the text box, my old complete list will be back..

for example,

apple          fruit        green
appleFruit    desert    brown
oranage      fruit        orange

if I type "uit", my all list will be there and if I type green, only first record, and empty text box - all my list...

I am bringing to the listview from my db. So, I really do not want to search my database for those fields.. using listview much faster.. I guess...Can someone help me out..?

Thanks ...


 
0
Comment
Question by:neonlights
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:neonlights
ID: 16804564
Just following up:

I am also wondering is it a good to search the database? Access 97. Vb 6

If I can, without frezzing the system then, I can check all the fields in the database for the search string...

thanks
0
 
LVL 5

Expert Comment

by:fadloun
ID: 16805008
hello
I think the only way to achieve that without requiring database is :
- fill your database records into array
-when start typing clear the list
- search your array for entered string
-fill the list again with match result

regards
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Accepted Solution

by:
vb_elmar earned 2000 total points
ID: 16805482
Needs:
=====
1 Textbox
1 Listview (CTRL-T / Microsoft Windows Common Controls 6)




Dim a(9) As String, col As Long



Private Sub Form_Load()

a(0) = "apple,fruit,green"
a(1) = "appleFruit,desert,brown"
a(2) = "oranage,fruit,orange"

   
    Me.Caption = "ListView"
    ListView1.GridLines = True
    ListView1.FullRowSelect = True
   
    Move 0, 15 * 180
    ListView1.View = lvwReport 'lvwReport is needed to show the list items one below the other
 
    ListView1.ColumnHeaders.Clear
    ListView1.ColumnHeaders.Add , , "Index"
    ListView1.ColumnHeaders.Add , , "Column1"
    ListView1.ColumnHeaders.Add , , "Column2"
   
    Dim o As ListItem

End Sub




Private Sub Text1_Change()
    If a(y) = "" Then Exit Sub
    If Text1 = "" Then Exit Sub
   
    cnt = ListView1.ListItems.Count
    For r = cnt To 1 Step -1: ListView1.ListItems.Remove (r): Next
   
   
    For y = 0 To UBound(a)
           
            If InStr(1, a(y), Text1) <> 0 Then
                s = Split(a(y), ",")
                cnt = ListView1.ListItems.Count
                Set o = ListView1.ListItems.Add(cnt + 1, , s(0))
                   
                For x = 1 To 2
                    ListView1.ListItems(cnt + 1).SubItems(x) = s(x)
                Next
            End If
    Next
   
End Sub

0
 

Author Comment

by:neonlights
ID: 16807388
Hi, thanks for all your suggestions. I am thinking of doing this in array as fadloun  suggested...

Quick question: let's say if I have 1000 records, name, id, desc.. is there any disadvantage on storing them in a array - I mean (system resources, memory) - donno - just wondering...
0
 

Author Comment

by:neonlights
ID: 16807867
HI vb_elmar ,

I am going thru your code.. what is "s" is it an array?

just confused.. I thought I only need text box and listview to test your code.. is it correct?
0
 
LVL 5

Expert Comment

by:fadloun
ID: 16807939
hi
with 1000 records ,You should not face any problem ,,,,
BTW ,s is an array ,split function return an array.
a(0) = "apple,fruit,green"
s=split(a(0),",")
this return
s(0)="apple"
s(1)="fruit"
s(2)="green"


regards
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 16807981
>> what is "s" is it an array?
Yes it's an array.

if you use the following:
a = "apple,fruit,green"
s = Split(a, ",")

... then VB generates an array with the three variables  :

s(0)="apple"
s(1)="fruit"
s(2)="green"

UBound(s) returns the highest index number of the array - in this case: "2"

========================================================
2nd question:
>> I only need text box and listview to test your code.. is it correct?
Yes it is correct
0
 

Author Comment

by:neonlights
ID: 16808542
Hi, vb_elmar, I used your code... and worked pretty good with some modifications - for my need.

I have a one problem.

How do I skip for loop - not exit - but, skip to the next record..

Here is the code I have...

Private Sub TxtSearchField_Change()
Dim o As ListItem
Dim cnt As Integer
Dim r As Integer
Dim Y As Integer
Dim s() As String
Dim X As Integer
If Me.Option1(0).Value = True Then
    If TxtSearchField = "" Then
        MeublesViewMainPage
        Exit Sub
    End If
    cnt = Me.ListView1.ListItems.Count
    For r = cnt To 1 Step -1: ListView1.ListItems.Remove (r): Next
   
    For Y = 0 To UBound(Array_Meubles)
        If InStr(1, UCase(Array_Meubles(Y).SearchString), UCase(TxtSearchField)) <> 0 Then
            If Me.cbofrom.Text = "" And Me.cboto.Text = "" Then
                     ' it is okay
            ElseIf Me.cbofrom.Text <> "" And Me.cboto.Text = "" Then
                If Array_Meubles(Y).Prix >= Me.cbofrom.Text Then
                   ' Found record
                Else
                    ' Skip the rest of the code and goto the NEXT RECORD - { I have to go to the end of the just before "Next" - skip all the sentence below.. }
                End If
            ElseIf Me.cbofrom.Text = "" And Me.cboto.Text <> "" Then
           
            ElseIf Me.cbofrom.Text <> "" And Me.cboto.Text <> "" Then
           
            End If
           
                s = Split(Array_Meubles(Y).SearchString, ",")
                cnt = ListView1.ListItems.Count
                Set o = ListView1.ListItems.Add(cnt + 1, , Array_Meubles(Y).TypeM)
                ListView1.ListItems(cnt + 1).SubItems(1) = Array_Meubles(Y).NomModele
                ListView1.ListItems(cnt + 1).SubItems(2) = Array_Meubles(Y).Prix
                ListView1.ListItems(cnt + 1).Tag = Array_Meubles(Y).ID
           
        End If
    Next
End If
End Sub
0
 

Author Comment

by:neonlights
ID: 16852390
Thanks vb_elmar - Worked pretty good. I had some errors.. but, your code worked pretty good.

Thanks
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question