Solved

Automate Batch with Access 2013

Posted on 2013-11-25
6
1,851 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
[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
  • 3
  • 3
6 Comments
 
LVL 25

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 25

Expert Comment

by:chaau
ID: 39679638
The code I have provided does not use the report. It uses the query directly
0
Increase your protection from Zero Day threats!

Running two Antivirus' is never a good idea.
Taking advantage of Multiple Security layers on the other hand can often save your hide.
See which top notch security software brands have been proven to happily coexist together.
Reduce your chances of becoming a statistic.

 
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 25

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

Technology Partners: 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!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

739 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