Full Version: Event issue on form
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
jackdaniel
It seems simple, but this code doesn't do anything. I want this code to prompt the user to enter the Date of Birth if both of the given conditions were meet. What am I doing wrong? Using the wrong event, coding or both?

CODE
Private Sub DOB_BeforeUpdate(Cancel As Integer)
If Me.Member_ = "True" And IsNull(Me.DOB) Then
MsgBox "You must a enter a DOB for this member", vbOKOnly, "Data Required"
Else
End If
End Sub
R. Hicks
Why do you have the code in the Before Update event of the control where the value would be entered ???
You need to trigger the code by other means sunch as in the Before Update event of the "form" ...

Also .. If "Member_" is a Boolean value (True or False) then the line should be:
CODE
If Me.Member_ = True And IsNull(Me.DOB) Then

And ... you need to invoke the Canacel argument in the procedure ...
So the code should be:
CODE
If Me.Member_ = True And IsNull(Me.DOB) Then

  Msgbox "You must a enter a DOB for this member", vbOKOnly, "Data Required"

  Cancel = True

  Me.DOB.SetFocus

End If

RDH
jackdaniel
Ricky, I have one question. Thanks for the last solution.

I was looking at another useful thread and tried to incorporate it into what I was doing, but nothing happen here either. crazy.gif

CODE
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyCode = vbKeyDelete Then
MsgBox "You don't have permission to delete records!", vbOKOnly, "Deletion Denied"
Else
End If
End Sub


I want the user to see the message when they try to delete a record.
R. Hicks
I would use the On KeyDown event of the form instead of the KeyPress event ...

CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
  KeyCode = 0
  MsgBox "You don't have permission to delete records!", vbOKOnly, "Deletion Denied"
End If
End Sub

Also .. you need to set the Key Preview property for the form to Yes ...

RDH
jackdaniel
The deletion MsgBox works fine, but I don't want it to popup when deleting a field just the record. How can I adjust this codeor event to have it to popup only during deleting a record.
CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
MsgBox "You don't have permission to delete records!", vbExclamation, "Deletion Denied"
Else
End If
End Sub

Also, how do I incorporate this statement "If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return linefeed character combination (Chr(13) & Chr(10)) between each line." into my msgbox prompt?

I tried this (MsgBox "You don't have permission to delete records!&Chr(13) & Chr(10)&Trying something new", vbExclamation, "Deletion Denied") and it didn't work.
R. Hicks
If this is for only when a record is deleted .. you need a completely different approch ...
You need to use the On Delete event to stop the user from deleting a record ...

RDH
jackdaniel
What is the code to do such and didn't respond to my second question?

QUOTE
Also, how do I incorporate this statement "If prompt consists of more than one line, you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return linefeed character combination (Chr(13) & Chr(10)) between each line." into my msgbox prompt?

I tried this (MsgBox "You don't have permission to delete records!&Chr(13) & Chr(10)&Trying something new", vbExclamation, "Deletion Denied") and it didn't work.
R. Hicks
You need something like the following:
CODE
MsgBox "You don't have permission to delete records!" & vbCrLf & "Trying something new", vbExclamation, "Deletion Denied"

RDH
R. Hicks
Try the following code in the On Before Del Confirm event of the form ...

CODE
Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
Dim strMsg As String, strTitle As String

strMsg = "You don't have permission to delete records."
strTitle = " Deletion Denied"

MsgBox strMsg, vbExclamation + vbOKOnly, strTitle
Cancel = True
Response = acDataErrContinue

End Sub

RDH
jackdaniel
Your last suggestion didn't work.

I think I need a statement like this under the Key Down Event:

If KeyCode = vbKeyDelete and me.recordselector is selectedThen
MsgBox "You don't have permission to delete records!", vbExclamation, "Deletion Denied"
Else
End If

Just don't know how to do it.

Thanks for all the help you been giving me!
R. Hicks
It works on my machine ...
How are you deleting the record ???
By clicking the record selector in the form and hitting the delete key ???

If you are .. then it will and does work ...

RDH
jackdaniel
Yes, that is what I'm doing. Having the allow deletion set to 'no' cause a problem.
R. Hicks
So .. have you got it going ???
The Allow Deletions property would need to be set to Yes .. the Cancel = True will stop the deletion.

RDH
jackdaniel
It's working now! I had to set Allow Deletions to 'yes'.

I am curious! What does the 'acDataErrContinue' mean or do?
R. Hicks
Access will generate an error when the deletion of the record is cancelled by the Cancel = True line ...
The acDataErrContinue sees this error and resumes without displaying an error message on screen ...

RDH
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.