Solved

Execute Parameters Queries in Batch

Posted on 2013-01-28
9
199 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you guys, I'll test and get back to you
0
 

Author Comment

by:fitaliano
Comment Utility
Fyed,

I get an error on:

CallByName Me, ctrlName & "_Click", vbMethod
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:fitaliano
Comment Utility
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)
Comment Utility
That should be:

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

Assisted Solution

by:fitaliano
fitaliano earned 0 total points
Comment Utility
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)
Comment Utility
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
Comment Utility
I think it si better to keep everything at the vba level rather then query the db but both solutions worked
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
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…
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…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now