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