Does anybody have any idea on how to automatically save a text box as text with the file name as todays date.
thanks
Printable View
Does anybody have any idea on how to automatically save a text box as text with the file name as todays date.
thanks
Well automatically depends on when you want to do this. But here's the code, to call it simply pass the TextBox you want to be saved as an argument. This will save the text in the same folder as the application and the name of the file will be in this format yyyymmdd.txt.VB Code:
Public Function SaveText(txtBox As TextBox) As Boolean Dim hFile As Integer Dim sFileName As String On Error Resume Next hFile = FreeFile sFileName = App.Path & "\" & Format(Now, "yyyymmdd") & ".txt" Open sFileName For Output As #hFile Print #hFile, txtBox.Text Close #hFile SaveText = (Err.Number = 0) End Function
I want to save it by clicking a button
Private Sub Command1_Click
Call SaveText
end sub
sir loin
Well it would then be:If the text box is named Text1 that is.VB Code:
Private Sub Command1_Click() Call SaveText(Text1) End Sub
its not working
this is what I have
VB Code:
Public Function SaveText(txtBox As TextBox) As Boolean Dim hFile As Integer Dim sFileName As String On Error Resume Next hFile = FreeFile sFileName = App.Path & "C:\My Documents\" & Format(Now, "yyyymmdd") & ".txt" Open sFileName For Output As #hFile Print #hFile, Text1.Text Close #hFile End Function Private Sub Command1_Click() Call SaveText(Text1) End Sub Private Sub Text1_Change() End Sub
please help
The problem is the line where you set the path:App.Path returns the path to where you're application is installed or where your project file is stored if you're running from inside the VB IDE. So let's say you have your program in "C:\MyVBProject" that means that you're trying to create a file in C:\MyVBProjectC:\My Documents\20050511.txt and that will of course not work. If you want to use c:\My Documents\ then remove App.Path.VB Code:
sFileName = App.Path & "C:\My Documents\" & Format(Now, "yyyymmdd") & ".txt"
Thanks a lot for the help
I works perfect now
Just want to point out that the code has been mosdified from its original intent. That is, to pass a TextBox (any) to the function and have that data saved out.
Currently, the line in error is:
VB Code:
Print #hFile, Text1.Text
and should be:
VB Code:
Print #hFile, [b]txtBox[/b].Text
In the current function, the "Public Function SaveText(txtBox As TextBox) As Boolean"
is being passed the Object, but not being used in the function the way it was intended.
Bruce.