Cancelled Previous Operation on DLookup or DCount

  • Follow


Hi,

I'm trying to prevent a duplicate entry of a number in a particular
field in a form, however, whenever I enter the data it gives me the
error "Run Time Error 2001"  you cancelled the previous operation.

The weird thing is that I have this exact same code running in another
form just fine.  The only thing different is that one is a txt field
and the other is an integer.   I've tried both DLookup and DCount as
shown in my sample code below.  Any ideas?

Private Sub SO__BeforeUpdate(Cancel As Integer)


If IsNull([SO#]) Then
Exit Sub
End If

Dim varTemp1 As Variant

    varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = Me![SO#]")

    If varTemp1 = Me![SO#] Then
    MsgBox "Duplicate!", vbOKOnly, "Duplicate SO Number Found"
    End If
End Sub

______________________________________________________________

    If DCount("*", "RMA INFO", "[SO#] =  Me.SO_.Value") > 0 Then
        MsgBox "You have entered a value that is already in the
table!"
        Me.Undo
    End If

______________________________________________________________

0
Reply AJ 6/21/2007 6:16:48 PM

You need to put the reference to the control outside of the quotes.

varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = " & Me![SO#])

That assumes SO# is numeric. If it's text, try

varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = '" & Me![SO#] & "'")

Exagerated for clarity, that's

varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = ' " & Me![SO#] & " ' ")

-- 
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"AJ" <aspoede@gmail.com> wrote in message 
news:1182449808.488409.206090@e9g2000prf.googlegroups.com...
> Hi,
>
> I'm trying to prevent a duplicate entry of a number in a particular
> field in a form, however, whenever I enter the data it gives me the
> error "Run Time Error 2001"  you cancelled the previous operation.
>
> The weird thing is that I have this exact same code running in another
> form just fine.  The only thing different is that one is a txt field
> and the other is an integer.   I've tried both DLookup and DCount as
> shown in my sample code below.  Any ideas?
>
> Private Sub SO__BeforeUpdate(Cancel As Integer)
>
>
> If IsNull([SO#]) Then
> Exit Sub
> End If
>
> Dim varTemp1 As Variant
>
>    varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = Me![SO#]")
>
>    If varTemp1 = Me![SO#] Then
>    MsgBox "Duplicate!", vbOKOnly, "Duplicate SO Number Found"
>    End If
> End Sub
>
> ______________________________________________________________
>
>    If DCount("*", "RMA INFO", "[SO#] =  Me.SO_.Value") > 0 Then
>        MsgBox "You have entered a value that is already in the
> table!"
>        Me.Undo
>    End If
>
> ______________________________________________________________
> 


0
Reply Douglas 6/21/2007 6:35:40 PM


On Jun 21, 1:35 pm, "Douglas J. Steele"
<NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> You need to put the reference to the control outside of the quotes.
>
> varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = " & Me![SO#])
>
> That assumes SO# is numeric. If it's text, try
>
> varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = '" & Me![SO#] & "'")
>
> Exagerated for clarity, that's
>
> varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = ' " & Me![SO#] & " ' ")
>
> --
> Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
> (no e-mails, please!)
>
> "AJ" <aspo...@gmail.com> wrote in message
>
> news:1182449808.488409.206090@e9g2000prf.googlegroups.com...
>
> > Hi,
>
> > I'm trying to prevent a duplicate entry of a number in a particular
> > field in a form, however, whenever I enter the data it gives me the
> > error "Run Time Error 2001"  you cancelled the previous operation.
>
> > The weird thing is that I have this exact same code running in another
> > form just fine.  The only thing different is that one is a txt field
> > and the other is an integer.   I've tried both DLookup and DCount as
> > shown in my sample code below.  Any ideas?
>
> > Private Sub SO__BeforeUpdate(Cancel As Integer)
>
> > If IsNull([SO#]) Then
> > Exit Sub
> > End If
>
> > Dim varTemp1 As Variant
>
> >    varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = Me![SO#]")
>
> >    If varTemp1 = Me![SO#] Then
> >    MsgBox "Duplicate!", vbOKOnly, "Duplicate SO Number Found"
> >    End If
> > End Sub
>
> > ______________________________________________________________
>
> >    If DCount("*", "RMA INFO", "[SO#] =  Me.SO_.Value") > 0 Then
> >        MsgBox "You have entered a value that is already in the
> > table!"
> >        Me.Undo
> >    End If
>
> > ______________________________________________________________

That worked like a charm!  Thanks so much. I knew I was just messing
up some syntax.

0
Reply AJ 6/21/2007 7:17:49 PM

Once you apply Doug's suggestion, don't forget to add the all important:
    Cancel = True
if a match is found.

HTH,


"AJ" <aspoede@gmail.com> wrote in message 
news:1182449808.488409.206090@e9g2000prf.googlegroups.com...
> Hi,
>
> I'm trying to prevent a duplicate entry of a number in a particular
> field in a form, however, whenever I enter the data it gives me the
> error "Run Time Error 2001"  you cancelled the previous operation.
>
> The weird thing is that I have this exact same code running in another
> form just fine.  The only thing different is that one is a txt field
> and the other is an integer.   I've tried both DLookup and DCount as
> shown in my sample code below.  Any ideas?
>
> Private Sub SO__BeforeUpdate(Cancel As Integer)
>
>
> If IsNull([SO#]) Then
> Exit Sub
> End If
>
> Dim varTemp1 As Variant
>
>    varTemp1 = DLookup("[SO#]", "RMA INFO", "[SO#] = Me![SO#]")
>
>    If varTemp1 = Me![SO#] Then
>    MsgBox "Duplicate!", vbOKOnly, "Duplicate SO Number Found"
>    End If
> End Sub
>
> ______________________________________________________________
>
>    If DCount("*", "RMA INFO", "[SO#] =  Me.SO_.Value") > 0 Then
>        MsgBox "You have entered a value that is already in the
> table!"
>        Me.Undo
>    End If
>
> ______________________________________________________________
> 


0
Reply George 6/21/2007 7:18:45 PM

3 Replies
2906 Views

(page loaded in 0.085 seconds)

Similiar Articles:










7/21/2012 6:30:35 AM


Reply: