Solved

Search field on a form

Posted on 2016-11-28
7
12 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 18
Comment Utility
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
Comment Utility
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 18

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:SteveL13
Comment Utility
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 18
Comment Utility
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
Comment Utility
This worked perfectly:

.Filter = "instr(Fieldname1 & fieldname2 & fieldname3 & Fieldname4 & fieldname5 & fieldname6 & fieldname7 ,""" & ActiveControl.Text & """) >0"
0
 
LVL 18
Comment Utility
awesome!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ListBox on Access form 19 27
Combobox issue 4 25
Trying to open a report with a filter 2 16
Resize text 4 14
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now