Solved

Automate Batch with Access 2013

Posted on 2013-11-25
6
1,719 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

743 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