Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use Excel Automation from MSAccess VBA to Import a Text File and Save as Workbook

Posted on 2013-01-28
3
Medium Priority
?
1,175 Views
Last Modified: 2013-01-28
I have a text file that has been importing via Excel with VBA and gets saved as a workbook. I want to do the same with automation in MSAccess. I am getting a compile error on the Workbooks.OpenText method: Expected function or variable. Can you help me with the syntax? I have attached the txt file to import and the Access accdb that needs to run the automation.

Here is the code that works in Excel VBA:
Private Sub ImportTextFile1()

'Import order data
Workbooks.OpenText Filename:="C:\Dev\OrderTest.txt", Origin:=936, _
        StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, _
        2), Array(12, 2), Array(32, 2), Array(34, 1), Array(48, 1), Array(60, 1), Array(76, 1), _
        Array(84, 1), Array(102, 2), Array(112, 2), Array(121, 2), Array(129, 2)), _
        TrailingMinusNumbers:=True

'Save the file
ActiveWorkbook.SaveAs Filename:= _
        "C:\Dev\Order.xls", _
        FileFormat:=xlNormal, Password:="", WriteResPassword:=""

End Sub
TestDB.accdb
OrderTest.txt
0
Comment
Question by:venture-it
3 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38825933
You have assigned excel zone. Do you need any help with excel?
0
 
LVL 77

Accepted Solution

by:
peter57r earned 2000 total points
ID: 38826024
Modify as follows to use in Access...(assumes Excel is not running)

Private Sub ImportTextFile1()

Dim xlApp As Object
Const xlfixedwidth = 2    
Const xlnormal = -4143
Set xlApp = CreateObject("Excel.Application")


'Import order data
xlApp.Workbooks.OpenText FileName:="C:\Dev\OrderTest.txt", Origin:=936, _
        StartRow:=1, DataType:=xlfixedwidth, FieldInfo:=Array(Array(0, 1), Array(4, _
        2), Array(12, 2), Array(32, 2), Array(34, 1), Array(48, 1), Array(60, 1), Array(76, 1), _
        Array(84, 1), Array(102, 2), Array(112, 2), Array(121, 2), Array(129, 2)), _
        TrailingMinusNumbers:=True

'Save the file
xlApp.ActiveWorkbook.SaveAs FileName:= _
        "C:\Dev\Order.xls", _
        FileFormat:=xlnormal, Password:="", WriteResPassword:=""


xlApp.Quit
Set xlApp = Nothing

End Sub
0
 

Author Closing Comment

by:venture-it
ID: 38829371
Thanks, this is exacly what I needed.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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