Code to open a swithboard page

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

Guest

I have created a switchboard using the switchboard manager. I know that
using an unbound form would be better, but I have already built a fairly
complex switchboard and would rather not start over using forms if I don't
have to. What I am trying to do is have two actions occur upon clicking a
button on the switchboard menu. I need to open another switchboard page and
open a form as well. I tried to open a form first and then write an event
procedure to open the desired switchboard page upon the opening of the form
but could not figure out the code (I am new at code and don't know very much
about it). I also tried to run a macro from the switchboard that would open
both the form and the desired switchboard page, but could not figure that out
either. Any guidance would be appreciated.

Thanks
 
So you want to press an option on the Switchboard form,
have the Switchboard switch to display a different page of
options, and finally open a form. Correct?

Lots of ways you could so this, but here is one that will work.

I just modified some instructions I have posted here:

http://home.bendbroadband.com/conradsystems/accessjunkie/switchboardfaq.html#specificmenu

(Watch out for any line wrapping on that link)

This is just a generic illustration; you will need to adapt to
your specific needs.

Create a function (has to be a function) in a new standard module like so:

Public Function OpenFormFromSBM()
On Error GoTo ErrorPoint

Forms!Switchboard.Filter = "[ItemNumber] = 0 " _
& "And [SwitchboardID] = 2"
Forms!Switchboard.Refresh
DoCmd.OpenForm "Form1"

ExitPoint:
Exit Function

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " _
& Err.Description, vbExclamation, _
"Unexpected Error"
Resume ExitPoint

End Function

Then launch the wizard again, and select the Run Code option.
In the Function Name area enter this:

OpenFormFromSBM

Nothing else, just like that.

Hitting that button on the Switchboard will change the form to the second page
(you may need to adjust that in the code) and then open a form called Form1.

Hope that helps,
--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
 
I finally located your original post. Thank you for your help. I have
entered the code you gave me and made a few small changes. The code I
entered is as follows:

Public Function OpenFormFromSBM()
On Error GoTo ErrorPoint

Forms!Switchboard.Filter = "[ItemNumber] = 0 " _
& "And [SwitchboardID] = 9"
Forms!Switchboard.Refresh
DoCmd.OpenForm "Form1"

ExitPoint:
Exit Function

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " _
& Err.Description, vbExclamation, _
"Unexpected Error"
Resume ExitPoint

End Function

My form is named Form1. I saved the module as OpenFormFromSBM. In the
switchboard manager, I entered the command as Run Code and typed
OpenFormFromSBM in the function name box. Now when I click the button in the
switchboard window, it gives an error message, "There was an error executing
the command."

I tried deleting the code "Option Compare Database" that was automatically
entered into the module at the top upon the creation of a new module, but it
still results in the same error message. Please advise. Thank you again for
your assistance.
--
Rywm


Jeff Conrad said:
So you want to press an option on the Switchboard form,
have the Switchboard switch to display a different page of
options, and finally open a form. Correct?

Lots of ways you could so this, but here is one that will work.

I just modified some instructions I have posted here:

http://home.bendbroadband.com/conradsystems/accessjunkie/switchboardfaq.html#specificmenu

(Watch out for any line wrapping on that link)

This is just a generic illustration; you will need to adapt to
your specific needs.

Create a function (has to be a function) in a new standard module like so:

Public Function OpenFormFromSBM()
On Error GoTo ErrorPoint

Forms!Switchboard.Filter = "[ItemNumber] = 0 " _
& "And [SwitchboardID] = 2"
Forms!Switchboard.Refresh
DoCmd.OpenForm "Form1"

ExitPoint:
Exit Function

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " _
& Err.Description, vbExclamation, _
"Unexpected Error"
Resume ExitPoint

End Function

Then launch the wizard again, and select the Run Code option.
In the Function Name area enter this:

OpenFormFromSBM

Nothing else, just like that.

Hitting that button on the Switchboard will change the form to the second page
(you may need to adjust that in the code) and then open a form called Form1.

Hope that helps,
--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
I have created a switchboard using the switchboard manager. I know that
using an unbound form would be better, but I have already built a fairly
complex switchboard and would rather not start over using forms if I don't
have to. What I am trying to do is have two actions occur upon clicking a
button on the switchboard menu. I need to open another switchboard page and
open a form as well. I tried to open a form first and then write an event
procedure to open the desired switchboard page upon the opening of the form
but could not figure out the code (I am new at code and don't know very much
about it). I also tried to run a macro from the switchboard that would open
both the form and the desired switchboard page, but could not figure that out
either. Any guidance would be appreciated.
 
in message:

Hi Rywm,

Comments in-line....
I finally located your original post. Thank you for your help.

No problem.
I have entered the code you gave me and made a few small changes.
The code I entered is as follows:
Public Function OpenFormFromSBM()
On Error GoTo ErrorPoint

Forms!Switchboard.Filter = "[ItemNumber] = 0 " _
& "And [SwitchboardID] = 9"
Forms!Switchboard.Refresh
DoCmd.OpenForm "Form1"

ExitPoint:
Exit Function

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " _
& Err.Description, vbExclamation, _
"Unexpected Error"
Resume ExitPoint

End Function

That code should be just perfect, don't change a thing.
My form is named Form1. I saved the module as OpenFormFromSBM.

<<sound of screeching brakes in the background>>
Whoa, wait a minute!
The names of any modules cannot be the same as any existing
functions or procedures in your database.
That is why you are getting this error....
In the switchboard manager, I entered the command as Run Code and typed
OpenFormFromSBM in the function name box. Now when I click the button in the
switchboard window, it gives an error message, "There was an error executing
the command."

Access is confused. It sees a function name and the name of the module
as being the same. Access is telling you "Tilt!! I don't know what to do."

Rename the module itself to something like basSBMCode. It can
be basically anything, just not the same as any functions or procedures
in your database.
I tried deleting the code "Option Compare Database" that was automatically
entered into the module at the top upon the creation of a new module, but it
still results in the same error message. Please advise. Thank you again for
your assistance.

No, you definitely do *not* want to delete that line. Put it back in. In fact,
you should leave these two lines in all of your code windows at the top.

Option Compare Database
Option Explicit

For an explanation of why, see this page:
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions

Rename the module, compile your database, and then retest.
All should be good then.
 
Jeff-

I did as suggested and it worked perfectly. Thank you for the code,
guidance, and links to further explanations to help me learn. I really
appreciate your help. Have a great day.
--
Rywm


Jeff Conrad said:
in message:

Hi Rywm,

Comments in-line....
I finally located your original post. Thank you for your help.

No problem.
I have entered the code you gave me and made a few small changes.
The code I entered is as follows:
Public Function OpenFormFromSBM()
On Error GoTo ErrorPoint

Forms!Switchboard.Filter = "[ItemNumber] = 0 " _
& "And [SwitchboardID] = 9"
Forms!Switchboard.Refresh
DoCmd.OpenForm "Form1"

ExitPoint:
Exit Function

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " _
& Err.Description, vbExclamation, _
"Unexpected Error"
Resume ExitPoint

End Function

That code should be just perfect, don't change a thing.
My form is named Form1. I saved the module as OpenFormFromSBM.

<<sound of screeching brakes in the background>>
Whoa, wait a minute!
The names of any modules cannot be the same as any existing
functions or procedures in your database.
That is why you are getting this error....
In the switchboard manager, I entered the command as Run Code and typed
OpenFormFromSBM in the function name box. Now when I click the button in the
switchboard window, it gives an error message, "There was an error executing
the command."

Access is confused. It sees a function name and the name of the module
as being the same. Access is telling you "Tilt!! I don't know what to do."

Rename the module itself to something like basSBMCode. It can
be basically anything, just not the same as any functions or procedures
in your database.
I tried deleting the code "Option Compare Database" that was automatically
entered into the module at the top upon the creation of a new module, but it
still results in the same error message. Please advise. Thank you again for
your assistance.

No, you definitely do *not* want to delete that line. Put it back in. In fact,
you should leave these two lines in all of your code windows at the top.

Option Compare Database
Option Explicit

For an explanation of why, see this page:
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions

Rename the module, compile your database, and then retest.
All should be good then.
--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html
 
Back
Top