Textbox filtering a subform

pds8475

Registered User.
Local time
Today, 12:13
Joined
Apr 26, 2015
Messages
84
Hi
I have a form called ComponentBookOut
with a Textbox used for filtering records called ItemTxt
and a subform called Componentsubform
Which shows all records from the table COMPONENTS

I am using the following code to filter the records shown on the subform

Code:
 Private Sub ItemTxt_Change()
 
    Dim strFilter As String
    
    Me.Refresh
    
    strFilter = "ITEM like '*" & Me.ItemTxt & "*'"
     Forms!ComponentBookout!Componentsubform.Form.Filter = strFilter
    Forms!ComponentBookout!Componentsubform.Form.FilterOn = True
   
    Me.ItemTxt.SelStart = Nz(Len(Me.ItemTxt), 0)
    
End Sub

this works fine unless I want to use a space
i.e. I can search for 100v but not for 100v 1A and putting 100v1A finds no records.
how can I tell the textbox to allow the space character?

Any help would be appreciated
 
It makes a difference if you use the Text property of the ItemTxt textbox during the change event . . .
Code:
Private Sub ItemTxt_Change()
    Dim strFilter As String
    
    strFilter = "ITEM like '*" & Me.ItemTxt[COLOR="Red"].Text[/COLOR] & "*'"
    Forms!ComponentBookout!Componentsubform.Form.Filter = strFilter
    Forms!ComponentBookout!Componentsubform.Form.FilterOn = True
End Sub
The Textbox.Change event only changes the Text property of the control, not the .Value property.
 
Just tried what you suggested but it made no difference. I believe that my problem is being caused because access removes trailing white spaces and the change event runs after every key press. so as soon as I press space it gets deleted. I can type 100v1a then go back and add the space. changing it to 100v 1a. But doing so on every search would be frustrating. Surely there is a way to stop access removing the trailing white space.
 
I found an example of how to do it.

The code I needed was

Code:
 Option Compare Database
Option Explicit
Private blnSpace As Boolean
 Private Sub ItemTxt_Change()
     If blnSpace = False Then
        Dim strFilter As String
    
        Me.Refresh
    
        strFilter = "ITEM like '*" & Me.ItemTxt.Value & "*'" & " "
         Forms!ComponentBookout!Componentsubform.Form.Filter = strFilter
        Forms!ComponentBookout!Componentsubform.Form.FilterOn = True
   
        Me.ItemTxt.SelStart = Nz(Len(Me.ItemTxt), 0)
    End If
End Sub
Private Sub ItemTxt_KeyPress(KeyAscii As Integer)
     On Error GoTo err_handle
          
       If KeyAscii = 32 Then
            blnSpace = True
      Else
            blnSpace = False
       End If
          
                         
    Exit Sub
err_handle:
    Select Case Err.Number
          Case Else
          MsgBox "An unexpected error has occurred: " & vbCrLf & Err.DESCRIPTION & _
                vbCrLf & "Error " & Err.Number & "(" & Erl & ")"
   End Select
End Sub
 
What I prefer doing is splitting the SearchTxt based by the spaces and search for every piece individually.
This way I can search both for 100v 1A and 1A 100v
 

Users who are viewing this thread

Back
Top Bottom