I am trying to create a form that allows a user to enter information and saves information to a file. Every item must be complete and the account balance must not be negative or error messages pop up. I have the form working, but when you click on the clear and reset button, the error message "The file cannot be saved. Please confirm customer information has been entered in all boxes" appears, but it should not. Can someone show me where I'm going wrong and how to correct?

I would also prefer that instead of random Customer Numbers they are sequential, but the method I tried didn't work. Suggestions for that change would also be appreciated.

Imports System.IO
Public Class AddAccountsForm
    Dim strCustomerNumber As Integer    'Customer's number
    Dim strFirstName As String          'Customer's first name
    Dim strLastName As String           'Customer's last name
    Dim strAddress As String            'Customer's address
    Dim strCity As String               'Customer's city of residence
    Dim strState As String              'Customer's state of residence
    Dim strZip As String                'Customer's zip code
    Dim strTeleSphone As String          'Customer telephone number
    Dim intAccountBalance As Integer   'Customer's account balance


    Private Sub CustomerAccountsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerAccountsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.CustomerAccountsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.AllAccountDataSet)
    End Sub

    Private Sub AddAccountsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CustomerAccountsBindingSource.AddNew()
        txtLastPaymentDate.Value = Today()
    End Sub

    Private Sub SaveCloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCloseToolStripMenuItem.Click

        Dim Save As New SaveFileDialog()
        Dim CustomerDataFile As StreamWriter


        'Configure Save Record
        Save.Filter = "Access files (*.accdb)|*.accdb|All Files (*.*)|*.*"
        Save.InitialDirectory = "C:\"
        Save.ShowDialog()


        'Get customer information from user
        Try
            CustomerDataFile = System.IO.File.AppendText(Save.FileName)
            CustomerDataFile.WriteLine(txtFirst_Name.Text)
            CustomerDataFile.WriteLine(txtLast_Name.Text)
            CustomerDataFile.WriteLine(txtCustomer_Number.Text)
            CustomerDataFile.WriteLine(txtAddress.Text)
            CustomerDataFile.WriteLine(txtCity.Text)
            CustomerDataFile.WriteLine(txtState.Text)
            CustomerDataFile.WriteLine(txtZIP_Code.Text)
            CustomerDataFile.WriteLine(TxtPhone.Text)
            CustomerDataFile.WriteLine(txtAccount_Balance.Text)
            CustomerDataFile.WriteLine(txtLastPaymentDate.Text)
            CustomerDataFile.Flush()
        Catch ex As Exception
            'Error message
            MessageBox.Show("The file cannot be saved. Please confirm customer information has been entered in all boxes ")
        End Try


        Try
            Me.CustomerAccountsBindingSource.EndEdit()
            Me.CustomerAccountsTableAdapter.Update(AllAccountDataSet.CustomerAccounts)
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("Error saving the new customer account.")
        End Try

    End Sub

    Private Sub SaveWithoutClosingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveWithoutClosingToolStripMenuItem.Click
        Me.CustomerAccountsBindingSource.CancelEdit()
        Me.Close()
    End Sub

    Private Sub Customer_NumberTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustomer_Number.TextChanged
        'Assign account number
        Dim intNum As Integer
        Dim rand As New Random
        intNum = rand.Next(1000)
        txtCustomer_Number.Text = intNum
    End Sub

    Private Sub txtAccount_Balance_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAccount_Balance.TextChanged
        Dim intAccountBalance As Integer   'Customer's account balance

        'Get account balance from user
        If Integer.TryParse(txtAccount_Balance.Text, intAccountBalance) Then
            'Validate entry
            If intAccountBalance >= 0 Then
                intAccountBalance = CInt(txtAccount_Balance.Text)
            End If
        Else
            'Error message
            MessageBox.Show("Account balance must be an integer and must not be negative.")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        'Prepare form for next extry
        txtFirst_Name.Clear()
        txtLast_Name.Clear()
        txtAddress.Clear()
        txtCity.Clear()
        txtState.Clear()
        txtZIP_Code.Clear()
        TxtPhone.Clear()
        txtAccount_Balance.Clear()
        txtFirst_Name.Focus()
    End Sub
End Class

Basically all your message does is say something went wrong. Using the message directly from ex will give you a better idea of exactly want went wrong. Change the line to MessageBox.Show(ex). Then make note of the exact message wording. That will determine how you proceed from there. Or better yet comment out the Try block and VS will tell you exactly what went wrong and on which line. Just from a perusal of your code, it looks like you're trying to update your database file by appending text to it. I think you probably should be using SQL to insert the record or update the table.

As for the customer number, you're changing the number after the text in the textbox is shanged already. Reading the customer number in the last row of the table should allow you to assign the next sequential number.

It causing of txtAccount_Balance_TextChanged event.
When you clear textbox txtAccount_Balance it makes that event work and rise an error. It happen because there are no numbers in the textbox after clear the textbox but empty string.

OK! I changed the txtAccount_Balance from clear to =0. The program is letting me safe with blank cells. Is there a way to validate all txt boxes with a not null code?

There is a way to scan all textboxes as in

For Each tbx As TextBox in Me.Controls.OfType(Of TextBox)
    .
    .
    .
Next

and you can put your validation code inside the loop. This will scan all textboxes directly coontained in the form. If the textboxes are within another container such as a panel, splitter, groupbox, etc. then you have to replace Me with the appropriate container name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.