Results 1 to 5 of 5
  1. #1
    MFriend is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Posts
    22

    Help getting nulls and duplicate checking to work

    Hi Folks:

    I have a field already that I was able to figure out how to use VBA to check if the value entered into it already is in the database and it works good (the field is the primary key and can't be left blank and the user wanted to have it checked before the submit button.

    I have another field that I also want to check to see if the number is already in the database (and if it is I want to return a message box). It is similar to the first except that this field allows the user to leave it empty (* see at the bottom for an explanation if needed).

    I have tried for several hours but nothing seems to work: (I get an error on the COW_NUMBER field if it is null)) and I couldn't figure out how to check for nulls using vba.

    Here is the code that is working for the other field:

    Private Sub CALF_NUMBER_Exit(Cancel As Integer)
    Dim NEWCALF_NUMBER As String
    Dim stLinkCriteria As String

    NEWCALF_NUMBER = Me.CALF_NUMBER.Value
    stLinkCriteria = "[CALF_NUMBER] = " & "'" & NEWCALF_NUMBER & "'"
    If Me.CALF_NUMBER = DLookup("[CALF_NUMBER]", "CALF_ENTRY", stLinkCriteria) Then
    MsgBox "This Calf Number, " & NEWCALF_NUMBER & ", has already been entered into the database." _
    & vbCr & vbCr & "Please check the Calf Number again.", vbInformation, "Duplicate Calf Number"
    End If
    End Sub


    The field I am working on now is called "COW_NUMBER". If anything is entered it has to be 6 characters long. I need help with checking to see if it is null and if it is post a message box t and goes on to the next field. If the number already exists in the table (which is allowed) then a message box needs to be opened alerting them to the number already being in the database, but allowing them to keep the number an go on to the next field.

    I would greatly appreciate any help.

    Thanks, Matthew



    * For those wanting more indepth information:
    The access database going to be used to track the births of Calves. So the COW_NUMBER is the mothers number. The COW_NUMBER may be left null (blank) if the cowboys can't figure out which Cow is the mother. It is also possible that The COW_NUMBER could already have been entered into the Calf_Entry table IF she has had twins. The reason for the pop-up is just to let them know that they should check to see if this is a twin or if the COW_NUMBER (mother cows number) was entered incorrectly before.Calving_Screens_07302014.zip

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Your error would come from here:

    Dim NEWCALF_NUMBER As String

    because only Variant can accept Null. When you're setting the variable try

    Nz(NEWCALF_NUMBER, "")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    MFriend is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by pbaldy View Post
    Your error would come from here:

    Dim NEWCALF_NUMBER As String

    because only Variant can accept Null. When you're setting the variable try

    Nz(NEWCALF_NUMBER, "")
    Thank you for giving me ideas. I've tried looking at a vba book I bought but I'm pretty new to this.

    Here is the code I'm currently using:

    Private Sub COW_NUMBER_Exit(Cancel As Integer)
    Dim NEWCOW_NUMBER As String
    Dim stLinkCriteria As String

    NEWCOW_NUMBER = Me.COW_NUMBER.Value
    stLinkCriteria = "[COW_NUMBER] = " & "'" & NEWCOW_NUMBER & "'"
    If Me.COW_NUMBER = DLookup("[COW_NUMBER]", "CALF_ENTRY", stLinkCriteria) Then
    MsgBox "This Calf Number, " & NEWCOW_NUMBER & ", has already been entered into the database." _
    & vbCr & vbCr & "Please check the Calf Number again.", vbInformation, "Duplicate Calf Number"
    End If
    End Sub


    I tried putting your code in at this line:
    Nz(NEWCALF_NUMBER, "")= Me.COW_NUMBER.Value

    but that is probably the wrong place. I tried a couple of others with the same results.

    I have attached the database so anyone can check it out that wants to. Its on the Cow Number field on an "on exit".

    Thanks again for any help.

    Matthew
    Attached Thumbnails Attached Thumbnails runtime_error.JPG  
    Attached Files Attached Files

  4. #4
    MFriend is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Posts
    22
    Quote Originally Posted by pbaldy View Post
    Your error would come from here:

    Dim NEWCALF_NUMBER As String

    because only Variant can accept Null. When you're setting the variable try

    Nz(NEWCALF_NUMBER, "")
    Thankyou very much Paul. With your help and others I was able to get it working properly.

    Thanks!

    Matthew

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Nulls problem, hmm
    By redbull in forum Programming
    Replies: 6
    Last Post: 06-29-2012, 12:16 PM
  2. Update duplicate checking
    By bigderon88 in forum Programming
    Replies: 19
    Last Post: 03-23-2012, 10:45 AM
  3. Error checking field, SetFocus does not work
    By SemiAuto40 in forum Access
    Replies: 9
    Last Post: 07-26-2011, 10:08 AM
  4. SUM in regards to nulls
    By detlion1643 in forum Access
    Replies: 5
    Last Post: 02-03-2010, 08:50 AM
  5. GetRows - Invalid Use of Nulls
    By Wannabe_Pro in forum Programming
    Replies: 3
    Last Post: 07-22-2009, 07:07 AM

Tags for this Thread

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