Matthew:
I'm currently using this code to do what you describe. It could be improved,
but it's a good starting place.
Put a button on the parent form, like mine, named cmdOfficesSubformView.
In the On_Click event, I call a proc named ToggleSiteOfficeView() and pass
the view state... datasheet or form.
I'm also reading/writing to the registry so the system remembers how the
user last viewed it and I call the ToggleSiteOfficeView at form open.
Hope this gets you started.
Private Sub cmdOfficesSubformView_Click()
On Error GoTo Err_Handler
With cmdOfficesSubformView
If .Caption = "View As Datasheet" Then
ToggleSiteOfficeView (acCmdSubformDatasheetView)
Else
ToggleSiteOfficeView (acCmdSubformFormView)
End If
End With
Exit_Here:
Exit Sub
Err_Handler:
msgbox Err.Description
Resume Next
End Sub
Private Sub ToggleSiteOfficeView(ByVal lngView As Long)
On Error GoTo Err_Handler
If lngView = acCmdSubformFormView Then
Me!SiteOfficesSubform.SetFocus
DoCmd.RunCommand lngView
SaveSetting "PTS", "User Prefs", "SiteOfficeView", acCmdSubformFormView
cmdOfficesSubformView.Caption = "View As Datasheet"
ElseIf lngView = acCmdSubformDatasheetView Then
Me!SiteOfficesSubform.SetFocus
DoCmd.RunCommand lngView
SaveSetting "PTS", "User Prefs", "SiteOfficeView", acCmdSubformDatasheetView
cmdOfficesSubformView.Caption = "View As Form"
Else
' do nothing
End If
Exit_Here:
Exit Sub
Err_Handler:
Select Case Err.Number
Case 2046
' The command or action 'SubformDatasheetView' isn't available now.
Case 3021
' No current record.
Case Else
msgbox Err.Description
End Select
Resume Next
End Sub