dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Validation

  1. #1
    Join Date
    Feb 2010
    Posts
    21

    Validation

    Hi,
    I would like to perform a validation check on a field (Employee/Staff Number) there should not be duplicate entries in this field so i would like for a check to be performed when the next field is clicked then a msg displayed saying "Staff Number already exists"
    Any sample code would be appreciated

    Regards

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,324

    Re: Validation

    Typically this would be done with a lookup query assuming that your data is stored in a database.

  3. #3
    Join Date
    Feb 2010
    Posts
    21

    Re: Validation

    yes my data is in an access database, any code in mind?

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Validation

    If in your access table the field is an index field with no duplicates, you can proceed like this:
    Try to write the record to the table. If the entry is a duplicate the write will be rejected, returning an error code (or even throw up an error). You can trap this error and write an apropriate reaction like a messagebox saying "no duplicates".

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: Validation

    Correct. This needs to be handled through the database. Full stop.
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  6. #6
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Validation

    Quote Originally Posted by RussKass View Post
    Hi,
    I would like to perform a validation check on a field (Employee/Staff Number) there should not be duplicate entries in this field so i would like for a check to be performed when the next field is clicked then a msg displayed saying "Staff Number already exists"
    Any sample code would be appreciated

    Regards
    First of all you should try to write some code, based on your requirement and if you get struck in between , there are many gurus in this forum to guide.

    Code:
    Private Sub txtempNo_Validate(Cancel As Boolean)
        Dim TmpRs As New ADODB.Recordset
        TmpRs.Open "Select EmpNo From EmpDetails  Where EmpNo=" & txtempNo.Text, Conn, adOpenDynamic, adLockBatchOptimistic
        If Not TmpRs.EOF Then
            MsgBox "Staff Number already exists"
            Cancel = True
        End If
    End Sub
    Last edited by ComITSolutions; March 10th, 2010 at 09:36 AM.
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)