Results 1 to 3 of 3

Thread: [2005] Saving Data to Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    2

    [2005] Saving Data to Text File

    I have a form, with a bunch of info. Name Number ect on different Text boxes. My question is how do i make it so if you hit the "Save" button it saves all that information to a excel spreadsheet or Txt file?

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Saving Data to Text File

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
            SaveTextBoxText("YourFileName")
        End Sub
    
        Private Sub SaveTextBoxText(ByVal filename As String)
            Dim w As New System.IO.StreamWriter(filename, False)
            w.WriteLine(TextBox1.Text)
            w.WriteLine(TextBox2.Text)
            w.WriteLine(TextBox3.Text)
            w.WriteLine(TextBox4.Text)
            'etc...
            w.Close()
        End Sub
    If you have a lot of TextBoxes you could also do;
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
            SaveTextBoxText("YourFileName")
        End Sub
    
        Private Sub SaveTextBoxText(ByVal filename As String)
            Dim w As New System.IO.StreamWriter(filename, False)
            For Each ctl As Control In Me.Controls
                If ctl.GetType.ToString = "System.Windows.Forms.TextBox" Then
                    w.WriteLine("TextBox " & ctl.Name & " has text " & ctl.Text)
                End If
            Next
            w.Close()
        End Sub
    But beware that the order the textboxes are written may change if you rename them.
    Last edited by Bulldog; May 10th, 2007 at 04:52 PM.

  3. #3
    New Member
    Join Date
    Feb 2007
    Posts
    14

    Re: [2005] Saving Data to Text File

    I am not farmiliar with txt files myself but I happen to know about this tutorial. Follow this link : http://www.homeandlearn.co.uk/NET/vbNet.html And Find The Heading : "VB .NET Nine - Working with Text Files"
    Hope this helps.

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