Programmatically Change Form Views (1 Viewer)

gray

Registered User.
Local time
Today, 02:07
Joined
Mar 19, 2007
Messages
578
Hi All

WinXPPro
Access 2002 SP3

I am hiding the menubars from my users so they can't see the normal 'View' options. I'd like to provide them with a button which would swap a subform from Datasheet view to Form view.

Anyone know if / how this can be done? My research hasn't revealed an answer? Nothing new there though :). I can see how to set the defaulf view, allow or disallow views but can't see a property that I can use to force the swap.

Thanks
 

Rabbie

Super Moderator
Local time
Today, 02:07
Joined
Jul 10, 2007
Messages
5,906
How are you planning to have a button in Datasheet view?
 

Rabbie

Super Moderator
Local time
Today, 02:07
Joined
Jul 10, 2007
Messages
5,906
In the onclick event for for the button

use DoCmd.RunCommand(acCmdDatasheetView) to switch to Datasheet view

Use DoCmdRuncommand(acCmdFormView) to switch to Form View

See Access/VBA Help for more info
 
Last edited:

missinglinq

AWF VIP
Local time
Yesterday, 21:07
Joined
Jun 20, 2003
Messages
6,423
As we say here down South, that second dog

DoCmdRuncommand(acCmdFormView)

ain't going to hunt, Rabbie! You left out the dot between DoCmd and RunCommand.

Also, your point in your first response still stands; you can't display command buttons in Datasheet View; I'd use the Double-Click event of one or more textboxes.

Code:
Private Sub AnyTextbox_DblClick(Cancel As Integer)
  DoCmd.RunCommand (acCmdFormView)
End Sub

To keep from confusing the end users, I'd use the event in both form views.
 

gray

Registered User.
Local time
Today, 02:07
Joined
Mar 19, 2007
Messages
578
Hi Chaps

Well, thanks for the prompt replies!... appreciated!

To address the point about the button...I have a form/subform arrangement and was intending to site the switcheroo button on the main form.. it would switch the subform view...

You've given me something to work with here if I can suss the syntax for addressing the subform.

Thanks again
 

gray

Registered User.
Local time
Today, 02:07
Joined
Mar 19, 2007
Messages
578
Hi

Well..... I thought it looked too easy... :)

I coded the following in one of my textbox's (dblClick event) in my SubForm :-

If Me.CurrentView = 1 Then
DoCmd.RunCommand (acCmdDatasheetView)
ElseIf Me.CurrentView = 2 Then
DoCmd.RunCommand (acCmdFormView)
End If

When the code runs however, I get the following error:-

Run-time error '2046':

the command or action 'Formview' isn't available now


or, equally:

the command or action 'Datasheetview' isn't available now


I have set the subform properties to allow both.

Also when I fire the form up (unlike my users) I have the option for View: Subform: datasheet or form and both work perfectly? Since Access appears capable of re-presenting the view I assume it's the vb command I am using (above)?

As an alternative, I tried completely closing and re-opening the subform in alternate views but this appears to lose the parent/child links.

Any ideas anyone?

Thanks
 

gray

Registered User.
Local time
Today, 02:07
Joined
Mar 19, 2007
Messages
578
Hi

Cracked it! It now works perfectly.

Here's what I did for anyone else struggling with this.

I added a command button to my main form and in the on-click event I added the following code.

Private Sub Your_Command_Button_Click()
Me.Your_SubForm.SetFocus

Select Case Me.CurrentView
Case 1
'Currently in form view
RunCommand acCmdSubformDatasheet
Case 2
'Currently in datasheet view
RunCommand acCmdSubformFormView
Case Else
'Must be design view (0) or some as yet undefined view
'So do nothing.
End Select


I found this code on

www.pcreview.co.uk/forums/thread-1157639.php

So thanks to them for posting it up.

rgds
 

HairyArse

Registered User.
Local time
Today, 02:07
Joined
Mar 31, 2005
Messages
92
I came across this thread as I was struggling to get the acCmdSubformDatasheet command to work as I constantly got the errors:

Run-time error '2046':
the command or action 'Formview' isn't available now

For anyone in the same situation, you need to ensure that on your Form Properties, under the Format Tab, that first of all, the Allow Datasheet View property is set to Yes, and that one of Allow Form View or Allow Layout is set to No as you can't set all of them to Yes.
 

Users who are viewing this thread

Top Bottom