help to Filtering Listbox

zozzz

Registered User.
Local time
Today, 07:30
Joined
Sep 19, 2015
Messages
14
hi everyone
I have a module for filtering subform bese on 4 Fields in onchange event and I cant use it to filtering Listbox . can anyone help please ?


Module Code :

Option Compare Database
Function Search(Expr As String, Domain As String, txt_search As String) As String
x = txt_search & " "
For i = 1 To Len(txt_search)
c = InStr(1, x, " ")
If c > 0 Then
y = Left(x, c - 1)
x = Right(x, Len(x) - c)
i = c
q = q & " and " & "((" & Domain & "." & Expr & ") like '*" & y & "*')"
Else
Exit For
End If
Next i
q = q & " and " & "((" & Domain & "." & Expr & ") like '*" & x & "*')"
Search = Right(q, Len(q) - 5)
Debug.Print x
End Function

Text4 code for filtering subform :

Private Sub Text4_Change()
On Error Resume Next
Me.Form_sub.Form.Filter = Search("[Fname] & [Contry] & [Age] & [Phon]", "Table1", Nz(Me.Text4.Text))
Me.Form_sub.Form.FilterOn = True
Me.Form_sub.Form.Requery
If Me.Text4.Text = "" Then
Me.Form_sub.Form.FilterOn = False
Me.Form_sub.Form.Requery
End If
End Sub

Text4 code for filtering Listbox ???

 
You want to use that function? Presumably it creates a valid filter, so to use it on a listbox you'd set the row source:

Me.ListboxName.RowSource = "SELECT Blah FROM TableName WHERE " & Search(...)
 
Thanks for your help

Excellent My Friend


 
As written above, does that function work?
notably you have no dim statements.

you should also have at the top of each module
Code:
Option Compare Database
Option Explicit
 
As written above, does that function work?
notably you have no dim statements.

you should also have at the top of each module
Code:
Option Compare Database
Option Explicit
==========================
Compile Error:
Variable not defined



what is this error for??
and how can fix it ??
 
==========================
Compile Error:
Variable not defined



what is this error for??
and how can fix it ??

Option explicit requires you to declare the datatype of all variables used.
It makes for better coding though with a little more effort.

So in your case you need statement like this for all variable used

Code:
 Dim I as Integer, c as integer
Dim x as string

and so on ...
 
if you go into the VBE under tools>options, you can set it to always include Option Explicit in all your modules. It will save you a lot of frustration in the long run.
 
Special Thanks to dear friends for replay

this code for just one textbox

Code:
Private Sub Text10_Change()
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & Search("[Fname] & [Contry] & [Age] & [Phon]", "Table1", Nz(Me.Text10.Text))
Me.List2.Requery
End Sub

Now, if we want to define separate textbox how to change the code ?
txt_fname , txt_contry , txt_age
 
You could just do this for e.g. textbox Text2:

Code:
Private Sub Text2_Change()
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & Search("[Fname] & [Contry] & [Age] & [Phon]", "Table1", Nz(Me.Text2,""))
Me.List2.Requery
End Sub

or perhaps use this repeateable code:
Code:
Private Sub Text2_Change()
strText = "Me.Text2"
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & Search("[Fname] & [Contry] & [Age] & [Phon]", "Table1", Nz(strText,""))
Me.List2.Requery
End Sub

If you do the 2nd version, add this line in the form module declaration section under Option Explicit

Code:
Dim strText As String
 
where is my fault in this code ?

for text1 :
Code:
Private Sub Text1_Change()
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & _
(Search("[Fname]", "Table1", Nz(Me.Text1.Text))) And (Search("[Contry]", "Table1", Nz(Me.Text2.Text)))
Me.List2.Requery
End Sub
and for text2
Code:
Private Sub Text2_Change()
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & _
(Search("[Contry]", "Table1", Nz(Me.Text2.Text))) And (Search("[Fname]", "Table1", Nz(Me.Text1.Text)))
Me.List2.Requery
End Sub
To better understand
I wish could attached file
 
Some points to consider:

1. You need to specify the alternative in each of the Nz statements

2. You almost certainly don't need .Text

3. Bracketing may need altering

This might be correct

Code:
Private Sub Text1_Change()
Me.List2.RowSource = "SELECT * FROM Table1 WHERE " & _
(Search("[Fname]", "Table1", Nz(Me.Text1,"")) And Search("[Contry]", "Table1", Nz(Me.Text2,"")))
Me.List2.Requery
End Sub
 
You haven't said what error you get, or used the method I posted to see the finished SQL.
 
honestly I say?
I want best and fast way to filter in too much data

About 32 Field and 98000 ID

I'm very grateful if anyone can give me a sample
 
That doesn't answer pbaldy's questions and so doesn't help us help you
 

Users who are viewing this thread

Back
Top Bottom