Solved

Search field on a form

Posted on 2016-11-28
7
42 Views
Last Modified: 2016-11-28
I have a form that has a subform.  On the main form's header I want to put a textbox field and when the user starts typing in the field, the subform starts to narrow down the possibilities.    The subform itself is a datasheet view form that has 7 fields.

How can this be done?
0
Comment
Question by:SteveL13
  • 4
  • 3
7 Comments
 
LVL 19
ID: 41904466
you can use the CHANGE event of a control to apply a filter to the subform
Private Sub MyFilterControl_Change()
'strive4peace
   With Me.subform_controlname.form
      If Len(ActiveControl.Text) > 0 Then
         '------------------------------------------ use ONE of these:
                   'this will filter for the beginning
         .Filter = "Left(FieldName," & Len(ActiveControl.Text) & ")="""   & ActiveControl.Text & """"
                   'this will filter for any part
         .Filter = "instr(Fieldname,""" & ActiveControl.Text & """) >0"
         '------------------------------------------
         .FilterOn = True
      Else
         .FilterOn = False
      End If
   End With
End Sub

Open in new window


WHERE
subform_controlname is the Name property of the subform control
FieldName is the name of the field you want to filter
-- to search all fields, use Fieldname1 & Fieldname2 & ... and convert if fields are not text

You probably want to reset the filter on the Current event of the mainform
0
 

Author Comment

by:SteveL13
ID: 41904489
This seems to work for just one field.  Here is my code:

Private Sub txtSearch_Change()
'strive4peace
   With Me.subfrmIntrepreters.Form
      If Len(ActiveControl.Text) > 0 Then
         .Filter = "Left(IntCity," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """"
         .FilterOn = True
      Else
         .FilterOn = False
      End If
   End With

End Sub

Open in new window


But I need it to filter on 7 different fields.  Is this possible?
0
 
LVL 19

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
ID: 41904506
search from beginning:
         .Filter = "Left(IntCity," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(Fieldname2," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(Fieldname3," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR ... and so on" 

Open in new window

for this, you probably also want to do this and substitute accordingly:
   dim iLen as integer
   iLen=Len(ActiveControl.Text) 

Open in new window

search any part:
.Filter = "instr(Fieldname1 & fieldname2 & fieldname3 & Fieldname4 & fieldname5 & fieldname6 & fieldname7 ,""" & ActiveControl.Text & """) >0"

Open in new window

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:SteveL13
ID: 41904540
I just entered this and it all turns red... ????

         .Filter = "Left(IntCity," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(InterpreterID," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(IntFullName," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(IntStreetNumber," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(IntAptOrOther," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(IntState," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR " _
           "Left(IntZipCode," & Len(ActiveControl.Text) & ")=""" _
            & ActiveControl.Text & """ OR "
0
 
LVL 19
ID: 41904569
sorry, someone came to the door so I just clicked Submit

everything should start with & ... ie: & "Left(IntStreetNumber," ...

take away the last OR

also, if you have fields that are stored as numbers then you will need to convert them
for instance, perhaps: format(InterpreterID,'#')
0
 

Author Closing Comment

by:SteveL13
ID: 41904572
This worked perfectly:

.Filter = "instr(Fieldname1 & fieldname2 & fieldname3 & Fieldname4 & fieldname5 & fieldname6 & fieldname7 ,""" & ActiveControl.Text & """) >0"
0
 
LVL 19
ID: 41904588
awesome!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

823 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