Run time error 2001

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajitk
    New Member
    • Feb 2008
    • 77

    Run time error 2001

    Dear All,

    I have been having a problem with the following code:

    Code:
    Private Sub Solar_Lantern_No_AfterUpdate()
    Dim vCheckEntry As Boolean
    [B]vCheckEntry = DLookup("[Solar Lantern No]", "Beneficiary", "[Solar Lantern No] = " & Me.Solar_Lantern_No)[/B]
      If vCheckEntry = True Then
        MsgBox "Solar Lantern NO. already exists. Please change it.", vbInformation, "Beneficiary"
        Cancel = True
        Me.Solar_Lantern_No.SetFocus
        Else
        Me.Name_of_the_Beneficiary.SetFocus
      End If
    End Sub
    The error occurs of the highlighted line. My basic idea is to check whether the primary key (Solar Lantern No) is not duplicated.

    Can anyone tell me where the problem is...??

    Thaning you in advance
    Sajit
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, Sajit.
    • I guess [Solar_Lantern_N o] form control is not bound to a correspondent [Solar_Lantern_N o] table field assuming [Solar_Lantern_N o] table field is really set as PK. Am I right?
    • "Cancel=Tru e" will work in event handler receiving "Cancel" argument by reference, for example in BeforeUpdate event handler. In AfterUpdate event handler it will throw an error or will do nothing depending on whether explicit variable declaration is enforced in the module or not.
    • DLookUp() function doesn't return Boolean, it returns a Variant "first" value of a field in record satisfying given criteria, if no records found it returns Null
    • The syntax of the criteria passed to DLookUp() is right for Number type table field. If [Solar_Lantern_N o] table field is set as Text, then single quotes wrapping the value, [Solar_Lantern_N o] table field compared to, have to be added.


    Regards,
    Fish

    Comment

    • sajitk
      New Member
      • Feb 2008
      • 77

      #3
      Thanks Fish,

      Can u give me an example of how to write this function when a text field is used...

      Sajit

      Originally posted by FishVal
      Hi, Sajit.
      • I guess [Solar_Lantern_N o] form control is not bound to a correspondent [Solar_Lantern_N o] table field assuming [Solar_Lantern_N o] table field is really set as PK. Am I right?
      • "Cancel=Tru e" will work in event handler receiving "Cancel" argument by reference, for example in BeforeUpdate event handler. In AfterUpdate event handler it will throw an error or will do nothing depending on whether explicit variable declaration is enforced in the module or not.
      • DLookUp() function doesn't return Boolean, it returns a Variant "first" value of a field in record satisfying given criteria, if no records found it returns Null
      • The syntax of the criteria passed to DLookUp() is right for Number type table field. If [Solar_Lantern_N o] table field is set as Text, then single quotes wrapping the value, [Solar_Lantern_N o] table field compared to, have to be added.


      Regards,
      Fish

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Quotes (') and Double-Quotes (") - Where and When to use them

        Comment

        • sajitk
          New Member
          • Feb 2008
          • 77

          #5
          Now it says "Type mismatch"

          sajit

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Well. And what is the type of [Solar_Lantern_N o] table field?

            P.S. In post #2 I've listed possible problems with code including those not causing the fault with DLookUp() function. Also I've made some assumptions as for info you've not provided (I mean particularly table metadata). So the list is not supposed to be applied as is but in accordance with form/table settings. :)

            Comment

            Working...