Hi, Sam.
I wonder if there is such code that allows me to switch between Design View
and Form View using a command button?
As Allen mentioned, switching from Design View isn't available on the
current form. You could only switch from Form View to Design View with the
command button. To do so, just add the following code to the button's click
event:
RunCommand acCmdDesignView
Since control events don't occur while in Design View, your command button
would need to reside on _another_ form in order to switch a particular form
back to Form View from Design View. You don't mention which version of
Access you are using, but if this situation is acceptable (i.e., another form
needs to be open with the button visible while switching is desired), then
you could toggle the target form from Form View or Datasheet View to Design
View and back, with VBA code like the following:
' * * * * Start Code * * * *
Private Sub SwitchBtn_Click()
On Error GoTo ErrHandler
Dim accObj As AccessObject
Dim sFormName As String
sFormName = "frmOrderDetails"
Set accObj = CurrentProject.AllForms(sFormName)
If (accObj.IsLoaded) Then
Select Case accObj.CurrentView
Case acCurViewDesign
DoCmd.OpenForm sFormName, acNormal
Case acCurViewFormBrowse
DoCmd.OpenForm sFormName, acDesign
Case acCurViewFormBrowse, acCurViewDatasheet
DoCmd.OpenForm sFormName, acDesign
Case Else
' Do nothing.
End Select
Else
DoCmd.OpenForm sFormName, acNormal
End If
CleanUp:
Set accObj = Nothing
Exit Sub
ErrHandler:
MsgBox "Error in SwitchBtn_Click( ) in " & vbCrLf & Me.Name & _
" form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp
End Sub
' * * * * End Code * * * *
This code won't work in Access 97.
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips.
(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that the first and
best answers are often given to those who have a history of rewarding the
contributors who have taken the time to answer questions correctly.