How to switch views using command button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I wonder if there is such code that allows me to switch between Design View
and Form View using a command button?

Regards,
Sam
 
No.
Command buttons do not work in design view.
That's probably what you would expect, because it's Design view.
 
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.
 
Sorry. I'm sitting in the Department of Redundancy Department this morning.
I accidentally pasted in too much code. My first suggestion will still work,
but the following is better, since it leaves out the duplication:

' * * * * 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, 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 * * * *

Again, 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.)
 
Thanks Allen. I will probably just make a command button that opens the
Design View (with the code from Gunny). Then enables the toolbar on top so
that the designer can switch back to Form View by using Access's default
toolbar. But thanks for the explaination :)

Regards,
Sam
 
Excellent. Thanks Gunny.
I think I will probably just have a button that opens the Design View and
displays Access's built-in toolbar on the top as well. So that the designer
can use the toolbar to get back to Form View, and save me,in my particular
case, to design another form just to accomodate the
switching-back-to-FormView function. But I'm sure your generous suggestion
would come handy one day for me :)

Regards,
Sam
 
Back
Top