Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the view of an MS Access subform at runtime in VBA code?

Tags:

vba

ms-access

This seems like it would be a simple affair, and I am sure I have done this before, but it has been a while since I have done any UI programming in Access. What I need to do is put a button on a form to toggle between datasheet and form view for a subform.

I have found a defaultview property, but nothing that looks like it would toggle the view of the form after it is already open.

Essentially I need the property I can fill in the following code..

sfEmployeeBatchEntry.Form.??? = acFormDS
like image 625
JohnFx Avatar asked Mar 25 '09 15:03

JohnFx


People also ask

How do you change a subform to a Datasheet view?

If the subform control is not currently selected, click it once to select it. On the Data tab of the property sheet, click the Source Object drop-down list, and then click the table or query that you want to display in the datasheet. For example, if you want to display data from the Orders table, click Table.

How do you reference a control on a subform in Access VBA?

To refer to a control on a subform, use the Form property to tell Access that you are referring to the subform as a form, not as a control. Using the Form property gets you into the subform. refers to the ctlStateTax control on the subfrmOrders subform or the frmCustomer form.

How do you edit a subform?

Modify a SubformDisplay the subform in Design View. Access displays the form in Design View. Click anywhere in the subform to modify it. Edit the subform as needed.


1 Answers

I found it on my own. I was missing it because it used the silly and clunky RunCommand syntax instead of a simple property or method on the control or form classes.

It ain't pretty, but for posterity, here is the answer.

'You have to set focus to the subform control or the change view call will'
'fail (UGH!)'
MyForm.mySubFormControl.SetFocus

'Change to datasheet view...'
DoCmd.RunCommand acCmdSubformDatasheet

'Change to Form View...'
DoCmd.RunCommand acCmdSubformFormView
like image 172
JohnFx Avatar answered Nov 12 '22 13:11

JohnFx