Form/datasheet view switch

L

Leslie Isaacs

Hello All

Access2K

I would like to be able to click on a button to swtich between form view and
datasheet view.
I assume that the main form would need to be a subform of a 'bigger' form,
so that my datasheet/form view switch button could be on the 'bigger' form
(otherwise the button obviously disappears when in datasheet view).
My problem is - what code to put behind the button to get it to make the
switch?
Incidentally, the main form also has a number of other buttons (primarily to
apply certain filters) and I would like to move these buttons also to the
'bigger' form so that they would be also visible. Would I need to change the
code behind those other buttons in order to get them to work on what would
become the subform?

Hope someone can help.

Many thanks
Leslie Isaacs
 
V

Van T. Dinh

See comments in-line.

--
HTH
Van T. Dinh
MVP (Access)


Leslie Isaacs said:
Hello All

Access2K

I would like to be able to click on a button to swtich between form view and
datasheet view.
I assume that the main form would need to be a subform of a 'bigger' form,
so that my datasheet/form view switch button could be on the 'bigger' form
(otherwise the button obviously disappears when in datasheet view).
My problem is - what code to put behind the button to get it to make the
switch?

DoCmd.RunCommand acCmdSubformDatasheetView

and

DoCmd.RunCommand acCmdSubformFormView

The Subform needs to have the Focus when the code is executed. This mean
that if the CommandButton is on the main (bigger) Form, you need to set the
Focus to the Subform (by code) before the above statements.




Incidentally, the main form also has a number of other buttons (primarily to
apply certain filters) and I would like to move these buttons also to the
'bigger' form so that they would be also visible. Would I need to change the
code behind those other buttons in order to get them to work on what would
become the subform?

Yes / most likely since CommandButtons will operate on the Subform /
RecordSource of the Subform, i.e. a different data object from their normal
context while they presently operate on the same data object / normal
context.
 
L

Leslie Isaacs

Van

Thank you for your reply.

I'm sure I'm being thick, but I can't get the code you gave me to work.

I have created a 2-button option group called [viewswitch] with the
following code behind:

Private Sub viewswitch_AfterUpdate()
Select Case Forms![frm_gp_work]![viewswitch]
Case 1
DoCmd.RunCommand acCmdSubformDatasheetView
Case 2
DoCmd.RunCommand acCmdSubformFormView
End Select
End Sub

But when I click either button I get 'Runtime error 2501 - the RunCommand
action was cancelled', then clicking debug highlights in yelly either the
case1 or the case2 expression (depending on which button was clicked).

Also, I can't get my other buttons (that apply the filters) to work - either
from the original (now sub-) form or from the 'larger' form.
They originally worked by running an 'apply filter' macro.

Thanks for the continued help.

Les
 
V

Van T. Dinh

The OptionGroup is probably in the main Form and therefore, the main Form
(as a data object) has the Focus, *not* the Subform. I wrote before that
you need to set the Focus to the Subform for the Command to run.

Try something like:

****Untested****
Private Sub viewswitch_AfterUpdate()
With Me.YourSubformControl.Form
.SetFocus
Select Case Me.[viewswitch]
Case 1
If .Form.CurrentView = 1 Then 'FormView
DoCmd.RunCommand acCmdSubformDatasheetView
End If
Case 2
If .Form.CurrentView = 2 Then 'DatasheetView
DoCmd.RunCommand acCmdSubformFormView
End If
End Select
End With
End Sub
****Code ends****

For the Filter to work in the Subform, you will need to use the Filter &
FilterOn Property of the Subform. The code should be something like:

****Untested****
With Me.YourSubformControl.Form
.Filter = {Your filter string}
.FilterOn = True
End With
****Snippet ends****
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top