Solved

Access command button

Posted on 2014-11-28
5
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 48

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 48

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 48

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

691 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