Is is possible to run a Macro & Code in 1 Event Procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bre035
    New Member
    • Sep 2007
    • 42

    #1

    Is is possible to run a Macro & Code in 1 Event Procedure

    I am attempting to require certain field be populated with data, save the record, and open a new form in one Event procedure when a Comand Button is selected on Form. Unfortunately, this is erroring out on me or halt messages when attempting to run Macro certain intervals.
    HAlT Message is Condition=True, Action Name=Close, Arguments Form, frm_CLLDateEntr y, prompt


    My code:
    Code:
    Private Sub Command36_Click()
    Dim box As ComboBox
    Dim box1 As ComboBox
    Dim box2 As ComboBox
    
    Set box = Me![LOB]
    Set box1 = Me![CALL TYPE]
    Set box2 = Me![CALL RESULT]
      
    If IsNull(box) Then
      MsgBox "You must select Line of Business", vbExclamation, "No Line of Business"
        box.SetFocus
          box.Dropdown
            Cancel = True
    ElseIf IsNull(box1) Then
      MsgBox "You must select Call TYPE", vbExclamation, "No Call Result"
        box1.SetFocus
        box1.Dropdown
          Cancel = True
    ElseIf IsNull(box2) Then
      MsgBox "You must select Call Result", vbExclamation, "No Call Type"
        box2.SetFocus
        box2.Dropdown
          Cancel = True
    End If
    
        Dim stDocName As String
    
        stDocName = "mcr_AddNew"
        DoCmd.RunMacro stDocName
    
    Exit_Command36_Click:
        Exit Sub
    
        
    End Sub
    Last edited by NeoPa; Jun 30 '10, 04:56 PM. Reason: add HALT Message Details - Please use the [CODE] tags provided.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by Bre035
    I am attempting to require certain field be populated with data, save the record, and open a new form in one Event procedure when a Comand Button is selected on Form. Unfortunately, this is erroring out on me or halt messages when attempting to run Macro certain intervals.
    HAlT Message is Condition=True, Action Name=Close, Arguments Form, frm_CLLDateEntr y, prompt


    My code:
    Private Sub Command36_Click ()
    Dim box As ComboBox
    Dim box1 As ComboBox
    Dim box2 As ComboBox

    Set box = Me![LOB]
    Set box1 = Me![CALL TYPE]
    Set box2 = Me![CALL RESULT]

    If IsNull(box) Then
    MsgBox "You must select Line of Business", vbExclamation, "No Line of Business"
    box.SetFocus
    box.Dropdown
    Cancel = True
    ElseIf IsNull(box1) Then
    MsgBox "You must select Call TYPE", vbExclamation, "No Call Result"
    box1.SetFocus
    box1.Dropdown
    Cancel = True
    ElseIf IsNull(box2) Then
    MsgBox "You must select Call Result", vbExclamation, "No Call Type"
    box2.SetFocus
    box2.Dropdown
    Cancel = True
    End If

    Dim stDocName As String

    stDocName = "mcr_AddNew "
    DoCmd.RunMacro stDocName

    Exit_Command36_ Click:
    Exit Sub


    End Sub
    Cancel = True has no meaning within the Context of a Click() Event unless it is defined in the Form's Code Module or Globally in a Standard Code Module. Should this be the case, it is never a good idea to Declare a Boolean Variable named Cancel. You may also wish to Exit the Routine should any NULLs be found in any of the 3 Combo Boxes.
    Code:
    Private Sub Command36_Click()
    Dim box As ComboBox
    Dim box1 As ComboBox
    Dim box2 As ComboBox
    Dim stDocName As String
    
    Set box = Me![LOB]
    Set box1 = Me![CALL TYPE]
    Set box2 = Me![CALL RESULT]
    
    If IsNull(box) Then
      MsgBox "You must select Line of Business", vbExclamation, "No Line of Business"
        box.SetFocus
          box.Dropdown
            Exit Sub
    ElseIf IsNull(box1) Then
      MsgBox "You must select Call TYPE", vbExclamation, "No Call Result"
        box1.SetFocus
          box1.Dropdown
            Exit Sub
    ElseIf IsNull(box2) Then
      MsgBox "You must select Call Result", vbExclamation, "No Call Type"
        box2.SetFocus
          box2.Dropdown
            Exit Sub
    End If
    
    'Execute only if ALL Combos are polulated
    stDocName = "mcr_AddNew"
    DoCmd.RunMacro stDocName
    End Sub

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      You can include a macro from within a VBA procedure by simply executing it :
      Code:
      Call DoCmd.RunMacro(MacroName, _
                          [RepeatCount], _
                          [RepeatExpression])

      Comment

      Working...