PDA

View Full Version : Solved: Cancel and Undo with Macro



Imdabaum
06-08-2010, 07:37 AM
I have a macro that I want to run on the BeforeUpdate event for a close date. There is another macro that runs on the AfterUpdate event.

The BeforeUpdate macro validates whether the person has permission to close out the order (by adding a date to the closed on field).

Currently if the validation fails, it runs CancelEvent and then calls Undo. This stops the event and clears the text box, but then it treats the Undo as an update and runs the BeforeUpdate again.

Anyone have any ideas on how to just restore the previoius data in a clean manner?

Imdabaum
06-08-2010, 08:48 AM
Or is there a way to have the Macro submit the Esc key if the validation returns false?

Imdabaum
06-10-2010, 03:59 PM
Turns out I can't use the 2007 variables because not everyone has 2007 even though it is still saved as an mdb.

So for those who are familiar with macros ( I usually prefer VBA to Macros, but the creator already has a very long macro.

Anyone know why I can't do the following? In my macro as a condition I have NeedsReview=True. NeedsReview tries to get some values from the loaded form and then validates using the RequiresReview function.

Enum RaterType
mgr = -1
reviewer = 0
srReviewer = 1
End Enum

Public Function GetExposure() As Double
GetExposure = [Forms]![Input Form]![Log Subform RR].[Form]![Text434]
'Doesn't return a value. And errors out.
End Function

Public Function NeedsReview(rt As RaterType) As Boolean
Dim mgr As String
Dim expsr As Integer
Dim rqst As String
Dim extnsn As String

expsr = GetExposure() 'ERRORS out here.

rqst = GetRequest()
extnsn = GetExtension()
Select Case rt
Case -1
mgr = GetMgr()
Case 0
mgr = GetReviewedBy()
Case 1
mgr = GetSrReviewer()
End Select
NeedsReview = RequiresReview(mgr, expsr, rqst, extnsn)
End Function


What's wrong with the way I am referencing my controls?

OBP
06-11-2010, 03:29 AM
What error do you get with this
GetExposure = [Forms]![Input Form]![Log Subform RR].[Form]![Text434]
what do you get if you use
msgbox [Forms]![Input Form]![Log Subform RR].[Form]![Text434]

Imdabaum
06-11-2010, 10:24 AM
Type Mismatch. Error 13.

A few of the processes call the acNext, then acPrevious to force the save, and it appears that calculated fields aren't calculated after that step. If I capture those values before any other checks are made, it works just fine.

In the end I converted her macro and did it my way with old fashioned VBA.:cloud9:

The macro feature seems pretty cool though for 2007 applications. I look forward to trying my hand at those a little more.

I still am struggling with the BeforeUpdate though. If the validation fails, I undo the change, and cancel the event. But if I try to leave the field it continues to call the BeforeUpdate method until the user hits ESC. Can I automate this somehow?

If ((m_NeedsReview = True) And (Not IsNull(Me.RR_Out_Date) Or Me.RR_Out_Date <> "")) Then
MsgBox "Total Exposure is higher than your authorized limit to approve." & VbNewline & _
Please remove the invalid data to proceed. Or press {ESC}.", vbOKOnly, ""
DoCmd.RunCommand acCmdUndo
DoCmd.CancelEvent
End If

Imdabaum
06-11-2010, 11:05 AM
Still learning... sorry.

DoCmd.RunCommand acCmdUndo <> Me.Undo.

Me.Undo fixes it.
If anyone can explain why this works and not the other method, I'd appreciate the knowledge.

OBP
06-11-2010, 12:21 PM
I don't know either :think: