How to save file with date and time stamp?
I know there are other examples, but using this code, is it possible to add the date and time to Output.txt filename, so I don't overwrite my file every time I save? Thx.
This is just a single command button that grabs the context of a text box and exports..
Code:
Private Sub CmdExport_Click()
Dim FF As Long
Dim FilePath As String
FF = FreeFile()
FilePath = "C:\Reports\Output.txt"
Open FilePath For Output As #FF
Print #FF, txtSql.Text & Date & " " & Time
Close #FF
End Sub
Re: How to save file with date and time stamp?
You open it for append
Open FilePath For Append As #FF
Edit: Do you want that Output.txt has Date-Time name instead of Output.txt?
Re: How to save file with date and time stamp?
Quote:
Edit: Do you want that Output.txt has Date-Time name instead of Output.txt?
Yep! :)
Both actually.... each time I hit save, the Reports directory would show:
Output12182008_345pm
Output12182008_346pm
etc...
A nicer looking date time format would be better.. not sure how to make that look best either.. though I can find that somewhere. Thx!
Re: How to save file with date and time stamp?
Well, date and time formats contain special characters you cannot use in filenames. You have to make a String variable containing Date and Time, and then replace those special characters with some other character, for example with - .
Re: How to save file with date and time stamp?
Quote:
Originally Posted by Dungeon Keeper
Well, date and time formats contain special characters you cannot use in filenames. You have to make a String variable containing Date and Time, and then replace those special characters with some other character, for example with - .
I tried that too.. created strDate as String and populated it, but still couldn't concantenate it to save file name.
Re: How to save file with date and time stamp?
I really don't even care if time date stamp is in the save file name. I just want it to create a new file every time I hit save.. and not overwrite the old one.
Re: How to save file with date and time stamp?
This code will replace the ":", "/" and empty spaces in string with "-" and "_" try the code :)
Code:
Dim MyFileName As String
MyFileName = Date & "_" & Time
For i = 1 To Len(MyFileName)
If Mid$(MyFileName$, i, 1) = " " Then Mid$(MyFileName$, i, 1) = "_"
If Mid$(MyFileName$, i, 1) = "/" Or Mid$(MyFileName$, i, 1) = ":" Then _
Mid$(MyFileName$, i, 1) = "-"
Next i
You can now use MyFileName As the name for your output file, + you have to add an extension.
Code:
MyFileName = MyFileName & ".EXT"
Edit, oh, i didnt see that you want both:
Code:
MyFileName = "Output" & MyFileName & ".EXT"
Re: How to save file with date and time stamp?
I did it!!
Naturally.. after you wrote the solution. :P
It looks like I'm basically doing the same thing though.
Code:
Private Sub CmdExport_Click()
Dim FF As Long
Dim FilePath As String
Dim strDate As String
strDate = Format(Date, "MMMM") & Format(Date, "DDDD") & Format(Date, "YYYY") & Format(Time, "HH") & Format(Time, "NN") & Format(Time, "SS") & ".txt"
FF = FreeFile()
FilePath = "C:\Reports\Report" & strDate
Open FilePath For Output As #FF
Print #FF, txtSql.Text & Date & " " & Time
Close #FF
End Sub
Thx for your time, DK. I really appreciate it!
Re: How to save file with date and time stamp?
OK.. yours does look much neater. I like! Thx again!
Re: How to save file with date and time stamp?
You don't have to run all your formats separately. This would work.
Code:
strDate = Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"
Re: How to save file with date and time stamp?
Quote:
Originally Posted by jolene_keagy
I did it!!
Naturally.. after you wrote the solution. :P
It looks like I'm basically doing the same thing though.
Code:
Private Sub CmdExport_Click()
Dim FF As Long
Dim FilePath As String
Dim strDate As String
strDate = Format(Date, "MMMM") & Format(Date, "DDDD") & Format(Date, "YYYY") & Format(Time, "HH") & Format(Time, "NN") & Format(Time, "SS") & ".txt"
FF = FreeFile()
FilePath = "C:\Reports\Report" & strDate
Open FilePath For Output As #FF
Print #FF, txtSql.Text & Date & " " & Time
Close #FF
End Sub
Thx for your time, DK. I really appreciate it!
You are welcome :wave:
Re: How to save file with date and time stamp?
Quote:
Originally Posted by MarkT
You don't have to run all your formats separately. This would work.
Code:
strDate = Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"
Your code created the following file name:
ReportFalse
Re: How to save file with date and time stamp?
Just a suggestion and don't know if it's useful or not but if you change your date format around so that it's stored with
Filename YYYY MM DD HH MM SS
Then your users can sort by filename and they'll be in date order.
Not a coding suggestion, but it's all I can offer right now for what it's worth ;-)
Re: How to save file with date and time stamp?
Quote:
Originally Posted by jolene_keagy
Your code created the following file name:
ReportFalse
If you run this does it return a false?
Msgbox Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"
The complete code should be
Code:
Private Sub CmdExport_Click()
Dim FF As Long
Dim FilePath As String
Dim strDate As String
strDate = Format(Now, "MMMMDDDDYYYYHHNNSS") & ".txt"
FF = FreeFile()
FilePath = "C:\Reports\Report" & strDate
Open FilePath For Output As #FF
Print #FF, txtSql.Text & Date & " " & Time
Close #FF
End Sub
Re: How to save file with date and time stamp?
Usually I use this format so the files will be sorted in correct order:
Code:
FilePath = "C:\Reports\Report " & Format(Now, "yyyymmdd_hhnnss") & ".txt"