Search problem

Search Problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1911 Views - Last Post: 02 April 2009 - 12:00 AM Rate Topic: -----

#1 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Search problem

Post icon  Posted 30 March 2009 - 10:47 PM

sir,
I have a problem that I have a list box ,in which i have the nemes e.g.ajay,abhay,abhinav,bani,coma,naveen etc. and have a text box for searching the name in the text box.When i press the a,it will show ajay,abhay and the abhinav.Which is good.when i add ab in text box,it will show abhay and abhinav,which is also right,but when I backspace the ab to a,then it will not show the names ajay,abhay and the abhinav,but it will show the abhay and the abhinav.Perhaps u understand my problem.My code is
Private Sub txtSearch1_Change()
	Dim intloopindex As Integer
	Dim charPos As Integer
	Dim intcount As Integer
	Dim strvariable() As String
	If Len(txtSearch1.Text) = 0 Then
		lstUserinfo.Clear
		Call Form_Load
		Exit Sub
	End If
	If lstUserinfo.ListCount > 0 Then
		ReDim strvariable(0)
		Dim i As Integer
		Dim j As Integer
		For i = lstUserinfo.ListCount - 1 To 0 Step -1
			If (InStr(1, lstUserinfo.List(i), txtSearch1.Text, vbTextCompare) = 1) Then
				strvariable(j) = lstUserinfo.List(i)
				j = j + 1
				ReDim Preserve strvariable(j)
			End If
		Next
		lstUserinfo.Clear
		For j = 0 To UBound(strvariable) - 1
			lstUserinfo.AddItem strvariable(j)
		Next
	End If

.
Please help me.............. B)

Is This A Good Question/Topic? 0
  • +

Replies To: Search problem

#2 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: Search problem

Posted 31 March 2009 - 01:24 AM

Hi,

The problem is, you are clearing your list before adding the names again.

This gives you an error, because the names are lost when you clear, so it can only work with the names in the list at that time.

To begin with you have Abay, Abinav, Ajay, Bani etc..

You type A and it leaves Abay, Abinav, Ajay in the list.

You type AB it leaves Abay, Abinav

If you delete the B, there are only two items in the list to search, which is what it gives you.

You need to look at repopulating the list if you want to do it that way, or you could look at using a hidden list to query against to populate the visible list, or use a combo box that you can type into which will do the search (if you're using it for selecting a single name).

Hope that helps.

Mark
Was This Post Helpful? 0
  • +
  • -

#3 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 31 March 2009 - 02:19 AM

View Postmaj3091, on 31 Mar, 2009 - 12:24 AM, said:

Hi,

The problem is, you are clearing your list before adding the names again.

This gives you an error, because the names are lost when you clear, so it can only work with the names in the list at that time.

To begin with you have Abay, Abinav, Ajay, Bani etc..

You type A and it leaves Abay, Abinav, Ajay in the list.

You type AB it leaves Abay, Abinav

If you delete the B, there are only two items in the list to search, which is what it gives you.

You need to look at repopulating the list if you want to do it that way, or you could look at using a hidden list to query against to populate the visible list, or use a combo box that you can type into which will do the search (if you're using it for selecting a single name).

Hope that helps.

Mark

Is there any solution in my code,I have clear the line that clear the list box??Edit my code and help me if u can??
Was This Post Helpful? 0
  • +
  • -

#4 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: Search problem

Posted 31 March 2009 - 02:48 AM

I'm not 100% sure what you are trying to acheive, is it basically to allow a name to be selected from the list by a query?

If so.....you could try the following..


Private Sub txtSearch1_Change()
	Dim intloopindex As Integer
	Dim charPos As Integer
	Dim intcount As Integer
	Dim strvariable() As String
	If Len(txtSearch1.Text) = 0 Then
		lstUserinfo.Clear
		Call Form_Load
		Exit Sub
	End If
	If lstUsers.ListCount > 0 Then
		ReDim strvariable(0)
		Dim i As Integer
		Dim j As Integer
		For i = lstUsers.ListCount - 1 To 0 Step -1
			If (InStr(1, lstUsers.List(i), txtSearch1.Text, vbTextCompare) = 1) Then
				strvariable(j) = lstUsers.List(i)
				j = j + 1
				ReDim Preserve strvariable(j)
			End If
		Next
		lstUserinfo.Clear
		For j = 0 To UBound(strvariable) - 1
			lstUserinfo.AddItem strvariable(j)
		Next
	End If



Notice the changes in your code above where I use a new list called LstUsers.

You need to add another listbox control and make it invisible, but use that list to hold the users and lstUserInfo is only to display the queried users.

There are probably better ways to do it, but that should work for you.
Was This Post Helpful? 0
  • +
  • -

#5 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 31 March 2009 - 03:42 AM

View Postmaj3091, on 31 Mar, 2009 - 01:48 AM, said:

I'm not 100% sure what you are trying to acheive, is it basically to allow a name to be selected from the list by a query?

If so.....you could try the following..


Private Sub txtSearch1_Change()
	Dim intloopindex As Integer
	Dim charPos As Integer
	Dim intcount As Integer
	Dim strvariable() As String
	If Len(txtSearch1.Text) = 0 Then
		lstUserinfo.Clear
		Call Form_Load
		Exit Sub
	End If
	If lstUsers.ListCount > 0 Then
		ReDim strvariable(0)
		Dim i As Integer
		Dim j As Integer
		For i = lstUsers.ListCount - 1 To 0 Step -1
			If (InStr(1, lstUsers.List(i), txtSearch1.Text, vbTextCompare) = 1) Then
				strvariable(j) = lstUsers.List(i)
				j = j + 1
				ReDim Preserve strvariable(j)
			End If
		Next
		lstUserinfo.Clear
		For j = 0 To UBound(strvariable) - 1
			lstUserinfo.AddItem strvariable(j)
		Next
	End If



Notice the changes in your code above where I use a new list called LstUsers.

You need to add another listbox control and make it invisible, but use that list to hold the users and lstUserInfo is only to display the queried users.

There are probably better ways to do it, but that should work for you.

sir,
I try this with another list box,as u give.It works but i want to work in the same list box,have u any idea?
Was This Post Helpful? 0
  • +
  • -

#6 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: Search problem

Posted 31 March 2009 - 04:00 AM

Try writing a procedure to populate the list box with all the names, calling the .clear method of the listbox first, then adding the items.

Call this procedure as shown below:

Private Sub txtSearch1_Change()
	Dim intloopindex As Integer
	Dim charPos As Integer
	Dim intcount As Integer
	Dim strvariable() As String
	If Len(txtSearch1.Text) = 0 Then
		'CALL PROCEDURE HERE TO POPULATE
		Call Form_Load
		Exit Sub
	End If

	'CALL PROCEDURE HERE TO POPULATE

	If lstUserinfo.ListCount > 0 Then
		ReDim strvariable(0)
		Dim i As Integer
		Dim j As Integer
		For i = lstUserinfo.ListCount - 1 To 0 Step -1
			If (InStr(1, lstUserinfo.List(i), txtSearch1.Text, vbTextCompare) = 1) Then
				strvariable(j) = lstUserinfo.List(i)
				j = j + 1
				ReDim Preserve strvariable(j)
			End If
		Next
		lstUserinfo.Clear
		For j = 0 To UBound(strvariable) - 1
			lstUserinfo.AddItem strvariable(j)
		Next
	End If



This is far from ideal, but should work.
Was This Post Helpful? 0
  • +
  • -

#7 Nikhil_07n   User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

Re: Search problem

Posted 31 March 2009 - 09:28 AM

I have'nt red your code, but i'm writing a new one
I'm not using redim command, i'm just printing the required items of List1 to List2.

I have all items in List1 from which i'm gonna transfer them to List2!

Private sub txtSrch_Change()

Static xy as Integer
Dim strSr as String

List2.Clear

strSr = txtSrch.text

For i=0 to List1.Listcount
If Instr( 1, List1.List(i), strSr ) <> 0 Then
List2.AddItem List1.List(i)
xy=xy + 1 'helpful when using ReDim statement
End If
Next i




now you just have to add items of List2 in an array.

This post has been edited by Nikhil_07n: 31 March 2009 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 31 March 2009 - 09:38 PM

View PostNikhil_07n, on 31 Mar, 2009 - 08:28 AM, said:

I have'nt red your code, but i'm writing a new one
I'm not using redim command, i'm just printing the required items of List1 to List2.

I have all items in List1 from which i'm gonna transfer them to List2!

Private sub txtSrch_Change()

Static xy as Integer
Dim strSr as String

List2.Clear

strSr = txtSrch.text

For i=0 to List1.Listcount
If Instr( 1, List1.List(i), strSr ) <> 0 Then
List2.AddItem List1.List(i)
xy=xy + 1 'helpful when using ReDim statement
End If
Next i




now you just have to add items of List2 in an array.

sir,i want that the result of search display only on the one list box
Was This Post Helpful? 0
  • +
  • -

#9 Nikhil_07n   User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

Re: Search problem

Posted 31 March 2009 - 09:42 PM

-You have a Listbox ('list1') in which you have all the items.
-You want all the search items in THAT('list1') lisbox only.

Am I right?
Was This Post Helpful? 0
  • +
  • -

#10 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 31 March 2009 - 11:00 PM

View PostNikhil_07n, on 31 Mar, 2009 - 08:42 PM, said:

-You have a Listbox ('list1') in which you have all the items.
-You want all the search items in THAT('list1') lisbox only.

Am I right?

yes,i want that result will appear in that list box,Please read my code and understand my problem.It run good in right side but when i backspace it will not show all the names........
Was This Post Helpful? 0
  • +
  • -

#11 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: Search problem

Posted 31 March 2009 - 11:48 PM

I think if you tried my last post, writing a procedure to re-populate the listbox, you will find that it works in a single listbox as you want.

Have you actually tried it?
Was This Post Helpful? 0
  • +
  • -

#12 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 01 April 2009 - 02:44 AM

View Postmaj3091, on 31 Mar, 2009 - 10:48 PM, said:

I think if you tried my last post, writing a procedure to re-populate the listbox, you will find that it works in a single listbox as you want.

Have you actually tried it?

No,I am unable to do so..........Can u do it??
Was This Post Helpful? 0
  • +
  • -

#13 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 330
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: Search problem

Posted 01 April 2009 - 03:23 AM

Yes I can do it....but I'm sure you can do it also.

You are only writing a procedure to add items to the list box.

list1.additem("Name 1")
list1.additem("Name 2")
etc.

Was This Post Helpful? 0
  • +
  • -

#14 naveen sharma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-March 09

Re: Search problem

Posted 01 April 2009 - 05:11 AM

View Postmaj3091, on 1 Apr, 2009 - 02:23 AM, said:

Yes I can do it....but I'm sure you can do it also.

You are only writing a procedure to add items to the list box.

list1.additem("Name 1")
list1.additem("Name 2")
etc.

its not working,i think if i use the len of the item added in the search text box,is it right?
Was This Post Helpful? 0
  • +
  • -

#15 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Search problem

Posted 01 April 2009 - 06:47 PM

where did you populate this records it it is a data base then there is a simple way to do this or else just store it in a array and filter this array every time and store this thing in the listbox's item
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2