D
David C. Holley
But of course it fails, because I failed to notice that the variable was
DIM'd as LONG. My bad.
In that case, and if you haven't already, change Dim ANumber As Long to
Dim ANumber As String.
Also the statement, [ANumber= Me.ANumber.text] is not neccessary since
you can make the DLookup() use Me.ANumber in the WHERE statement. Note
that its not neccessary to specify the .TEXT property since Access will
automatically grab the value in a control when you reference the control.
If removing that statement doesn't fix the problem, I'm at a bit of a
loss, based on your comments. I can see how the problem *might* be
related to the .Undo statement and the DoCmd that follows it. But your
comment suggests that VBA is breaking at the ANumber=Me.ANumber.text
statement. At any rate try the following...
1) Comment out those lines and see if works
2) If it doesn't work, then the next step that I would do is to rename
the SUB to something generic (mySub) and then execute the code via a
temporary command button on the form. This will isolate your code from
the events and determine whether or not the issue is with the code or if
the issue is with how the code runs in conjuction with the events. I've
had a couple of instances where I've created a conflict for Access and
have to figure out where and what the conflict was.
David H
- Me.Filter = "lngTransportId = " & Me.txtGotoRecord
- Me.FilterOn = True
DIM'd as LONG. My bad.
In that case, and if you haven't already, change Dim ANumber As Long to
Dim ANumber As String.
Also the statement, [ANumber= Me.ANumber.text] is not neccessary since
you can make the DLookup() use Me.ANumber in the WHERE statement. Note
that its not neccessary to specify the .TEXT property since Access will
automatically grab the value in a control when you reference the control.
If removing that statement doesn't fix the problem, I'm at a bit of a
loss, based on your comments. I can see how the problem *might* be
related to the .Undo statement and the DoCmd that follows it. But your
comment suggests that VBA is breaking at the ANumber=Me.ANumber.text
statement. At any rate try the following...
1) Comment out those lines and see if works
2) If it doesn't work, then the next step that I would do is to rename
the SUB to something generic (mySub) and then execute the code via a
temporary command button on the form. This will isolate your code from
the events and determine whether or not the issue is with the code or if
the issue is with how the code runs in conjuction with the events. I've
had a couple of instances where I've created a conflict for Access and
have to figure out where and what the conflict was.
David H
- Me.FilterOn = FalseWhen I tried "Test" it didn't work at all
:
..Text? what type of control is A_Number and have you tried the .Value
property instead?
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.Filter = "lngTransportId = " & Me.txtGotoRecord
- Me.FilterOn = True
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