Results 1 to 11 of 11
  1. #1
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27

    how can i do this constrain

    hello guys,
    i have a form that is called items, and it contains a field that is called itemNo and the type of this field is number. i want to do my own constraint which prevents the user to enter a number that is previously existed.
    i need your help please


    many thanks to you,
    bye

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Check for a duplicate in the BeforeUpdate event of the control on your form that is bound to this field.

  3. #3
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27
    first, thank you RuralGuy for your reply


    Check for a duplicate in the BeforeUpdate event of the control on your form that is bound to this field.
    really i didn't understand what do you mean exactly, do you mean that i have to go to the form that contanis itemNo field then go to the event BeforeUpdate of the itemNo field.
    could you add more explains to understand what do you mean
    thanks with regards,

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you entering the itemNo in a control on a form? If so, do a DCount or DLookup in the BeforeUpdate of that control or even a FindFirst would work.

  5. #5
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27
    hello,
    In the beforeUpdate i wrote this code

    Code:
    Private Sub itemNo_BeforeUpdate(Cancel As Integer)
    numberfield = Me.itemNo
    Dim a
    a = DLookup("[itemNo]", "Items", "[itemNo]='" & Me.itemNo & "'")
    If Not (IsNull(a)) Then
    MsgBox "This number is previously existed", vbCritical, "Warning Message!!!"
    Cancel = True
    Me.undo
    DoCmd.FindRecord numberfield
    End If
    is this right, when i test this code an error occur, in the attachment the image of this error
    the itemNo is of string type

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try this code instead:
    Code:
    Private Sub itemNo_BeforeUpdate(Cancel As Integer)
        If Not IsNull(DLookup("[itemNo]", "Items", "[itemNo]='" & Me.itemNo & "'")) Then
            MsgBox "This number is previously existed", vbCritical, "Warning Message!!!"
            Cancel = True
        End If
    End Sub

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    My bad, [itemNo] is numeric:
    Code:
    Private Sub itemNo_BeforeUpdate(Cancel As Integer)
        If Not IsNull(DLookup("[itemNo]", "Items", "[itemNo]=" & Me.itemNo)) Then
            MsgBox "This number is previously existed", vbCritical, "Warning Message!!!"
            Cancel = True
        End If
    End Sub

  8. #8
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27
    Thanks alot RuralGuy
    i'm sorry for annoying you, in really i haven't a sufficient information about dlookup function so i'm sorry for this mistake.
    could you give me a reference that explains how to use dlookup function in details.
    thanks alot
    regards,

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you looked in VBA Help yet? Here's a good link on the topic: http://www.mvps.org/access/general/gen0018.htm

  10. #10
    grad2009 is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    27
    many thanks to you (RuralGuy)
    regards,

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Sounds like you got it working. That's great! Glad we could help.

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums