how do I run code from switchboard button

P

Pen

in the switchboard manager. if I enter the function name xxx() in the
Function field after selecting the command:"run code" and then try to run the
code by clicking on the appropriate button, Access returns an error.
if I enter the function name xxx() in the find case code instead of
rs![argument] then it all works fine. Unsatisfactory and of course this would
only work if this was the only function I would be using.
if I use the debugger, the variable as object rs![argument] has the value of
the string representing the function I want to run.
It looks like the application.run command used to run the code is seeing the
object rs![argument] as a litteral string and not as a variable
 
P

Pen

No that doesn't work. The code that is in question is below
as you can see the "select case" for run code (8) is using the variable
rs![argument]
but the syntax for run or Application.run is
{run[string,variant,...variant]} this would mean that the case select 8
condition is trying to run rs![argument] instead of run the function
contained in rs![argument]
the error returned is 2517


Private Function HandleButtonClick(intBtn As Integer)
' This function is called when a button is clicked.
' intBtn indicates which button was clicked.

' Constants for the commands that can be executed.
Const conCmdGotoSwitchboard = 1
Const conCmdOpenFormAdd = 2
Const conCmdOpenFormBrowse = 3
Const conCmdOpenReport = 4
Const conCmdCustomizeSwitchboard = 5
Const conCmdExitApplication = 6
Const conCmdRunMacro = 7
Const conCmdRunCode = 8
Const conCmdOpenPage = 9

' An error that is special cased.
Const conErrDoCmdCancelled = 2501

Dim con As Object
Dim rs As Object
Dim stSql As String
On Error GoTo HandleButtonClick_Err

' Find the item in the Switchboard Items table
' that corresponds to the button that was clicked.
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")
stSql = "SELECT * FROM [Switchboard Items] "
stSql = stSql & "WHERE [SwitchboardID]=" & Me![SwitchboardID] & " AND
[ItemNumber]=" & intBtn
rs.Open stSql, con, 1 ' 1 = adOpenKeyset

' If no item matches, report the error and exit the function.
If (rs.EOF) Then
MsgBox "There was an error reading the Switchboard Items table."
rs.Close
Set rs = Nothing
Set con = Nothing
Exit Function
End If

Select Case rs![Command]

' Go to another switchboard.
Case conCmdGotoSwitchboard
Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" &
rs![Argument]

' Open a form in Add mode.
Case conCmdOpenFormAdd
DoCmd.OpenForm rs![Argument], , , , acAdd

' Open a form.
Case conCmdOpenFormBrowse
DoCmd.OpenForm rs![Argument]

' Open a report.
Case conCmdOpenReport
DoCmd.OpenReport rs![Argument], acPreview

' Customize the Switchboard.
Case conCmdCustomizeSwitchboard
' Handle the case where the Switchboard Manager
' is not installed (e.g. Minimal Install).
On Error Resume Next
Application.Run "ACWZMAIN.sbm_Entry"
If (Err <> 0) Then MsgBox "Command not available."
On Error GoTo 0
' Update the form.
Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
Me.Caption = Nz(Me![ItemText], "")
FillOptions

' Exit the application.
Case conCmdExitApplication
CloseCurrentDatabase

' Run a macro.
Case conCmdRunMacro
DoCmd.RunMacro rs![Argument]

' Run code.
Case conCmdRunCode
Application.Run rs![Argument]

' Open a Data Access Page
Case conCmdOpenPage
DoCmd.OpenDataAccessPage rs![Argument]

' Any other command is unrecognized.
Case Else
MsgBox "Unknown option."

End Select

' Close the recordset and the database.
rs.Close

HandleButtonClick_Exit:
On Error Resume Next
Set rs = Nothing
Set con = Nothing
Exit Function

HandleButtonClick_Err:
' If the action was cancelled by the user for
' some reason, don't display an error message.
' Instead, resume on the next line.
If (Err = conErrDoCmdCancelled) Then
Resume Next
Else
MsgBox "There was an error executing the command." & Err &
rs![Argument], vbCritical
Resume HandleButtonClick_Exit
End If

End Function
Function testcode()
MsgBox "the code executed ok", vbExclamation

End Function

Pen said:
in the switchboard manager. if I enter the function name xxx() in the
Function field after selecting the command:"run code" and then try to run the
code by clicking on the appropriate button, Access returns an error.
if I enter the function name xxx() in the find case code instead of
rs![argument] then it all works fine. Unsatisfactory and of course this would
only work if this was the only function I would be using.
if I use the debugger, the variable as object rs![argument] has the value of
the string representing the function I want to run.
It looks like the application.run command used to run the code is seeing the
object rs![argument] as a litteral string and not as a variable
 
P

Pen

Here's a strange thing.
if I replace the variable rs![argument] so the cmd reads
application.run testcode()
the code runs ok but then the error handler still returns the error

Pen said:
No that doesn't work. The code that is in question is below
as you can see the "select case" for run code (8) is using the variable
rs![argument]
but the syntax for run or Application.run is
{run[string,variant,...variant]} this would mean that the case select 8
condition is trying to run rs![argument] instead of run the function
contained in rs![argument]
the error returned is 2517


Private Function HandleButtonClick(intBtn As Integer)
' This function is called when a button is clicked.
' intBtn indicates which button was clicked.

' Constants for the commands that can be executed.
Const conCmdGotoSwitchboard = 1
Const conCmdOpenFormAdd = 2
Const conCmdOpenFormBrowse = 3
Const conCmdOpenReport = 4
Const conCmdCustomizeSwitchboard = 5
Const conCmdExitApplication = 6
Const conCmdRunMacro = 7
Const conCmdRunCode = 8
Const conCmdOpenPage = 9

' An error that is special cased.
Const conErrDoCmdCancelled = 2501

Dim con As Object
Dim rs As Object
Dim stSql As String
On Error GoTo HandleButtonClick_Err

' Find the item in the Switchboard Items table
' that corresponds to the button that was clicked.
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")
stSql = "SELECT * FROM [Switchboard Items] "
stSql = stSql & "WHERE [SwitchboardID]=" & Me![SwitchboardID] & " AND
[ItemNumber]=" & intBtn
rs.Open stSql, con, 1 ' 1 = adOpenKeyset

' If no item matches, report the error and exit the function.
If (rs.EOF) Then
MsgBox "There was an error reading the Switchboard Items table."
rs.Close
Set rs = Nothing
Set con = Nothing
Exit Function
End If

Select Case rs![Command]

' Go to another switchboard.
Case conCmdGotoSwitchboard
Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" &
rs![Argument]

' Open a form in Add mode.
Case conCmdOpenFormAdd
DoCmd.OpenForm rs![Argument], , , , acAdd

' Open a form.
Case conCmdOpenFormBrowse
DoCmd.OpenForm rs![Argument]

' Open a report.
Case conCmdOpenReport
DoCmd.OpenReport rs![Argument], acPreview

' Customize the Switchboard.
Case conCmdCustomizeSwitchboard
' Handle the case where the Switchboard Manager
' is not installed (e.g. Minimal Install).
On Error Resume Next
Application.Run "ACWZMAIN.sbm_Entry"
If (Err <> 0) Then MsgBox "Command not available."
On Error GoTo 0
' Update the form.
Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
Me.Caption = Nz(Me![ItemText], "")
FillOptions

' Exit the application.
Case conCmdExitApplication
CloseCurrentDatabase

' Run a macro.
Case conCmdRunMacro
DoCmd.RunMacro rs![Argument]

' Run code.
Case conCmdRunCode
Application.Run rs![Argument]

' Open a Data Access Page
Case conCmdOpenPage
DoCmd.OpenDataAccessPage rs![Argument]

' Any other command is unrecognized.
Case Else
MsgBox "Unknown option."

End Select

' Close the recordset and the database.
rs.Close

HandleButtonClick_Exit:
On Error Resume Next
Set rs = Nothing
Set con = Nothing
Exit Function

HandleButtonClick_Err:
' If the action was cancelled by the user for
' some reason, don't display an error message.
' Instead, resume on the next line.
If (Err = conErrDoCmdCancelled) Then
Resume Next
Else
MsgBox "There was an error executing the command." & Err &
rs![Argument], vbCritical
Resume HandleButtonClick_Exit
End If

End Function
Function testcode()
MsgBox "the code executed ok", vbExclamation

End Function

Pen said:
in the switchboard manager. if I enter the function name xxx() in the
Function field after selecting the command:"run code" and then try to run the
code by clicking on the appropriate button, Access returns an error.
if I enter the function name xxx() in the find case code instead of
rs![argument] then it all works fine. Unsatisfactory and of course this would
only work if this was the only function I would be using.
if I use the debugger, the variable as object rs![argument] has the value of
the string representing the function I want to run.
It looks like the application.run command used to run the code is seeing the
object rs![argument] as a litteral string and not as a variable
 
P

Pen

Hi Jeff
thanks for your reply
I am running access 2002 service pack 3
I run the same at work and have the same reaction there
the only thing I can do is call a macro and run the code from the macro,
this works fine but is way too untidy for me
Hope you can come up with something
regards
Pen

Jeff Conrad said:
Hi Pen,

It works just fine for me in Access 97. The form code is exactly the same as well. Strange that it
does not work for you. What version of Access are you using? I'll do some testing at home tonight
when I can have 97-2003 all in front of me.

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
Access 2007 Info: http://www.AccessJunkie.com

in message:
Here's a strange thing.
if I replace the variable rs![argument] so the cmd reads
application.run testcode()
the code runs ok but then the error handler still returns the error
 
J

Jeff Conrad

Ok, this is really weird.

I tested the exact same procedures using Access 97, 2000, 2002, and 2003 on my main machine at home
and it worked every time with no error messages. I created a public function:

Public Function DoSomething()
MsgBox "Hello"
End Function

I put DoSomething in the SBM wizard entry and no problems for all four versions. The weird thing is
I tried this late yesterday on a machine with only 2003 and it failed just as you observed.

I'm a little clueless here at the moment! Only suggestions I have at the moment are:

1. Try importing all your database objects into a new container *without* the SBM form and table.
2. Compile the code in the container and then compact.
3. Have the SBM rebuild a new form in the new container
4. Test it out on a sample function. Make sure it is declared as Public.

You might try going to Office Update to make sure you have all the latest service packs and updates.

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
Access 2007 Info: http://www.AccessJunkie.com

in message:
 
P

Pen

Hi again jeff
Er I'm just a novice with both vbasic and Access so most of what you suggest
passed right over my head. I am just about happy with the more intuitive
aspects of the two systems but a lot of the operations and code are out of my
reach at the moment. I'll try to do what you suggest but be prepared for more
shouts for help on the website.
I am releived that you could reproduce the fault though, I was just about to
give it all up.
In case you do find out what the problem is maybe you could email me the
solution at (e-mail address removed)
thanks again
Pen
 

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