Filter continuous Form (1 Viewer)

basilyos

Registered User.
Local time
Today, 02:27
Joined
Jan 13, 2014
Messages
252
hello guys


am filtering my continuous form usin this code


Code:
Dim str1 As String

str1 = "[Sanction_Number] LIKE " & Chr(34) & "*" & Me.Search.Text & "*" & Chr(34)

Form.Filter = str1
Form.FilterOn = True
Me.Search.Value = ""
Me.Search.SetFocus


but if the result is empty and try to search something else am getting this error


you can't reference a property or method for a control unless the control has the focus


i know that the problem is when the result is empty so the controls will not appear and then i cant refer to this hidden controls


so how to solve this problem


thanks
 

basilyos

Registered User.
Local time
Today, 02:27
Joined
Jan 13, 2014
Messages
252
thank you guys i find the solution


Code:
    Dim strFilter As String
    If Len(Trim(Me.Search.Value & vbNullString)) > 0 Then
        strFilter = "Sanction_Number Like '*" & _
            Replace(Me.Search.Value, "'", "''") & _
            "*'"
        Debug.Print strFilter
        Me.Filter = strFilter
        Me.FilterOn = True
        Me.Search.Value = ""
        Me.Search.SetFocus
    Else
        Me.FilterOn = False
        Me.Search.Value = ""
        Me.Search.SetFocus
    End If
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:27
Joined
Feb 28, 2001
Messages
26,996
Consider this:

Code:
        Me.Search.SetFocus
        Me.Search = ""

Since you get an error that TELLS you that you need to have focus on something to work with it, why would you NOT (just for safety) put the .SetFocus first. AND for any control that HAS a value, the .Value property is the default so you can omit it.

But the truth is, the value of a control is always available if it is a control that HAS a value at all, unless the control is unbound.
 

Users who are viewing this thread

Top Bottom