Solved

Filtering data on a form based on a field on another form

Posted on 2013-06-13
5
338 Views
Last Modified: 2013-06-13
So I have this form, frmMembersSwitchBoard,  On this form there are three choices.
They are Department, Division and Duty Section.

When you click on Department a combo box appears with a list of departments you can choose from.  Once you choose the department the execute button appears.  When you click the execut button I would like it to open the form frmPersonnelRows and filter that form based on the department field chosen on the form frmMembersSwitchBoard

Thank you
John
0
Comment
Question by:John Sheehy
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 39245477
DoCmd.OpenForm "frmPersonnelRows", , , "DepartmentID = '" & Me.DepartmentID & "'"

Use that command on the Execute button click event.  Change the first DepartmentID to the name of the department ID field in the source table/query for frmPersonnelRows.  Change the second DepartmentID to the name of the department field on the frmMembersSwitchboard form.

Search VBA Help for OpenForm and take a look at the DoCmd.OpenForm method.

OM Gang
0
 

Author Comment

by:John Sheehy
ID: 39245530
That works great but I get a run time error if no record are assigned to a department.  The run time error is:  Run-time error '2501':
The OpenForm action was Canceled

Any thoughts?

John
0
 
LVL 28

Expert Comment

by:omgang
ID: 39245601
A couple.

1)  Capture the error and respond accordingly

2)  Use OpenArgs instead and move the filtering to the Form Open event of the frmPersonnelRows form


Here's some sample code for option 1

Private Sub cmdExecute_Click()
    On Error Resume Next
    DoCmd.OpenForm "frmPersonnelRows", , , "DepartmentID = '" & Me.DepartmentID & "'"

    If Err.Number = 2501 Then           'no existing record for department
        DoCmd.OpenForm "frmPersonnelRows"  'open form without filter
    Else
        GoTo Err_Click
    End If

Exit_cmdExecute_Click:
    Exit Sub

Err_Click:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_cmdExecute_Click

End Sub
0
 

Author Comment

by:John Sheehy
ID: 39245635
That did it.  Thank you.
0
 

Author Closing Comment

by:John Sheehy
ID: 39245640
The response was fast and accurate.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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…
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

708 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