J
Judy Ward
I have a Creche table (Creche is another word for Nativity). CrecheNum is
actually a text field. In order to get the records to sort correctly, I
added leading zero(s) to make each CrecheNum 3 digits.
I have a form for users to enter new Creches. I want two things to happen:
#1 - Add leading zero(s) if needed to make the CrecheNum 3 digits
#2 - Check to see if the CrecheNum entered is already in use, and if so, undo
I have #2 working in the form BeforeUpdate event, but when I put in the code
to do #1, I get Run-time error -2147352567 telling me that the BeforeUpdate
function is preventing the form from saving the data in the field. #1 works
in the AfterUpdate event, but I can't figure out how to cancel (can't set the
focus back to txtCrecheNum).
Below is my code that is not working. I would appreciate suggestions on how
to fix what I have or how to accomplish my goals a different way.
Private Sub txtCrecheNum_BeforeUpdate(Cancel As Integer)
'#1 If the user enters a one-digit number, add two leading zeroes
If Me.txtCrecheNum Like "#" Then
Me.txtCrecheNum = "00" & Me.txtCrecheNum
ElseIf Me.txtCrecheNum Like "##" Then
Me.txtCrecheNum = "0" & Me.txtCrecheNum
End If
'#2 Check to see if this CrecheNum is already being used
If Not IsNull(DLookup("[CrecheNum]", "Creche", "[CrecheNum]=" & "'" &
Me.txtCrecheNum & "'")) Then
MsgBox Me.txtCrecheNum & " is already in use. Please choose a
different Creche Number.", vbInformation
Cancel = True
Me.Undo
End If
End Sub
Thank you!
Judy
actually a text field. In order to get the records to sort correctly, I
added leading zero(s) to make each CrecheNum 3 digits.
I have a form for users to enter new Creches. I want two things to happen:
#1 - Add leading zero(s) if needed to make the CrecheNum 3 digits
#2 - Check to see if the CrecheNum entered is already in use, and if so, undo
I have #2 working in the form BeforeUpdate event, but when I put in the code
to do #1, I get Run-time error -2147352567 telling me that the BeforeUpdate
function is preventing the form from saving the data in the field. #1 works
in the AfterUpdate event, but I can't figure out how to cancel (can't set the
focus back to txtCrecheNum).
Below is my code that is not working. I would appreciate suggestions on how
to fix what I have or how to accomplish my goals a different way.
Private Sub txtCrecheNum_BeforeUpdate(Cancel As Integer)
'#1 If the user enters a one-digit number, add two leading zeroes
If Me.txtCrecheNum Like "#" Then
Me.txtCrecheNum = "00" & Me.txtCrecheNum
ElseIf Me.txtCrecheNum Like "##" Then
Me.txtCrecheNum = "0" & Me.txtCrecheNum
End If
'#2 Check to see if this CrecheNum is already being used
If Not IsNull(DLookup("[CrecheNum]", "Creche", "[CrecheNum]=" & "'" &
Me.txtCrecheNum & "'")) Then
MsgBox Me.txtCrecheNum & " is already in use. Please choose a
different Creche Number.", vbInformation
Cancel = True
Me.Undo
End If
End Sub
Thank you!
Judy