Solved

Access command button

Posted on 2014-11-28
5
269 Views
Last Modified: 2014-11-28
FormB displays records whose source is QueryA.
QueryA returns the following records sorted alphabetically on Field1:

Action
Add
After
Age
Back
Before
Build
Call
Clear

I have created command buttons on FormB captioned A,B,C,...one command button for each letter in alphabet.
How can I trigger an event so that when a command button is pressed (example: B), only records beginning with the letter "B" in Field1 are displayed in FormB?  Subsequently, I will want to press another command button, clear the form and display only those records where Field1 records begin with the letter corresponding with the caption of the command button pressed.
0
Comment
Question by:clock1
  • 3
  • 2
5 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40470816
Well, if you are going to do this with buttons, your best bet would be to create a single button, and set the caption property of that button to "A", and the property of the Click event of the button to:  =fnFilterForm()

Then you create a function in the forms code module that looks like:

Private Function fnFilterForm()

    Dim strFilter as string

     strFilter = "[Field1] LIKE '" & screen.activecontrol.Caption & "*'"
     me.filter = strFilter
     me.filterOn = true

End Function

Open in new window

Then you would copy that command button, once for each letter of the alphabet, and simply change the caption from "A" to the appropriate letter.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40470821
But personally, I prefer to either use a combo box, or a textbox to save screen realestate.  For a combo box, I would set the RowSourceType to ValueList (although I rarely do this), and set the RowSource to: A;B;C;D;E;...;Z.  Then I would use the combo boxes AfterUpdate event:
Private Sub cbo_Filter()

    Dim strFilter as string

     strFilter = "[Field1] LIKE '" & me.cbo_Filter & "*'"
     me.filter = strFilter
     me.filterOn = true

End Sub

Open in new window

When I need more flexability than filtering on just the first letter, generally add a textbox (txt_Filter) and a command button (cmd_Filter).  I then use the cmd_Filter_Click event

Private sub cmd_Filter_Click()

    Dim strFilter as string

     if me.txt_Filter & "" = "" then
        me.FilterOn = false
     Else
          strFilter = "[Field1] LIKE '" & me.txt_Filter & "*'"
          me.filter = strFilter
          me.filterOn = true
     end if

End Sub
0
 

Author Comment

by:clock1
ID: 40470827
Dale,

Access is throwing a compile error:

"Invalid use of Me keyword"
0
 

Author Comment

by:clock1
ID: 40470835
My error, forgot to declare this as a Private function
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40470839
You also need to put the function in FormB's code module, not in a shared (standard) code module.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

16 Experts available now in Live!

Get 1:1 Help Now