none
I need help with a VB Application RRS feed

  • Question

  • I need help with building an application and I am on a tight deadline.  Below I have included the specifics for what I need the application to do as well as the code that I have completed so far.  I am having trouble getting the data input into the text fields to save to a .txt file.  Also, I need validation to ensure that the values entered into the text fields coincide with the field type.  I am new to VB so please be gentle.  Any help would be appreciated.  Thanx
     

    •I need to use the OpenFileDialog and SaveFileDialog in my application.
    •Also, I need to use a structure.
     
    1. The application needs to prompt the user to enter the file name on Form_Load.
     
    2. Also, the app needs to use the AppendText method to write the Employee Data to the text file. My project should allow me to write multiple Employee Data to the same text file.  The data should be written to the text file in the following format (comma delimited)
     
    FirstName, MiddleName, LastName, EmployeeNumber, Department, Telephone, Extension, Email
     
    3. The Department dropdown menu DropDownStyle property should be set so that the user cannot enter inputs that are not in the menu.

    Public Class Form1
        Dim filename As String
        Dim oFile As System.IO.File
        Dim oWrite As System.IO.StreamWriter
        Dim openFileDialog1 As New OpenFileDialog()
        Dim fileLocation As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            openFileDialog1.InitialDirectory = "c:\"
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 1
            openFileDialog1.RestoreDirectory = True
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                fileLocation = openFileDialog1.FileName
            End If
            'filename = InputBox("Enter output file name")
            'oWrite = oFile.CreateText(filename)
            cobDepartment.Items.Add("Accounting")
            cobDepartment.Items.Add("Administration")
            cobDepartment.Items.Add("Marketing")
            cobDepartment.Items.Add("MIS")
            cobDepartment.Items.Add("Sales")
        End Sub
        Private Sub btnSave_Click(ByValsender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            'oWrite.WriteLine("Write e file")
            oWrite.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}{5,10}{6,10}{7,10}", txtFirstname.Text, txtMiddlename.Text, txtLastname.Text, txtEmployee.Text, cobDepartment.SelectedText, txtTelephone.Text, txtExtension.Text, txtEmail.Text)
            oWrite.WriteLine()
        End Sub
        Private Sub btnExit_Click(ByValsender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            oWrite.Close()
            End
        End Sub
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            txtFirstname.Text = ""
            txtMiddlename.Text = ""
            txtLastname.Text = ""
            txtEmployee.Text = ""
            txtTelephone.Text = ""
            txtExtension.Text = ""
            txtEmail.Text = ""
            cobDepartment.SelectedText = ""
        End Sub
    End Class
    

    • Edited by Mikey81 Thursday, January 15, 2015 3:20 AM
    • Moved by Amanda Zhu Thursday, January 15, 2015 7:14 AM
    • Edited by Paul IshakModerator Thursday, January 15, 2015 7:24 AM Clean up code insertion
    Thursday, January 15, 2015 3:19 AM

Answers

All replies

  • Hi Mikey81,

    Your issue is about VB programming, so Visual Basic forum is a better forum for your case. I moved this thread there,

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 15, 2015 7:15 AM
  • Try this

            Dim CSV As String = String.Join(",", {txtFirstname.Text, txtMiddlename.Text, txtLastname.Text, txtEmployee.Text, cobDepartment.SelectedText, txtTelephone.Text, txtExtension.Text, txtEmail.Text})
            oWrite.WriteLine(CSV)


    jdweng

    Thursday, January 15, 2015 7:36 AM
  • 'oWrite = oFile.CreateText(filename)

    For the code you have, your StreamWriter should append, not overwrite.  At the moment, it appears to be Nothing.  See here for creating an append StreamWriter:

    http://msdn.microsoft.com/en-us/library/system.io.file.appendtext%28v=vs.110%29.aspx

    Thursday, January 15, 2015 8:09 AM