Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Execute Parameters Queries in Batch

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
fitaliano
Asked:
fitaliano
  • 5
  • 3
3 Solutions
 
Rey Obrero (Capricorn1)Commented:
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
 
Dale FyeCommented:
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
 
fitalianoAuthor Commented:
Thank you guys, I'll test and get back to you
0
Independent Software Vendors: 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!

 
fitalianoAuthor Commented:
Fyed,

I get an error on:

CallByName Me, ctrlName & "_Click", vbMethod
0
 
fitalianoAuthor Commented:
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
 
Dale FyeCommented:
That should be:

CallByName Me, ctrl.Name & "_Click", vbMethod
0
 
fitalianoAuthor Commented:
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
 
Dale FyeCommented:
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
 
fitalianoAuthor Commented:
I think it si better to keep everything at the vba level rather then query the db but both solutions worked
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now