How to Open a Form from Switchboard and Add Filtering

whdyck

Registered User.
Local time
Today, 11:18
Joined
Aug 8, 2011
Messages
169
I'm using Access 2003.

I have a form I'd like to open from two Switchboard items. In one case, I want it just to open with a default filter I have defined in the form's Open event. In the other I want to apply a different filter.

Is this possible? Can I add something to the form name in the Switchboard's Argument field to apply the special filter?

Thanks for any help you can give.

Wayne
 
Option 1 would be to duplicate the form and apply the second filter.

Option 2 would be to adjust the switchboard code to pass an argument to the form so it knows which filter to apply.
 
Option 1 would be to duplicate the form and apply the second filter.

Option 2 would be to adjust the switchboard code to pass an argument to the form so it knows which filter to apply.

Could you please elaborate on how I would implement Option 2?

In the interests of not duplicating code unnecessarily, I'd prefer not to use Option 1.

Thanks.

Wayne
 
I should add that I know there is a Switchboard argument where I would enter the name of the form to open, but can I just add more to that argument string to tell it which filter to apply?

Thanks.

Wayne
 
All you have to do is when opening the Second From use the OpeArgs property.. On opening the Form, check the OpenArgs.. What I am saying is, you have the SwitchBoard button to open a Form? in that button event,
Code:
Private Sub formBtn_Click()
    DoCmd.OpenForm theFormName, _
    OpenArgs :="someValue"
End Sub
On open of the Second Form, you have to check the openArgs..
Code:
Private Sub Form_Open(Cancel As Integer)
    If Me.OpenArgs = "someValue" Then
        'DoSomething
    End If
End Sub
 
All you have to do is when opening the Second From use the OpeArgs property.. On opening the Form, check the OpenArgs.. What I am saying is, you have the SwitchBoard button to open a Form? in that button event,
Code:
Private Sub formBtn_Click()
    DoCmd.OpenForm theFormName, _
    OpenArgs :="someValue"
End Sub
On open of the Second Form, you have to check the openArgs..
Code:
Private Sub Form_Open(Cancel As Integer)
    If Me.OpenArgs = "someValue" Then
        'DoSomething
    End If
End Sub

In the default Switchboard that Access creates, the buttons have the following in the On Click event:
Code:
 =HandleButtonClick(n)
where n = the order of the Switchboard button clicked.

So if I understand you correctly, I need to add some code in function HandleButtonClick so say something like, "if the particular form just opened is the one I'm interested in, then OpenArgs := 'some value'

Does that sound right?

Thanks again.

Wayne
 
Yes, go into the Code window and in which you will have the event, look under the Case corresponding to the order.. and add the open args argument to the OpenForm method..
 
Instead, could I just select "Run Code" in the Switchboard Manager, then write a Module function that opens the form and sets the args, as you suggested?

That way, I wouldn't have to change the Switchboard form's code. (Seems like it might be a bit cleaner that way.)

Thanks.

Wayne
 
I never have personally used SwitchBoards in my applications so I am not sure if that is the best way... but this is how we learn.. so take your chance.. see what happens when you implement what you have proposed.. I am sure someone will be able to guide you exactly what you are after. Sorry I could not be of much help here..
 
In the switchboard code find this Function
HandleButtonClick

Then find these lines to open a form
Case conCmdOpenFormBrowse
DoCmd.OpenForm rs![Argument]

Now you have a few ways to do this using the arguments of the DoCmd.OpenForm method;
Filter argument. You pass the name of a save filter for the form to apply. If the different filter will not change then this is the easiest method. With the form open in normal view (ie showing records), use Records>>Filter>>Advanced Filter/Sort from the tool bar to create the filter and then SAVE it. Use this then as the filter argument.

Where argument. You could provide the details of the filter in the WHERE argument.

OpenArgs argument. Provide a value for this to be passed to the form. Then in the forms Open Event code check for the OpenArgs value and actions accordingly. EG you could pass “normal” to indicate normal filter and “different” to indicate different filter. If “different” you provide the details for the filtering here.
 
In addition to changing the code in the switchboard, I have also added columns to the Switchboard Items table. Of course the built-in wizard won't populate the new columns so you will have to do it manually but you could use them to provide an argument that identifies a filter. Rather than modifying the code in the switchboard, I woul have the switchboard open a function and the function would know how to make use of the argument to use the OpenForm using the desired arguments. The problem with customizing the code in the switchboard is that it will apply to all similar operations so every OpenForm action will be impacted not just the ones you want to customize. Using the function method, you can use functions when you want to customize the code and the OpenForm argument when you don't.
 
Filter argument. You pass the name of a save filter for the form to apply. If the different filter will not change then this is the easiest method.

Actually, when the form opens, the Form_Open event sets the default filter, so this won't work. However, you gave me some other useful tips.

Thanks.

Wayne
 
Rather than modifying the code in the switchboard, I woul have the switchboard open a function and the function would know how to make use of the argument to use the OpenForm using the desired arguments. The problem with customizing the code in the switchboard is that it will apply to all similar operations so every OpenForm action will be impacted not just the ones you want to customize. Using the function method, you can use functions when you want to customize the code and the OpenForm argument when you don't.

Yes, that's what I did and it worked. I call a function from the Switchboard, which sets the OpenArgs. Then in the the Form_Open event, if the OpenArgs parm indicates so, I use the custom filter rather than the default one the form would normally use.

Thanks.

Wayne
 

Users who are viewing this thread

Back
Top Bottom