Results 1 to 6 of 6

Thread: Data Validation in Visual Basic 6.0

  1. #1
    Join Date
    Oct 2009
    Posts
    38

    Data Validation in Visual Basic 6.0

    I am a new joiner in the degree of software engineer and they have started my course with the Visual Basic 6.0. I don't have much ideas about this software and programming stuff about this. I have just started reading about this and I have came across Data Validation but I didn't got the concept of this. Can somebody tell me that how Data Validation can be done in Visual Basic. I have installed the Visual Basic 6.0 version on my Windows 7 machine.

  2. #2
    Join Date
    May 2009
    Posts
    511

    Re: Data Validation in Visual Basic 6.0

    Though I am not so friendly with the Visual Basic but still I would like to inform you some things about the validation on it. Validation means making sure that the input values are good. Is validated to ensure we pass the correct values for the calculations and to avoid making a "crash" the application. For example, if you enter data that is not digital, the calculation procedure will stop on an error "Type mismatch" and the user will stay down.

  3. #3
    Join Date
    May 2009
    Posts
    543

    Re: Data Validation in Visual Basic 6.0

    The MsgBox function

    Upon validation you will probably use the MsgBox () often. The MsgBox we have used until now is as simple as possible. There are other versions of the MsgBox that allow you to specify the user's intentions.

    For example:
    Code:
    Sun intMsg AS Integer 
    intMsg = MsgBox ("Error in value," vbOKCancel) 
    If intMsg = 1 Then 
    txtValeur.SetFocus 
    Else 
    Exit Sub 
    End If
    If intMsg is 1, the user has clicked on OK and you get a new value. If intMsg is 2, the user has made you want to Cancel and exit the procedure.

    The constants are used:
    • vbOKOnly
    • vbOKCancel
    • vbAbortRetryIgnore
    • vbYesNoCancel
    • vbYesNo
    • vbRetryCancel


    and return values:

    1 vbOk Ok
    2 vbCancel Cancel
    3 vbAbort Abort
    4 vbRetry Retry
    5 vbIgnore Ignore
    6 vbYes Yes
    7 vbNo

    There are 4 events that are generally used for validation:
    1. the _Change
    2. the _KeyPress
    3. the _LostFocus
    4. the _Validate

  4. #4
    Join Date
    Apr 2009
    Posts
    569

    Re: Data Validation in Visual Basic 6.0

    The "Change event"

    Note on Change: the Change event is invoked at every change we do: enter a character is therefore a change, Change runs every character you type. Here is a sample exchange:

    Code:
     Sun INTREP AS Integer
    	 Private Sub txtHeures_Change ()
        	    If Not IsNumeric (txtHeures.Text) Then
            	   INTREP = MsgBox ("Must be numeric" vbOKCancel)
        	    End If
    	    Then if INTREP = vbCancel
    		 Exit Sub
    	    End If
    	 End Sub
    The problem with this structure is that the letter is posted and remove it before continuing. There is another event that could be used which is easier: the KeyPress.

  5. #5
    Join Date
    May 2009
    Posts
    539

    Re: Data Validation in Visual Basic 6.0

    The "KeyPress event" : The KeyPress before the ASCII code of the keystroke before it is displayed in the TextBox. So we can verify it and ignore it if it is not good. The example illustrates how valid a "control array".

    Code:
     Private Sub txtHeures_KeyPress (Index As Integer, KeyAscii As Integer)
         'The numbers 0 to 9 have ASCII codes 48 to 57
         '43 Is the + sign, the sign is 45 - and 46 is the point.
         If (KeyAscii <48 Or KeyAscii> 57) _
             And Not KeyAscii = 43 _
             And Not KeyAscii = 45 _
             And Not KeyAscii = 46 Then
                 KeyAscii = 0
         End If
     End Sub
    Here's how it would do the same thing with Change

    Code:
     Private Sub txtHeures_Change (Index As Integer)
         If Not IsNumeric (txtHeures (Index)) Then
             MsgBox ("Must be numeric")
         End If
     End Sub

  6. #6
    Join Date
    May 2009
    Posts
    527

    Re: Data Validation in Visual Basic 6.0

    The "Validate event" : The TextBox has a CausesValidation property that can be True or False. If it is True, the Validate event will be invoked as soon as I try to leave the box. If I decide not to validate, in response to a question, for example, I can set the CausesValidation to False. Here is an example of a TextBox with Validate individual:

    Code:
     Private Sub txtHeures_Validate (Cancel As Boolean)
        	    If txtHeures <0 Or txtHeures> 100 Then
            	 MsgBox ("Must be a number between 0 and 100")
            	 Cancel = True
        	    End If
    	 End Sub
    Here is the Validate TextBox if part of a control array - the index should be included:
    Code:
     Private Sub txtHeures_Validate (Index As Integer, Cancel As Boolean)
         If txtHeures (Index) <0 txtHeures Gold (Index)> 100 Then
         	 MsgBox ("Must be a number between 0 and 100")
           Cancel = True
        End If
     End Sub
    "Cancel = True" is used to keep the focus in the current field. So long as there is an error the user can not leave the TextBox. It also uses the Validate to validate if the data entry is a date before going further:

    Code:
    	 Private Sub txtDate_Validate (Cancel As Boolean)
       		 If Not IsDate (txtDate.Text) Then
            		 MsgBox ("Must be a valid date")
            		 Cancel = True
        		 End If
    	 End Sub

Similar Threads

  1. Replies: 3
    Last Post: 01-03-2011, 02:05 PM
  2. What are data types and identifiers in Visual basic?
    By fLUTE in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:55 PM
  3. what are Visual Basic IDE?
    By Naresh Modi in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 09:49 AM
  4. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  5. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,561,084.19446 seconds with 17 queries