Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening Queries using the "Run Code" in Switchboard.

Posted on 2004-09-09
11
Medium Priority
?
2,797 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 85
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 143

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 143

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

824 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