Results 1 to 10 of 10

Thread: saving a text box as text with todays date

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    16

    Resolved saving a text box as text with todays date

    Does anybody have any idea on how to automatically save a text box as text with the file name as todays date.

    thanks
    Last edited by bparker; May 11th, 2005 at 03:57 PM. Reason: Solved

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: saving a text box as text with todays date

    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:
    1. Public Function SaveText(txtBox As TextBox) As Boolean
    2.     Dim hFile As Integer
    3.     Dim sFileName As String
    4.  
    5.     On Error Resume Next
    6.     hFile = FreeFile
    7.     sFileName = App.Path & "\" & Format(Now, "yyyymmdd") & ".txt"
    8.     Open sFileName For Output As #hFile
    9.     Print #hFile, txtBox.Text
    10.     Close #hFile
    11.     SaveText = (Err.Number = 0)
    12. End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    16

    Re: saving a text box as text with todays date

    I want to save it by clicking a button

  4. #4
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: saving a text box as text with todays date

    Private Sub Command1_Click
    Call SaveText
    end sub

    sir loin

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: saving a text box as text with todays date

    Well it would then be:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call SaveText(Text1)
    3. End Sub
    If the text box is named Text1 that is.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    16

    Re: saving a text box as text with todays date

    its not working

    this is what I have

    VB Code:
    1. Public Function SaveText(txtBox As TextBox) As Boolean
    2.     Dim hFile As Integer
    3.     Dim sFileName As String
    4.  
    5.     On Error Resume Next
    6.     hFile = FreeFile
    7.     sFileName = App.Path & "C:\My Documents\" & Format(Now, "yyyymmdd") & ".txt"
    8.     Open sFileName For Output As #hFile
    9.     Print #hFile, Text1.Text
    10.     Close #hFile
    11. End Function
    12.  
    13. Private Sub Command1_Click()
    14.     Call SaveText(Text1)
    15. End Sub
    16.  
    17. Private Sub Text1_Change()
    18.  
    19. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    16

    Re: saving a text box as text with todays date

    please help

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: saving a text box as text with todays date

    The problem is the line where you set the path:
    VB Code:
    1. sFileName = App.Path & "C:\My Documents\" & Format(Now, "yyyymmdd") & ".txt"
    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    16

    Re: saving a text box as text with todays date

    Thanks a lot for the help

    I works perfect now

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: saving a text box as text with todays date

    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:
    1. Print #hFile, Text1.Text


    and should be:
    VB Code:
    1. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width