?
Solved

Execute Parameters Queries in Batch

Posted on 2013-01-28
9
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 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 48

Accepted Solution

by:
Dale Fye earned 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fitaliano
ID: 38833045
Fyed,

I get an error on:

CallByName Me, ctrlName & "_Click", vbMethod
0
 

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 48

Expert Comment

by:Dale Fye
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 48

Expert Comment

by:Dale Fye
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

771 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