?
Solved

Access command button

Posted on 2014-11-28
5
Medium Priority
?
289 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 earned 2000 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
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
ID: 40470839
You also need to put the function in FormB's code module, not in a shared (standard) code module.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 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