Solved

Opening Queries using the "Run Code" in Switchboard.

Posted on 2004-09-09
11
2,729 Views
Last Modified: 2007-12-19
Firstly, I understand that the Switchboard feature is the work of the devil and should be avoided, but I am helping out a client and I can't redo all his Switchboard menus for him or ask my client to scrap them.

However, on the positive side the Switchboard menus do seem quite stable in this database.

Anyway, my client wants to be able to use Switchboard to open queries directly, Switchboard has a Open Report and Open Form feature but not Open Query.

I've decided to use the Run Code feature of Switchboard, however despite my best efforts I can't get it to work.

Here's what I did. I added a new Module to the database, called CustomMod.

In CustomMod I added the subroutine:

Public Sub OpenQuery(strQuery As String)
    If (TypeName(CurrentData.AllQueries(strQuery)) = "Nothing") Then
        MsgBox "Query Doesn't Exist: " & strQuery
    Else
        DoCmd.OpenQuery strQuery
    End If
End Sub

Then I added a new Switchboard menu item that runs the exact code:
OpenQuery "Test"

But when activated that gives the error "There was an error executing the command.". What gives??? Running a sub without a parameter works fine.

BTW I am doing this all on Access 2000.

PLEASE HELP !!!
0
Comment
Question by:nickwilton
11 Comments
 
LVL 84
ID: 12015218
Your code should work ... assuming the name is correct, and the query returns records.

What line does your code error on?
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12015222
Hi

The problem is that if the query does not exist then CurrentData.AllQueries(strQuery))  returns an error

try this instead. The code captures the error and displays a message to the user if the query does not exist or opens the query if it does

Public Sub OpenQuery(strQuery As String)
On Error GoTo err_handler

  temp = CurrentData.AllQueries(strQuery).FullName

  DoCmd.OpenQuery strQuery
   
  Exit Sub
 
err_handler:
  MsgBox "Query Doesn't Exist: " & strQuery

End Sub

HTH

Scott
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12015261
You cannot use parameters in the switchboard...

Public Sub OpenQuery(strQuery As String)
    If (TypeName(CurrentData.AllQueries(strQuery)) = "Nothing") Then
        MsgBox "Query Doesn't Exist: " & strQuery
    Else
        DoCmd.OpenQuery strQuery
    End If
End Sub

Public Sub OpenQueryX()
   OpenQuery "test"
End Sub

ANd you Switchboard menu get's the command
OpenQueryX

You might do the same using a macro instead of code, but the idea and limitations are the same

CHeers
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:nickwilton
ID: 12015264
Sorry Scott. Your code gives the same error.

To answer the first question. My code doesn't error on any line at all, it doesn't even try to run the code (i.e. if I put a breakpoint on the first line of the subroutine it doesn't break there at all.).
The error seems to be related to the Switchboard command...maybe Switchboard doesn't accept code with parameters, however this would be a fairly severe limitation.

If you have any other ideas about how I could run a query from Switchboard, let me know.
0
 

Author Comment

by:nickwilton
ID: 12015296
Thanks AngelIII. I was hoping that wasn't the case, because the client will go bananas if he sees any code, and macros are just plain messy.

Is there any other suggestions to opening Queries within Switchboard ???
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12015416
You'd have to modify the switchboard code to allow arguments to be passed. Have a look here:
http://www.experts-exchange.com/Databases/MS_Access/Q_21116625.html
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 12015433
nickwilton,
 (Just to explain the reason behind why it won't work) The Switchboard uses the "Application.Run" method to run the command wich would require your command to be typed like this: "OpenQuery" , "YourQueryName" including the quotes, however when it gets the command from a variable (or recordset) it screws up the format and the run command cannot read it. If you wanted to have some fun you can add your own switchboard category for DoCmd.OpenQuery (I can give details). Otherwise I think you are stuck using a MACRO or hard coding the query name into your sub.

God bless!

Sam
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 12015516
Here a possible suggestion to support 1 argument, based on the SPACE as separator
        ' Run code.
        Case conCmdRunCode
            If InStr(rs![Argument], " ") > 0 Then
                Application.Run Left(rs![Argument], InStr(rs![Argument], " ") - 1), Mid(rs![Argument], 1 + InStr(rs![Argument], " "))
            Else
                Application.Run rs![Argument]
            End If

The Switchboard menu item that runs the exact code (note to remove the double quotes)
OpenQuery Test

CHeers
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12015520
PS: the above code needs to be place in the Switchboard form's code of function HandleButtonClick()
0
 

Author Comment

by:nickwilton
ID: 12015618
Thanks angelIII. Your solution works perfectly and gives me exactly the functionality I wanted. Full points to you.
0
 
LVL 4

Expert Comment

by:szacks
ID: 12015643
just a thought, I've never used the switchboard before...

Can you reference the switchboard in code the way you reference a normal form? i.e. Forms!frm_name
if so you can grab the queryname right off the form.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question