Code to Change Current Form's view

G

Guest

I am an Access nOOb! (so please forgive my first (of many) stupid questions)
I have a database with several different ways of looking at the same data.
IE: reviewing parts by Branch or by Vendor. The detail information is the
same no matter how I am grouping it, but each way of looking at the data has
a different form built specifically for it.
I have an button on the form with an On_Click assigned to change the view to
datasheet. I have another On_Click assigned to a field that will change it
back to form view.
The problem is that I have to make 2 form-specific macros for each form...
which is kinda ridiculous. I want to be able to write code that basically
says to take the current form, whatever it is, and change the view to
whatever I specify.
Any help is appreciated!!
patrick
 
N

Nick 'The database Guy'

Hi Patrick

For a start it is good news that you want to write code and not just
reply on macros!

If you write a macro, then save it as code you can begin to discover
many methods and properties.

It always sticks in the memory better if you find it out yourself
rather than be told or cut and paste the solution.

I hope this helps.

Good luck,

Nick
 
R

RoyVidar

Patrick said:
I am an Access nOOb! (so please forgive my first (of many) stupid
questions) I have a database with several different ways of looking
at the same data. IE: reviewing parts by Branch or by Vendor. The
detail information is the same no matter how I am grouping it, but
each way of looking at the data has a different form built
specifically for it. I have an button on the form with an On_Click
assigned to change the view to datasheet. I have another On_Click
assigned to a field that will change it back to form view.
The problem is that I have to make 2 form-specific macros for each
form... which is kinda ridiculous. I want to be able to write code
that basically says to take the current form, whatever it is, and
change the view to whatever I specify.
Any help is appreciated!!
patrick

You could try something like the following in a standard module (in
VBE,
use Insert | Module)

Function ToggleView(frm As Form)
If frm.CurrentView = 1 Then ' 1 means form view
DoCmd.RunCommand acCmdDatasheetView
Else
DoCmd.RunCommand acCmdFormView
End If
End Function

This can be called, either with just typing

=ToggleView([Form])

in the event property of the button AND text controls on click event,
or
called with

Call ToggleView(Me)

within the on click event procedures of these controls.
 
G

Guest

I've not figured out how to save macros as code, yet (that will be next on my
list)... but I did find a solution for my issue:

Sub MyDSView()
Dim FrmName As String
FrmName = Screen.ActiveForm.Name
DoCmd.OpenForm FrmName, acFormDS
End Sub

Sub MyFrmView()
Dim FrmName As String
FrmName = Screen.ActiveForm.Name
DoCmd.OpenForm FrmName, acNormal
End Sub

Now I have a generic bit of code that I can use from any of my forms...
quite a relief! Thanks for responding and the offered encouragement, Nick!
patrick
 
G

Guest

Ooh! I like it!! Thanks Roy-Vidar!!

RoyVidar said:
Patrick said:
I am an Access nOOb! (so please forgive my first (of many) stupid
questions) I have a database with several different ways of looking
at the same data. IE: reviewing parts by Branch or by Vendor. The
detail information is the same no matter how I am grouping it, but
each way of looking at the data has a different form built
specifically for it. I have an button on the form with an On_Click
assigned to change the view to datasheet. I have another On_Click
assigned to a field that will change it back to form view.
The problem is that I have to make 2 form-specific macros for each
form... which is kinda ridiculous. I want to be able to write code
that basically says to take the current form, whatever it is, and
change the view to whatever I specify.
Any help is appreciated!!
patrick

You could try something like the following in a standard module (in
VBE,
use Insert | Module)

Function ToggleView(frm As Form)
If frm.CurrentView = 1 Then ' 1 means form view
DoCmd.RunCommand acCmdDatasheetView
Else
DoCmd.RunCommand acCmdFormView
End If
End Function

This can be called, either with just typing

=ToggleView([Form])

in the event property of the button AND text controls on click event,
or
called with

Call ToggleView(Me)

within the on click event procedures of these controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top