Consulting

Results 1 to 7 of 7

Thread: Solved: Cancel and Undo with Macro

  1. #1
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location

    Solved: Cancel and Undo with Macro

    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?
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  2. #2
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    Or is there a way to have the Macro submit the Esc key if the validation returns false?
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  3. #3
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    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.

    [vba]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
    [/vba]

    What's wrong with the way I am referencing my controls?
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    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]

  5. #5
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location

    Cancel and Undo changes/No more Macro....

    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.

    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?

    [vba]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
    [/vba]
    Last edited by Imdabaum; 06-11-2010 at 10:58 AM. Reason: Method changed, no longer doing this with a macro, so title doesn't make sense.
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  6. #6
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    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.
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  7. #7
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    I don't know either

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •