Solved

Automate Batch with Access 2013

Posted on 2013-11-25
6
1,746 Views
Last Modified: 2013-11-26
Hello,

I created a simple database in Access 2013. I setup a query on certain fields if one of the Yes/No fields is true. Then, I created a Saved Export to export the True results to a CSV file. The CSV file contains values I need to run a command line utility. The command line utility uses the data from the CSV file to know what to do. Now, I want to automate this.

How do I create a macro to run the Saved Export and then run the MS-DOS batch file? Can this be done easily? If I need to create something else instead of a Macro then what is it? I would like to be able to create the CSV file and run the batch file from within Access 2013 so that I don't have to bounce between the desktop and Access.

Thanks in advance.
0
Comment
Question by:jhieb
  • 3
  • 3
6 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39676542
You can write a macro, but better, write a VBA function. I recommend you create a form and placed a button on it. Double click on the button in the Form Designer and a VB editor will appear.

There you need to write a simple code:
Private Sub cmdButton1_Click()
On Error GoTo Err_cmdButton1_Click

    Dim strFileName, stAppName As String

    stAppName = CurrentProject.Path & "\yourbatchfile.bat"
    
    strFileName = CurrentProject.Path & "\Export.csv"

    DoCmd.TransferText acExportDelim, "qryResults", strFileName, False

    Call Shell(stAppName, 1)

Exit_cmdButton1_Click:
    Exit Sub

Err_cmdButton1_Click:
    Msg = "Error # " & str(Err.Number) & Chr(13) & Err.Description
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
    Resume Exit_cmdButton1_Click

End Sub

Open in new window

Please replace in the code above the "qryResults" with the real query name.
Also fix the batch file name, and the name of the Export.csv.
Please note that the code assumes that the batch file and the csv file are in the same folder as your database file
0
 
LVL 1

Author Comment

by:jhieb
ID: 39679633
Thank you. I may have missed something. My query does not make the export. Instead, I created a "Saved Export" that creates the CSV file. I don't know how to do that with a query so that is why I made an export and saved the steps.
0
 
LVL 24

Expert Comment

by:chaau
ID: 39679638
The code I have provided does not use the report. It uses the query directly
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Author Comment

by:jhieb
ID: 39679653
I placed the database, and batch file in the same directory.

OK. I am making progress. I modified your script and changed the names of the batch file, csv file, and query name. When I click on the button I get the following error:

Error #3625
The text file specification 'qryExport' does not exist. You cannot import, export, or link using the specification.

Here is the modified version:

Private Sub Command0_Click()
On Error GoTo Err_cmdButton1_Click

    Dim strFileName, stAppName As String

    stAppName = CurrentProject.Path & "\MyBatch.cmd"
   
    strFileName = CurrentProject.Path & "\Batchlist.txt"

    DoCmd.TransferText acExportDelim, "qryExport", strFileName, False

    Call Shell(stAppName, 1)

Exit_cmdButton1_Click:
    Exit Sub

Err_cmdButton1_Click:
    Msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
    Resume Exit_cmdButton1_Click

End Sub
0
 
LVL 24

Assisted Solution

by:chaau
chaau earned 500 total points
ID: 39679671
Please add a comma here. I have made a typo the first time.
DoCmd.TransferText acExportDelim, , "qryExport", strFileName, False
0
 
LVL 1

Author Closing Comment

by:jhieb
ID: 39679681
This works very well. Thank you very much for the help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
My experience with Windows 10 over a one year period and suggestions for smooth operation
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

930 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

11 Experts available now in Live!

Get 1:1 Help Now