2 Replies - 365 Views - Last Post: 26 January 2013 - 05:21 AM Rate Topic: -----

#1 909kidd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-January 13

Validation Error in Visual Basic

Posted 25 January 2013 - 11:56 PM

I wrote this code to create customer accounts and save them to file. It is also supposed to let you search for customers by last name. But The validation in the Customer Accounts Information part, blocks you from Typing in the Search by last name text box.

The form needs to not let customers leave blanks when filling out the form. How do I Block the validation for the search part?

I am not sure if I should use a boolean value or event handlers.


Imports System.IO

Structure CustomerAcstsInformation
    Dim last_name As String
    Dim first_name As String
    Dim customer_number As String
    Dim address As String
    Dim city As String
    Dim state As String
    Dim zipcode As String
    Dim telephone As String
    Dim accountBalance As Double
    Dim lastPaymentDate As Date

End Structure


Public Class Form1


    Dim filename As String = "Customer_Accounts.txt"
    Dim customerObj As CustomerAcstsInformation = New CustomerAcstsInformation()

    Private Sub txtLastName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
        If txtLastName.Text = "" Then
            MsgBox("Last Name must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtFirstName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
        If txtFirstName.Text = "" Then
            MsgBox("First Name must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtCustomerNumber_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCustomerNumber.Validating
        If txtCustomerNumber.Text = "" Then
            MsgBox("Customer Number must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtAddress_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAddress.Validating
        If txtAddress.Text = "" Then
            MsgBox("Address must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtCity_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCity.Validating
        If txtCity.Text = "" Then
            MsgBox("City must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtState_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtState.Validating
        If txtState.Text = "" Then
            MsgBox("State must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtZipCode_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtZipCode.Validating
        If txtZipCode.Text = "" Then
            MsgBox("Zip Code must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtTelephone_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelephone.Validating
        If txtTelephone.Text = "" Then
            MsgBox("Telephone Number must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If
    End Sub

    Private Sub txtAccountBalance_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAccountBalance.Validating
        If txtAccountBalance.Text = "" Then
            MsgBox("Account Balance must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            e.Cancel = True
        End If

        Try
            Double.Parse(txtAccountBalance.Text)
        Catch ex As Exception
            MsgBox("Account Balance must be numeric", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtAccountBalance.Text = ""
        End Try

    End Sub




    Private Sub txtAccountBalance_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAccountBalance.KeyPress
        If Not Char.IsNumber(e.KeyChar()) And Not e.KeyChar() = "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub populateCustomerStructure()

        customerObj.last_name = txtLastName.Text
        customerObj.first_name = txtFirstName.Text
        customerObj.customer_number = txtCustomerNumber.Text
        customerObj.address = txtAddress.Text
        customerObj.city = txtCity.Text
        customerObj.state = txtState.Text
        customerObj.zipcode = txtZipCode.Text
        customerObj.telephone = txtTelephone.Text
        customerObj.lastPaymentDate = dtLastPayment.Text
        customerObj.accountBalance = txtAccountBalance.Text
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click


        If validateCustomerInformation() Then
            populateCustomerStructure()
            Dim customerRecord As String = ""

            customerRecord = customerRecord + customerObj.last_name + vbTab
            customerRecord = customerRecord + customerObj.first_name + vbTab
            customerRecord = customerRecord + customerObj.customer_number + vbTab
            customerRecord = customerRecord + customerObj.address + vbTab
            customerRecord = customerRecord + customerObj.city + vbTab
            customerRecord = customerRecord + customerObj.state + vbTab
            customerRecord = customerRecord + customerObj.zipcode + vbTab
            customerRecord = customerRecord + customerObj.telephone + vbTab
            customerRecord = customerRecord + customerObj.accountBalance.ToString() + vbTab
            customerRecord = customerRecord + customerObj.lastPaymentDate + vbNewLine

            File.AppendAllText(filename, customerRecord)

            resetControls()

        End If


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeCustomersFile()

    End Sub
    Private Function validateCustomerInformation() As Boolean
        If txtLastName.Text = "" Then
            MsgBox("Last Name must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtLastName.Focus()
            Return False
        End If
        If txtFirstName.Text = "" Then
            MsgBox("First Name must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtFirstName.Focus()
            Return False
        End If

        If txtCustomerNumber.Text = "" Then
            MsgBox("Customer Number must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtCustomerNumber.Focus()
            Return False
        End If
        If txtAddress.Text = "" Then
            MsgBox("Address must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtAddress.Focus()
            Return False
        End If
        If txtCity.Text = "" Then
            MsgBox("City must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtCity.Focus()
            Return False
        End If


        If txtState.Text = "" Then
            MsgBox("State must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtCity.Focus()
            Return False
        End If

        If txtZipCode.Text = "" Then
            MsgBox("Zip Code must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtZipCode.Focus()
            Return False
        End If

        If txtTelephone.Text = "" Then
            MsgBox("Telephone Number must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtTelephone.Focus()
            Return False
        End If

        If txtAccountBalance.Text = "" Then
            MsgBox("Account Balance must contain some valid value", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtAccountBalance.Focus()
            Return False
        End If

        Try
            Double.Parse(txtAccountBalance.Text)
        Catch ex As Exception
            MsgBox("Account Balance must be numeric", MsgBoxStyle.OkOnly, "Error in Validating Control")
            txtAccountBalance.Focus()
            Return False
        End Try

        Return True
    End Function

    Private Sub InitializeCustomersFile()
        Dim fs As StreamWriter
        fs = File.CreateText(filename)
        fs.Close()
    End Sub

    Private Sub resetControls()
        txtLastName.Text = ""
        txtFirstName.Text = ""
        txtCustomerNumber.Text = ""
        txtAddress.Text = ""
        txtCity.Text = ""
        txtState.Text = ""
        txtZipCode.Text = ""
        txtTelephone.Text = ""
        dtLastPayment.Value = DateTime.Today()
        txtAccountBalance.Text = ""

    End Sub

    Private Sub btnSearchCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchCustomer.Click
        Dim customerRecords() As CustomerAcstsInformation = loadFileToCustomerStructure()
        If Not txtSearchBylastName.Text = "" Then
            For Each record As CustomerAcstsInformation In customerRecords
                If record.last_name.ToUpper().Trim() = txtSearchBylastName.Text.ToUpper().Trim() Then
                    loadCustomUsingSearch(record)
                    Return
                End If

            Next
        End If

        If Not txtSearchByCustomerNumber.Text = "" Then
            For Each record As CustomerAcstsInformation In customerRecords
                If record.customer_number.ToUpper().Trim() = txtSearchByCustomerNumber.Text.ToUpper().Trim() Then
                    loadCustomUsingSearch(record)
                    Return
                End If

            Next
        End If

        MsgBox("No such customer Account exists in the system", MsgBoxStyle.OkOnly, "Customer Account does not Exist")

    End Sub

    Private Sub loadCustomUsingSearch(ByVal record As CustomerAcstsInformation)
        txtLastName.Text = record.last_name
        txtFirstName.Text = record.first_name
        txtCustomerNumber.Text = record.customer_number
        txtAddress.Text = record.address
        txtCity.Text = record.city
        txtState.Text = record.state
        txtZipCode.Text = record.zipcode
        txtTelephone.Text = record.telephone
        txtAccountBalance.Text = record.accountBalance.ToString()
        dtLastPayment.Text = record.lastPaymentDate

    End Sub


    Private Function loadFileToCustomerStructure() As CustomerAcstsInformation()

        Dim customerRecords() As String = File.ReadAllText(filename).Split(vbNewLine)
        Dim recordCount As Integer = customerRecords.Count() - 1
        Dim CustomerStructureArray(recordCount - 1) As CustomerAcstsInformation

        For i = 0 To recordCount - 1
            Dim fields() As String = customerRecords(i).Split(vbTab)
            CustomerStructureArray(i).last_name = fields(0)
            CustomerStructureArray(i).first_name = fields(1)
            CustomerStructureArray(i).customer_number = fields(2)
            CustomerStructureArray(i).address = fields(3)
            CustomerStructureArray(i).city = fields(4)
            CustomerStructureArray(i).state = fields(5)
            CustomerStructureArray(i).zipcode = fields(6)
            CustomerStructureArray(i).telephone = fields(7)
            CustomerStructureArray(i).accountBalance = Double.Parse(fields(8))
            CustomerStructureArray(i).lastPaymentDate = fields(9)
        Next




        Return CustomerStructureArray

    End Function

    Private Sub btnPrintReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintReport.Click
        CustomerAccountsReport.Show(Me)
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.Close()
    End Sub


    Private Sub lblName_Click(sender As Object, e As EventArgs) Handles lblName.Click

    End Sub

    Private Sub txtLastName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLastName.TextChanged

    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Validation Error in Visual Basic

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Validation Error in Visual Basic

Posted 26 January 2013 - 05:05 AM

Set the CausesValidation property of txtSearchByLastName and of txtSearchByCustomerNumber to False. Then:

    Private Sub txtSearchByLastName_GotFocus(sender As Object, e As EventArgs) Handles txtSearchByLastName.GotFocus
        Me.AutoValidate = Windows.Forms.AutoValidate.Disable
    End Sub

This disables validation for the whole form. Add the same code to txtSearchByCustomerNumber_GotFocus.

This post has been edited by andrewsw: 26 January 2013 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Validation Error in Visual Basic

Posted 26 January 2013 - 05:21 AM

BTW Me.ValidateChildren() will run the validation checks for all the form controls, rather than duplicating all your code in validateCustomerInformation(). However, it will produce all the error messages and won't give you the opportunity to set a particular focus.

This post has been edited by andrewsw: 26 January 2013 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1