Roger, It still didn't like this line: ANumber = Me.A_Number.Text same
error message. If I change it to A_Number then I get a Run-time error
-'21473567 (80020009)': The macro or function set to the BeforeUpdate or
ValidationRule property for thi Field is prevent Microsoft Access from saving
the data in the field.
So, I thought maybe since I had ANumber set to "No Duplicates" that was
creating a problem so I changed it to allow duplicates. That didn't help
either. This is my last effort and then they'll just have to look up the
record themselves. Thanks for all your help.
:
Sorry. I didn't know that ANumber was text. My code assumed it was
numeric. This should work:
Private Sub txtANumber _BeforeUpdate(Cancel As Integer)
Dim Response As Integer
Dim ANumberAs Long
Dim rs As Object
If Me.NewRecord Then
ANumber= Me.ANumber.text
If ANumber= DLookup("ANumber", "tblNewApplicants", "[ANumber] = '" &
ANumber & "'") Then
Response = MsgBox("Value Exists - Open Existing?", vbYesNo)
If Response = vbYes Then
Me.ANumber.Undo
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Set rs = Me.Recordset.Clone
rs.FindFirst "[ANumber] = '" & ANumber & "'"
Me.Bookmark = rs.Bookmark
Else
Cancel = True
Me.AuID.SelStart = 0
Me.AuID.SelLength = Len(Me.ANumber)
End If
End If
End If
End Sub
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
All code is like this (I dimmed out what wasn't working) When I purposely
type in a duplicate number and tab out of the field I get a Run-time error
'13": Type mismatch. When I run my cursor over it the highlighted line,
it
is the (A_Number = CLng(Me.A_Number.Text)) Clng that comes up with a
type
mismatch.
Private Sub A_Number_BeforeUpdate(Cancel As Integer)
If ANumber = DLookup("ANumber", "tblNewApplicants", "[ANumber] = '" &
ANumber & "'") Then
MsgBox "You have entered a duplicate number."
Cancel = True
Me.A_Number.SelStart = 0
Me.A_Number.SelLength = Len(Me.ANumber)
End If
'In that case, try something like this:
'Private Sub txtANumber _BeforeUpdate(Cancel As Integer)
'Dim Response As Integer
'Dim ANumber As Long
'Dim rs As Object
'
' If Me.NewRecord Then
' A_Number = CLng(Me.A_Number.Text)
' If ANumber = DLookup("ANumber", "tblNewApplicants ", "[ANumber] = " &
ANumber) Then
' MsgBox "Value Exists - Open Existing?", vbYesNo
' If Response = vbYes Then
' Me.A_Number.Undo
' DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
'
' Set rs = Me.Recordset.Clone
' rs.FindFirst "[ANumber] = " & ANumber
' Me.Bookmark = rs.Bookmark
' Else
' Cancel = True
' Me.A_Number.SelStart = 0
' Me.A_Number.SelLength = Len(Me.ANumber)
'End If
'End If
'End If
'
End Sub
:
Exactly what does your code look like and what is the exact error
message?
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
Roger sorry, but when it debugs, this is the line it doesn't like.
When I
move over it, it shows the ANumber I put in, but it continues to
highlight
it
and say it needs to be deguged. I'm not sure what I'm doing wrong, as
one
time it says it is a Type MisMatch but when I saved it and opened a
new
form
it still says it needs to be degugged but returns the number I
entered?
It
isn't worth a lot of time but if you have a moment to look at it I'd
appreciate it.
:
In that case, try something like this:
Private Sub txtANumber _BeforeUpdate(Cancel As Integer)
Dim Response As Integer
Dim ANumberAs Long
Dim rs As Object
If Me.NewRecord Then
ANumber= CLng(Me.ANumber.text)
If ANumber= DLookup("ANumber", "Table1 ", "[ANumber] = " &
ANumber)
Then
Response = MsgBox("Value Exists - Open Existing?", vbYesNo)
If Response = vbYes Then
Me.ANumber.Undo
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, ,
acMenuVer70
Set rs = Me.Recordset.Clone
rs.FindFirst "[ANumber] = " & ANumber
Me.Bookmark = rs.Bookmark
Else
Cancel = True
Me.AuID.SelStart = 0
Me.AuID.SelLength = Len(Me.ANumber)
End If
End If
End If
End Sub
Note: I added the IF ME.NEWRECORD bit because this only makes sense
if
it
happens in a new record. Without this, it will happen if you try to
change
the value of an existing record, which may have unforeseen results.
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
Roger, that's exactly what I thought! I wondered if I could ask
another
quick question. Someone else who wrote gave me code saying if it
was
a
duplicate did they want to open the record, and to push yes if so.
That's
a
really good idea, but I can't make the code work. Is this
difficult
to
do?
:
Humpf! I made it up from scratch AND created a little database
to
test
it
in about 20 minutes.
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
Roger, Thank you so much! I was able to modify the code
slightly
and
it
worked! I knew there had to be simple code just couldn't get
it
in my
head
the proper syntax. I had a programmer that does work for us
tell
me
it