Results 1 to 5 of 5
  1. #1
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169

    Error message to display on entry of duplicate value with two fields as index


    Hello,
    I was using the following code on a field (ItemCode) that was indexed to prevent duplication of records. The intent is that the user will get an error message that a duplicate exists before they enter all the data for the record and get the built in error message that Access 2003 provides when an index violation has occurred.
    Code:
    Private Sub ItemCode_BeforeUpdate(Cancel As Integer)
     Dim Answer As Variant
     Answer = DLookup("[ItemCode]", "tblQuestions", "[ItemCode] = '" & Me.ItemCode & "'")
     If Not IsNull(Answer) Then
     MsgBox "Item Code already exists" & vbCrLf & "Please enter unique Item Code.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
     
     Cancel = True
     Me.ItemCode.Undo
     
     Else:
     End If
    End Sub
    Now, the index for this is based on two fields (ItemCode and Question Group). I would like to display the same message before update but don't know how to include the second field in the syntax.

    Anybody know a solution?

    Thanks,

  2. #2
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    I thought I might have it figured out by using the following code but get a type mismatch error. Am I heading down the right path?

    Code:
    Private Sub ItemCode_BeforeUpdate(Cancel As Integer) 
    Dim Answer As Variant
    Dim strWhere As String
    strWhere = ("[ItemCode] = '" & Me.ItemCode & "'") And ("[Question Group] = '" & Me.Question_Group & "'")
     Answer = DLookup("[ItemCode]", "tblQuestions", strWhere)
     If Not IsNull(Answer) Then
     MsgBox "Item Code already exists" & vbCrLf & "Please enter unique Item Code.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
     
     Cancel = True
     Me.ItemCode.Undo
     
     Else:
     End If
    End Sub

  3. #3
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Figured it out...syntax error. The darn single quote double quote...when to use messed me up again. Just had to mess around with it a bit.

    Code:
    Private Sub ItemCode_BeforeUpdate(Cancel As Integer) 
    Dim Answer As Variant
    Dim strWhere As String
    strWhere = "[ItemCode] = '" & Me.ItemCode & "' And [Question Group] = '" & Me.Question_Group & "'"
    Answer = DLookup("[ItemCode]", "tblQuestions", strWhere)
    If Not IsNull(Answer) Then
    MsgBox "Item Code already exists" & vbCrLf & "Please enter unique Item Code.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
     
    Cancel = True
    Me.ItemCode.Undo
     
    Else:
    End If
    End Sub

  4. #4
    hawkdriver's Avatar
    hawkdriver is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    23
    Good stuff, thanks Jim.

  5. #5
    veronica is offline Novice
    Windows 8 Access 2007
    Join Date
    Jan 2016
    Posts
    1

    similar, but I'm using two number fields.

    Quote Originally Posted by jpkeller55 View Post
    Figured it out...syntax error. The darn single quote double quote...when to use messed me up again. Just had to mess around with it a bit.

    Code:
    Private Sub ItemCode_BeforeUpdate(Cancel As Integer) 
    Dim Answer As Variant
    Dim strWhere As String
    strWhere = "[ItemCode] = '" & Me.ItemCode & "' And [Question Group] = '" & Me.Question_Group & "'"
    Answer = DLookup("[ItemCode]", "tblQuestions", strWhere)
    If Not IsNull(Answer) Then
    MsgBox "Item Code already exists" & vbCrLf & "Please enter unique Item Code.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
     
    Cancel = True
    Me.ItemCode.Undo
     
    Else:
    End If
    End Sub
    Hi,

    A co-worker was building a database for our office, but went into labor and left it unfinished. I am trying to do essentially what is described here using a composite index of two number fields [IndicatorMonth] and [IndicatorYear] and tried to copy the code above into a before update event for the field [IndicatorMonth] (the second of the two fields that are entered, but am getting runtime errors. The code I tried is below.

    Private Sub IndicatorMonth_BeforeUpdate(Cancel As Integer)
    Dim Answer As Variant
    Dim strWhere As String
    strWhere = "[IndicatorMonth] = '" & Me.IndicatorMonth & "' And [IndicatorYear] = '" & Me.IndicatorYear & "'"
    Answer = DLookup("[IndicatorMonth]", "CDCD", strWhere)
    If Not IsNull(Answer) Then
    MsgBox "A record for this month already exists" & vbCrLf & "Try again.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"

    Cancel = True
    Me.IndicatorMonth.Undo

    Else:
    End If
    End Sub


    Help??

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

Similar Threads

  1. How to display a Message
    By seb in forum Queries
    Replies: 7
    Last Post: 05-27-2011, 11:33 AM
  2. MsgBox for duplicate entry
    By usmcgrunt in forum Programming
    Replies: 3
    Last Post: 11-09-2010, 08:48 AM
  3. Error Message re: data entry of date
    By Pro-not in forum Access
    Replies: 9
    Last Post: 10-22-2010, 10:14 AM
  4. Replies: 4
    Last Post: 08-26-2010, 09:44 PM
  5. Replies: 3
    Last Post: 06-04-2010, 12:47 PM

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