Access exporting text files with variable file names

Mr. Plow

New Member
Joined
Aug 29, 2007
Messages
21
I have a music database that I am trying to export txt playlists from. The code would cycle through a table of dates and date codes (example: 7/1/1967; 19670701) to create a playlist with absolute file addresses. The problem I get when running isn't with the queries, but with the filename. My code is as follows:

VBA Code:
Sub rundata()


Dim rs As DAO.Recordset, i As Long
Dim dt As String


Set rs = CurrentDb.OpenRecordset("pdates")

Do While Not rs.EOF
    'DoCmd.RunMacro "macro1"
    dt = "D:\music\Playlists\Charts\" & DLookup("[DtStr]", "[Chart Date]") & ".txt"
    DoCmd.TransferText transferType:=acExportDelim, TableName:="Playlist 2", FileName:=dt, hasfieldnames:=True
    DoCmd.OpenQuery "Delete Min Date"
    'rs.MoveNext
Loop
MsgBox "Done"
Set rs = Nothing

End Sub

The error I get from this is Run-time error '3125': '19670701.txt' is not a valid name. Make sure that it does not include invalid characters of punctuation and that it is not too long."
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
TBH I cannot see anything wrong with the syntax. :(
However why the code, when you can get that from a format() function of your date field? Plus you are always picking up the first date, no criteria selected?
Is 19670701 actually a text field?

Perhaps try CStr() ?
 
Upvote 0
19670701 is a text field. It is being pulled from the [DtStr] field in the dlookup.

I tried a cstr(), but it put in a / instead of a -, which I can't use in file name.
 
Upvote 0
I cannot see how Cstr() would do that? :(

Try using Format() on your actual date field
Format(YourDateField,"yyyymmdd")

Also check the Len() of DtStr in case of any hidden characters.
 
Upvote 0
Ooooh. This did it! Ran a Trim$() on the DtStr field and it worked. Must have had a blank space at the start of the string. Thanks!
 
Upvote 0
You realize that with no criteria in the DLookup, you will get a random record from the table/query (usually the 1st in my experience)? That might be OK in your situation but maybe worth mentioning...
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,115
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top