Link to home
Start Free TrialLog in
Avatar of JENNIFER NEWTON
JENNIFER NEWTONFlag for United States of America

asked on

Need Help Getting VBA to Export Query as .txt then .zip final Folder

Hey Everyone,

I'm looking to create an Access 365 sub that does the following things:

1.)  Creates a Folder
2.)  Exports the results of a query to that folder in a .txt tab delimited format
3.)  Copies a file from the C drive to this folder
4.)  Zips the folder

I'm a novice with coding in general so I'm being stopped at step 2.  My code to create the folder is:

Dim rstemp As String
rstemp = "//SharedDrivePath/" & Me.OrderID.Value & ".FlowerOrder"
If Dir(rstemp, vbDirectory) = "" Then MkDir rstemp

Open in new window


That seems to work well

The query I want to export is based on a single table.  So getting the query to work is easy, but exporting as a .txt file has tripped me up.  Everything I try fails.

Here is an example table:
 User generated image
I've also attached that example table here as an excel sheet.

This is my basic select query with some slight formatting:

SELECT [FlowerName] & "_" & [Color] AS Flower_Color, Format([SproutDate],"yyyy/mm/dd") & "_" & Format([SproutDate],"hhnn") AS FormatSproutDate, FlowersTable.ScanCode, FlowersTable.FrostDate
FROM FlowersTable;

Open in new window


The resulting query looks like this:
User generated image
If anyone could help me with this I would really appreciate it.  Thank you and have a wonderful day.
FlowersTable.xlsx
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

For pt. 4 you can use the function Zip from my article and demo:

Zip and unzip files and folders with VBA the Windows Explorer way
Avatar of JENNIFER NEWTON

ASKER

Hey Gustav, thanks so much for responding to my post.  I'm trying your excellent looking zip function code now and it looks like I'm getting a variable not defined error regarding "ForWriting"

With FileSystemObject.OpenTextFile(ZipTemp, ForWriting, True)

Is there another function that I need to add first?
Perhaps you miss the scripting reference?

User generated image
If you don't use early binding, the enum that holds ForWriting is declared here:

#If EarlyBinding = False Then
    Public Enum IOMode
        ForAppending = 8
        ForReading = 1
        ForWriting = 2
    End Enum
#End If

Open in new window

Ah, thank you!  That works perfectly now!
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's it!  You saved the day!  Thank you for all of your help!
You are welcome!