I'm not sure what you're up to.
By creating the form object variable in your procedure, you're not looking at a subform -- at least not the way I think about it. I thought about trying to recreate your setup to debug it and explain what you're getting. But I think we should be clear we're talking about the same thing.
Suppose you have "frmMain", your main form, and "fsubForm" as your sub form.
On "frmMain" you create a button, name it "cmdChangeView". Then you create a subform object on "frmMain" (using the subform object from the Toolbox). If you use the Toolbox wizard to create the subform, it will automatically name the object "fsubForm". If you're not using the wizard, name it that.
Then your code in frmMain is:
Private Sub cmdChangeView_Click()
dim intCurrView as integer
intCurrView = me.fsubForm.Form.CurrentView
me.fsubForm.SetFocus
if intCurrView = acCurViewFormBrowse then
DoCmd.RunCommand acCmdsubformDatasheetView
Else
DoCmd.RunCommand acCmdsubformFormView
End If
End Sub
A couple of notes:
1) I don't add error trapping until I know the code works. Access error messages are more complete than the error traps I write. (That's not completely true. But the error trap routine I have puts a record in a table. I don't need to do that while I'm debugging code.)
2) I try to use the Ac constants (e.g. acCurViewFormBrowse) because they are more descriptive than "1".
FWIW, you're probably getting the error because of something like:
- your form object isn't visible
- your form isn't actually a subform
- you may actually be getting the error from the SetFocus statement (another reason not to do error trapping while you're debugging code)
- you should have either defined
Dim myForm as New Form
and did the set statement as is, or Dim the way you did and do
Set myForm = New ...
This may be the reason you're not able to SetFocus.
Any more debugging your code and I'm going to have to send you a bill. ;)
Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org