Solved

Execute Parameters Queries in Batch

Posted on 2013-01-28
9
206 Views
Last Modified: 2013-02-27
I have 20 queries that look as the following:

SELECT [ZARAD (M)].*
FROM [ZARAD (M)]
WHERE ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtARGCE] And ([ZARAD (M)].MG) Is Not Null)) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLATAMCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLATAMFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtARGIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACINS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACSOVIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLATAMIFR])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtCANCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtCANFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtCANIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEACORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEAFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEAINS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEAIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEASOV])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtGRCHNCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtGRCHNFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtIFRPROJFIN])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtJPNCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtJPNFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtJPNINS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtJPNJREIT])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtKORCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtKORFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLATAMSOVIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLEVFINCE])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtMEXCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtMEXFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtMEXIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtPACSOVIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtSSEACORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtSSEAFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtTWNCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtTWNFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSCORP])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSFI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtFUNDS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSINS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSPFMEG])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSPFSLG])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLEVFINREC])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSCOM])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSMFGT])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSTMT])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSCRH])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSUI])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSPFIFR])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACBKS])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtJPNSOVIPF])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAPACINFR])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtAMERICA_CG_QA])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtEMEA_CG_QA])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtUSSOV])) OR ((([ZARAD (M)].MG)=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtLATAMFUNDS]))
ORDER BY [ZARAD (M)].MG;

Open in new window


I select values from a form with checkboxes where if a specific checkbox is selected, a text field is filled with a value. Example if I select ARGCE, txtARGCE gets filled with a value (ARGCE) and the query gets the parameter.

The routine then proceeds to export everything into a template with the following code:

    DoCmd.TransferSpreadsheet acExport, 8, "zPRAD", "C:\WFReports_CG.xls", True, ""

Open in new window


Right now if I need to get a report for each one of the teams listed in the parameters, so I select one checkbox, I run the routine, export the query in the template, save the file with the name of the parameter (in our example: ARCGE.xls)

********************************************************************

MY QUESTION is:

Can I automate this manual process? Meaning: can I repeat the query indicated above for a list of parameters (teams) maybe stored in another table? Also, can I automatically save the exported file with the name of the team in a designated folder?

Thanks a lot
0
Comment
Question by:fitaliano
  • 5
  • 3
9 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 38829294
yes you can,

you need to change the querydef using vba codes..

dim qd as dao.querydef, nSql as string, dim rs as dao.recordset, db as dao.database
set db=currentdb

set qd=db.querydef("zPRAD")


set rs=db.openrecordset("tblTeams")

do until rs.eof
     nsql="select * from tableX where [team]='" & rs!team & "'"

     qd.sql=nsql

         DoCmd.TransferSpreadsheet acExport, 8, "zPRAD", "C:\WFReports_CG" & rs!team & ".xls", True, ""
                                 

     rs.movenext
loop

basically thats it..

just need to change object and field names
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 38831143
As I understand it, you have a separate textbox for each of the checkboxes in your form, is that correct?  Why, wouldn't it make more sense to have a single textbox and set the value of that one textbox to the particular value?  Then your query would be simple:
SELECT [ZARAD (M)].*
FROM [ZARAD (M)]
WHERE [ZARAD (M)].MG=[Forms]![30_F_Main_Menu]![S_MG_CG].[Form]![txtTeamName]
 AND [ZARAD (M)].MG Is Not Null
ORDER BY [ZARAD (M)].MG;

Open in new window

Now to your actual question.  Another way to approach this would be to loop through the checkboxes on your form, and execute the function from there.  Something like:
Private Sub cmd_BulkExport_Click

    Dim ctrl as control

    for each ctrl in me.controls
        if ctrl.controltype = 106 Then     'checkbox
            ctrl.value = true
            'this next line evaluates the Click event of the control
            CallByName Me, ctrlName & "_Click", vbMethod
                     DoCmd.TransferSpreadsheet acExport, 8, "zPRAD", "C:\WFReports_CG" & me.txtTeamName & ".xls"
            ctrl.Value = False
        EndIf
    Next

End Sub

Open in new window

0
 

Author Comment

by:fitaliano
ID: 38831531
Thank you guys, I'll test and get back to you
0
 

Author Comment

by:fitaliano
ID: 38833045
Fyed,

I get an error on:

CallByName Me, ctrlName & "_Click", vbMethod
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:fitaliano
ID: 38833132
My problem is that I cannot use one single text box to dynamically get the value because I must allow for multiple selections, therefore I have a text box for each check box.

Example: I have txtARGCE next to chkARGCE, txtAPAC next to chkAPAC, etc.

Sorry about the confusion I should have specified this before.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38833471
That should be:

CallByName Me, ctrl.Name & "_Click", vbMethod
0
 

Assisted Solution

by:fitaliano
fitaliano earned 0 total points
ID: 38835169
I still get the run time error 2465, Application defined or object defined error

This is the code

Private Sub MassGen_Click()

  Dim ctrl As Control
  
    For Each ctrl In Me.Controls
        If ctrl.ControlType = 106 Then     'checkbox
            ctrl.Value = True
''*********** This next line evaluates the Click event of the control **************
             CallByName Me, ctrl.Name & "_Click", VbMethod
                      DoCmd.TransferSpreadsheet acExport, 8, "zPRAD", "C:\C\WFReports_CG" & ctrl.Name & ".xls"
            ctrl.Value = False
        End If
    Next

End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38835239
Where are you getting the error?  

My guess is that you have a checkbox control on the form that is not related to this process, or does not have a "Click" event associated with it.

Try this:

For Each ctrl In Me.Controls
    If ctrl.ControlType = 106 Then     'checkbox
        debug.print me.name, ctrl.Name
    End If
Next
0
 

Author Closing Comment

by:fitaliano
ID: 38933223
I think it si better to keep everything at the vba level rather then query the db but both solutions worked
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run Time Error 3075 15 45
count all and uniques records in a table 20 27
Prevent use of Microsoft Office application 9 54
Field Size - Double?  Want to display 0 5 30
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now