Toggle between Form and DataSheet view at runtime

A

Alex

I have a form where I want the user to be able to click on a checkbox to toggle
between DataSheet and Single Form views. Unfortunately when the code executes,
I get run-time error 2136: "To set this property, open the form or report in
Design view". How can I change the DefaultView at run-time? The click event
for the form is as follows:

Private Sub CheckBoxViewList_Click()

If Me.CheckBoxViewList Then
Me.DefaultView = 2 ' DataSheet
Else
Me.DefaultView = 0 ' Single Form
End If

End Sub
 
D

Dennis

Try this instead

Private Sub CheckBoxViewList_Click()

If Me.CheckBoxViewList Then
RunCommand acCmdDatasheetView ' DataSheet
Else
RunCommand acCmdFormView ' Single Form
End If

End Sub
 
A

Alex

Thank you Dennis. Your code takes care of the problem I was having, but now I
have encountered another issue. I had put the checkbox for toggling between
Single Form and DataSheet modes in the form's header, but the header disappears
when in DataSheet mode. I want to use a control on the form as opposed to a
menu item to make it very evident to the user how to switch modes.
 
D

Dennis

The way I did it was to put this in the form Click event
RunCommand acCmdFormView

If you click on the record selector or column headers when in datasheet
view, it will return to form view. I provided a help file with this
explained. You may have to instruct your users on this functionality if you
do not provide a help file.
 
J

Joan Wild

Make your form a subform on an unbound main form. You can then have the
checkbox on the mainform that toggles the view.
 

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