Do Menu Item

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Currently I have a command button (Save) . There is a couple of lines of code under that button that are not completed due to a BeforeUpdate function that checks to determine if any of the fields were left blank. The from the command button (Save) the code is:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.GoToRecord , , acNewRec
DoCdm.GoToControl "MRN"

Is there some way to surpress the Msg Box "The Do Menu Item action was canceled"?

Thanks For Your Help,

Kenny G
 
Hi Kenny:

I tried your code on a test form and... I didn't get the error message. I
did, though, change the 3rd "DoCdm" to "DoCmd" before successfully running
the code.

Regards,
AL
Kenny G said:
Hello,

Currently I have a command button (Save) . There is a couple of lines of
code under that button that are not completed due to a BeforeUpdate function
that checks to determine if any of the fields were left blank. The from the
command button (Save) the code is:
 
Al,

Many thanks for your response. I probably need to cut and paste the original code - see below. The beforeupdate event kicks in before the actual record is saved therefore the docmd(s) save record and go to newrecord are not completed.


Command Button:

Private Sub cmdsaverecordDataEntry_Click()
' Comments : Save record and move cursor as required.
' Parameters: -
' Modified : 06/03/04 kgg
'
' --------------------------------------------------
Dim intanswer2 As Integer

On Error GoTo Err_cmdsaverecordDataEntry_Click

If Me.Dirty = False Then
MsgBox "You Can't Save This Record - No Data Entry Was Made!", vbInformation, "UHS"
DoCmd.GoToControl "MRN"
Else
intanswer2 = MsgBox("Do You Have Another Entry To Make?", vbYesNo)
Select Case intanswer2
Case vbYes
MsgBox "Your Record Has Been Saved - Please Continue.", vbInformation, "UHS"
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "MRN"
Case vbNo
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.GoToControl "cmdReturnToMainMenu"
MsgBox "Click Return To Main Menu Button To Exit!", vbInformation, "UHS"
End Select
Exit_cmdsaverecordDataEntry_Click:
Exit Sub

Err_cmdsaverecordDataEntry_Click:
MsgBox Err.Description
Resume Exit_cmdsaverecordDataEntry_Click
End If
End Sub

Conflicting code:

Private Sub Form_BeforeUpdate(pintCancel As Integer)
' Comments : All active controls must have data input before data is saved.
' Parameters: pintCancel -
' Modified : 06/03/04 kgg
'
' --------------------------------------------------
'TVCodeTools ErrorEnablerStart
On Error GoTo PROC_ERR
'TVCodeTools ErrorEnablerEnd

Dim strRqdMsg As String

strRqdMsg = "The Following Required Field(s) Were Left Blank!"

If IsNull(MRN) Then
strRqdMsg = strRqdMsg & vbCrLf & "MRN"
End If
If IsNull(DOB) Then
strRqdMsg = strRqdMsg & vbCrLf & "DOB"
End If
If IsNull(QIReviewer) Then
strRqdMsg = strRqdMsg & vbCrLf & "QIReviewer"
End If
If IsNull(Unit) Then
strRqdMsg = strRqdMsg & vbCrLf & "Unit"
End If
If IsNull(ClinicalJustification) Then
strRqdMsg = strRqdMsg & vbCrLf & "ClinicalJustification"
End If
If IsNull(RestraintType) Then
strRqdMsg = strRqdMsg & vbCrLf & "RestraintType"
End If
If IsNull(BehaviorExhibited) Then
strRqdMsg = strRqdMsg & vbCrLf & "BehaviorExhibited"
End If
If IsNull(RestraintApplied) Then
strRqdMsg = strRqdMsg & vbCrLf & "RestraintApplied"
End If
If IsNull(Complications) Then
strRqdMsg = strRqdMsg & vbCrLf & "Complications"
End If
If IsNull(RiskAssessment) Then
strRqdMsg = strRqdMsg & vbCrLf & "RiskAssessment"
End If
If IsNull(DTMMDOrder) Then
strRqdMsg = strRqdMsg & vbCrLf & "DTMMDOrder"
End If
If IsNull(DTMSignedOrder) Then
strRqdMsg = strRqdMsg & vbCrLf & "DTMSignedOrder"
End If
If IsNull(OrderRenewal) Then
strRqdMsg = strRqdMsg & vbCrLf & "OrderRenewal"
End If
If IsNull(AppliedMonitoredPolicy) Then
strRqdMsg = strRqdMsg & vbCrLf & "AppliedMonitoredPolicy"
End If
If IsNull(RangeOfMotion) Then
strRqdMsg = strRqdMsg & vbCrLf & "RangeOfMotion"
End If
If IsNull(CirculationCheck) Then
strRqdMsg = strRqdMsg & vbCrLf & "CirculationCheck"
End If

If Len(strRqdMsg) > 48 Then
MsgBox strRqdMsg
pintCancel = True

End If

'TVCodeTools ErrorHandlerStart
PROC_EXIT:
Exit Sub

PROC_ERR:
MsgBox Err.Description
Resume PROC_EXIT
'TVCodeTools ErrorHandlerEnd

End Sub


I appreciate your assistance in this matter.

Kenny G
 
Hi Kenny:

I apologize for not getting back sooner; I've been on call the past few
nights (I'm a physician).

Ok, here's how I would write the code- just dump it all in the clickbutton
OnClick event. I've taken the liberty of adding error checking for duplicate
values. I've also added a few "Exit Sub" statements and added "... and a New
One Added" to your MsgBox statement (to let the user know that he's on a new
record). It should theoretically work well. Let me know if you get any bugs:

---------------------------------------------

Option Compare Database
Dim intanswer2 As Integer
Option Explicit


Private Sub CheckIndex()
On Error GoTo CheckIndex_Error

If Me.Dirty = False Then
MsgBox "You Can't Save This Record - No Data Entry Was Made!",
vbInformation, "UHS"
DoCmd.GoToControl "MRN"

Exit Sub
Else

DoCmd.RunCommand acCmdSaveRecord
intanswer2 = MsgBox("Do You Have Another Entry To Make?", vbYesNo)
Select Case intanswer2
Case vbYes
MsgBox "Your Record Has Been Saved, and a New One Added - Please Continue.",
vbInformation, "UHS"

DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "MRN"

Case vbNo
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToControl "cmdReturnToMainMenu"
MsgBox "Click Return To Main Menu Button To Exit!", vbInformation, "UHS"
End Select

Exit Sub

CheckIndex_Error:
' here we check and trap for any possible identical key field value records
If Err.Number = 3022 Then
MsgBox "Duplicate record. We'll delete the lower valued record."
Me.Undo
Else
MsgBox Err.Number & " " & Err.Description, , "Private Sub CheckIndex()"
End If
Exit Sub
End Sub

Regards,

Al

Kenny G said:
Al,

Many thanks for your response. I probably need to cut and paste the
original code - see below. The beforeupdate event kicks in before the
actual record is saved therefore the docmd(s) save record and go to
newrecord are not completed.
 
Hi Kenny:



I apologize for not getting back sooner; I've been on call the past few
nights (I'm a physician). I accidentally left out a lot of your code in my
just saved response- so ignore it. Here's the complete code.



First of all, before we begin, it would be easier to go to each textbox
control's tag property and add something like "ChkNull". This way when you
wish to check for nulls, you don't have to have a zillion If... End If
statements. All you would need to do is to add the following code to access
all of your controls:



Dim c As Object

For Each c in Me.Controls

If c.Tag = "ChkNull" Then

strRqdMsg = strRqdMsg & vbCrLf & c.Name

End If

Next 'c



Easy huh? Anyhow, let's clean up your code...


therefore the docmd(s) save record and go to newrecord are not completed.



Actually it is better that the beforeupdate event kicks in first, as you
want to check for null fields and thus allow the user to add information to
them, right? The 3 major events are as such:

1) check to see if the IsDirty event is true, and if so

2) check for null fields, and if none then

3) save record, and if there aren't duplicate key fields

4) ask the user if he wants to add another record, then invoke
the yes/no msgbox event and route appropriately.



Ok, here's how I would write the code- I would just dump it all in the
clickbutton's OnClick event. I've taken the liberty of adding error checking
for duplicate values. I've also added a few "Exit Sub" statements and added
"... and a New One Added" to your MsgBox statement (to let the user know
that he's on a new record). It should theoretically work well. Let me know
if you get any bugs:



Option Compare Database
Dim intanswer2 As Integer

Dim c As Object

Dim strRqdMsg As String
Option Explicit


Private Sub Command8_Click()
On Error GoTo CheckIndex_Error



If Me.Dirty = False Then
MsgBox "You Can't Save This Record - No Data Entry Was Made!",
vbInformation, "UHS"
DoCmd.GoToControl "MRN"

Exit Sub
Else



strRqdMsg = "The Following Required Field(s) Were Left Blank!"
For Each c in Me.Controls

If c.Tag = "ChkNull" Then

strRqdMsg = strRqdMsg & vbCrLf & c.Name

End If

Next

If Len(strRqdMsg) > 48 Then
MsgBox strRqdMsg
pintCancel = True
Exit Sub
End If



DoCmd.RunCommand acCmdSaveRecord
intanswer2 = MsgBox("Do You Have Another Entry To Make?", vbYesNo)
Select Case intanswer2
Case vbYes
MsgBox "Your Record Has Been Saved, and a New One Added - Please Continue.",
vbInformation, "UHS"

DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "MRN"

Case vbNo
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToControl "cmdReturnToMainMenu"
MsgBox "Click Return To Main Menu Button To Exit!", vbInformation, "UHS"
End Select

Exit Sub



CheckIndex_Error:
' here we check and trap for any possible identical key field value records
If Err.Number = 3022 Then
MsgBox "Duplicate record. We'll delete the lower valued record."
Me.Undo
Else
MsgBox Err.Number & " " & Err.Description, , "Private Sub CheckIndex()"
End If
Exit Sub
End Sub
------------------------------------
Regards,

Al
Kenny G said:
Al,

Many thanks for your response. I probably need to cut and paste the
original code - see below. The beforeupdate event kicks in before the
actual record is saved therefore the docmd(s) save record and go to
newrecord are not completed.
 
I got all the code placed where I think it should except:
Hi Kenny:

This should be embedded right into the code as shown in my previous letter
and as noted below (without the weird spacing introduced my Windows XP's
WordPad. It should all go right into your OnClick event... remember, the
phrase replaces your multiple If.. then.. End If statements:

-------------------------------------------------------

Option Compare Database
Dim intanswer2 As Integer
Dim c As Object
Dim strRqdMsg As String
Option Explicit
------------------------------------------------------
Private Sub Command8_Click()
On Error GoTo CheckIndex_Error
If Me.Dirty = False Then
MsgBox "You Can't Save This Record - No Data Entry Was Made!",
vbInformation, "UHS"
DoCmd.GoToControl "MRN"
Exit Sub
Else
strRqdMsg = "The Following Required Field(s) Were Left Blank!"

'+++++++++++ place the code right here
For Each c in Me.Controls
If c.Tag = "ChkNull" Then
strRqdMsg = strRqdMsg & vbCrLf & c.Name
End If
Next

'+++++++++++
If Len(strRqdMsg) > 48 Then
MsgBox strRqdMsg
pintCancel = True
Exit Sub
End If
DoCmd.RunCommand acCmdSaveRecord
intanswer2 = MsgBox("Do You Have Another Entry To Make?", vbYesNo)
Select Case intanswer2
Case vbYes
MsgBox "Your Record Has Been Saved, and a New One Added - Please
Continue.",
vbInformation, "UHS"
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToControl "MRN"
Case vbNo
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToControl "cmdReturnToMainMenu"
MsgBox "Click Return To Main Menu Button To Exit!", vbInformation,
"UHS"
End Select
Exit Sub
CheckIndex_Error:
' here we check and trap for any possible identical key field value records
If Err.Number = 3022 Then
MsgBox "Duplicate record. We'll delete the lower valued record."
Me.Undo
Else

MsgBox Err.Number & " " & Err.Description, , "Private Sub CheckIndex()"
End If
Exit Sub
End Sub
------------------------------------



That's it... please let me know if it goes well!



Regards,

Al
 
Hi Kenny:

Actually, I realized a possible bug.... this aspect of the code should work
better in this manner:

For Each c in Me.Controls
If c.Tag = "ChkNull" and IsNull(c) Then
strRqdMsg = strRqdMsg & vbCrLf & c.Name
End If
If Len(strRqdMsg) > 48 Then
MsgBox strRqdMsg
pintCancel = True
Exit Sub
End If
Next

This not only checks for a c.Tag of "ChkNull" and that the control is null;
also, but including the "If Len(strRqdMsg) > 48 Then" with the Exit Sub, it
would allow the sub to be cancelled as soon as one null object is found.

Regards,
Al
 
Back
Top