Text boxes that will save input to .txt or .xls

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
I've got a question that I hope someone can answer for me...At my place of work we have one person that creates programs for the other employees to use. Because of the nature of the work there are generally errors, and the process of reporting them is rather fuzzy.

What i want to do is create a few text boxes so that users can input data such as descriptions of errors, and for that data to be saved into its own new text file using one of the fields as a file name (saved into a specific directory). So ultimately there would be a folder with a list of text files containing errors to be dealt with. I'm doing this in visual Basic Express by the way.

I've made a quick mock up to illustrate what I'm trying to do...

rar.jpg


So when the data is entered, the 'OK' button would save it to a .txt file.

Can anyone point me in the right direction with this?
 
Last edited:
One way of accomplishing what you want...

This is demonstration code. Not necessarily the best way. But commented code if it helps. I've included a zip file you could unzip into your projects directory if you wish to.

-------------------------------------------------------------
VB.NET:
Public Class Form1
    Dim CustomerName As String
    Dim TemplateName As String
    Dim Description As String

    Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click

        'String variables are filled with the text that's in the appropriate TextBoxes.
        'I'd suggest checking for Null Text prior to loading the variables below.
        CustomerName = txt_CustomerName.Text
        TemplateName = txt_TemplateName.Text
        Description = txt_Description.Text

        'Write the Date Stamp and three strings to a text file.
        'This method creates a single TextFile on the C Drive.
        'You would need to make sure the file was read prior to
        '   a second User clicking the OK button or you'd lose the
        '   text the first User supplied.
        'It wouldn't be difficult to modify this code to prevent that.

        Using sw As IO.StreamWriter = New IO.StreamWriter("C:\FileName.txt") ' Make Path and FileName anything you want.
            'Date and Time Stamp it.
            'Write suppresses the carriage return / line feed (CRLF)
            sw.Write("The date is: ")   'Write will put the text in the file suppressing the CRLF
            sw.WriteLine(DateTime.Now) 'so the date and time appear on the same line as the descriptive text above.

            'Write The Customer, Template and Description
            'WriteLine takes care of putting each string
            'on its own line in the text file.
            sw.WriteLine(CustomerName)
            sw.WriteLine(TemplateName)
            sw.WriteLine(Description)
            sw.Close()

        End Using
    End Sub
    'You could put an EXIT or CLOSE button on the form that would check that the user clicked the OK button
    'prior to exiting and prompt to save data (if any) before closing your form.
    'It could be in the form of a MsgBox or another form that opened presenting a summary of the data and
    ' asking the User if he/she wanted to save before closing your form.

    'But hopefully this will help you with the problem you presented.
End Class
-------------------------------------------------------------
 

Attachments

  • ErrorReport.zip
    82.5 KB · Views: 21
Last edited by a moderator:
Hey thanks for that, seems to be what I was looking for. As you mentioned in the comments though, each time data is submitted to the text file, it overwrites it, losing the previous entry.

How would I go about preventing that?
 
Last edited:
I've realised something while playing around with this, it's going to be a problem when somebody has the text file open reading the error logs, and someone else is trying to save a new error to it using the form at the same time.

Is there a way around that? If not would it be possible to create a new text file within the directory each time the button is pressed? And use one of the text fields as the filename? This would overcome that problem...
 
Last edited:
I've fixed the problem of the text overwriting, and the issue of saving data into an already open txt file...so I think it's now more or less functional in the way I wanted, thanks alot for your help.

Here's what I did just in case you're interested -

VB.NET:
[B]Imports System.IO[/B]


[B]Public Class Form1[/B]

    Dim CustomerName As String
    Dim TemplateName As String
    Dim Description As String
    Dim FILE_NAME As String = ("\\IFADATAFLOW1\SharedDocs\FileName.txt")



[B]    Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click[/B]

        CustomerName = txt_CustomerName.Text
        TemplateName = txt_TemplateName.Text
        Description = txt_Description.Text


         Using sw As System.IO.StreamWriter = New System.IO.StreamWriter(FILE_NAME, True)                                         
            sw.BaseStream.Seek(0, SeekOrigin.End)

            sw.Write("Log Date: ")
            sw.WriteLine(DateTime.Now) 
            sw.Write("Customer Name: ")
            sw.WriteLine(CustomerName)
            sw.Write("Template Name: ")
            sw.WriteLine(TemplateName)
            sw.Write("Description: ")
            sw.WriteLine(Description)
            sw.WriteLine("         ")
            sw.WriteLine("------------------------------")
            sw.WriteLine("         ")
            sw.Close()

        End Using

[B]    End Sub

End Class[/B]
 
Last edited:
Back
Top