|
-
Dec 18th, 2008, 04:38 PM
#1
Thread Starter
Member
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
-
Dec 18th, 2008, 04:51 PM
#2
Fanatic Member
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?
-
Dec 18th, 2008, 04:54 PM
#3
Thread Starter
Member
Re: How to save file with date and time stamp?
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!
Last edited by jolene_keagy; Dec 18th, 2008 at 04:59 PM.
-
Dec 18th, 2008, 04:58 PM
#4
Fanatic Member
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 - .
-
Dec 18th, 2008, 05:01 PM
#5
Thread Starter
Member
Re: How to save file with date and time stamp?
 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.
-
Dec 18th, 2008, 05:02 PM
#6
Thread Starter
Member
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.
-
Dec 18th, 2008, 05:02 PM
#7
Fanatic Member
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"
Last edited by Dungeon Keeper; Dec 18th, 2008 at 05:08 PM.
-
Dec 18th, 2008, 05:12 PM
#8
Thread Starter
Member
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!
-
Dec 18th, 2008, 05:16 PM
#9
Thread Starter
Member
Re: How to save file with date and time stamp?
OK.. yours does look much neater. I like! Thx again!
-
Dec 18th, 2008, 05:19 PM
#10
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"
-
Dec 18th, 2008, 05:29 PM
#11
Fanatic Member
Re: How to save file with date and time stamp?
 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
-
Dec 18th, 2008, 05:30 PM
#12
Thread Starter
Member
Re: How to save file with date and time stamp?
 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
-
Dec 18th, 2008, 05:35 PM
#13
Member
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 ;-)
-
Dec 18th, 2008, 05:43 PM
#14
Re: How to save file with date and time stamp?
 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
-
Dec 18th, 2008, 06:11 PM
#15
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"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|