Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Text Files and saving text to them. 4

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
US
This is the best I can find to create a textfile and save a string variable's contents to it. Logically it doesn't make sense to me since you would first need to "create" a file before "opening" it, so either I just don't get it, or this is wrong.

Either way, it's not working, and generates the error: Run-time error '52': Bad file name or number

What am I doing wrong?
Code:
'Saves to Text File
Dim strUserText As String
strUserText = "This is a bunch of text to save in a textfile."

Open "c:\ProgramLogs\Log_" & Date$ & ".log" For Output As #1
Print #1, strUserText
Close #1

-Ovatvvon :-Q
 
depending on your locale settings you might have invalid characters in date$ causing the error 52. You could do a format of date$ to construct a valid filename.

Herman

 
Opening a non-existant file in this way does create the file. The code you posted works fine for me. Might it be something to do with the way it if formatting the date perhaps?

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
Too slow again...[sad]

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
After removing the Date variables, it works fine. So the problem is then, how do I create the date as part of the logfile name? That is important to me to have the date and time included. (I'm on a Windows XP Pro machine.)

-Ovatvvon :-Q
 
look at the format function. Be sure to not to use any non acceptable char from it such as "\" or "/".

Standard format for dates would be yyyy-mm-dd as being an universal understood one, instead of the less common dd-mm-yyyy or mm-dd-yyyy that can lead to confusion on what day/month it is referring to.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Try something like:
Code:
Format$(Date,"yyyy-mm-dd_hhmmss")
The above is an example format, you can change it to suit your own needs.

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
Try formatting the date/time as under.

Open "c:\ProgramLogs\Log_" & Format$(Now, "yyyy-mm-dd-hh-nn-ss") & ".log" For Output As #1

You can change the format string as per your requirement but make sure that it does not contain any invalid characters that do not qualify for file name.
 
Having read Hypetia's post I saw a child like mistake in mine. Date in my post should of course read Now (to allow the time formatting). I just wrote the Format() part around the existing code from the post. Doh!!! [blush]

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
You all have been excellent help. Stars for all of you! Thank you very much!!!!

-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top