• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 104
  • Last Modified:

Excel VBA Msg Box Run Twice

Hello Experts,

i have problem as i have use following Userfor Event and when condition meet Msgbox Run Twice and i have to ok twice please guide me that i shows once and is there any way that userform auto okay after 5sec.

Regards,

Private Sub cmbDbtAmt1_Change()

If VchrFrm.CmbAccTyp1.Value = "Receipt" Then
VchrFrm.cmbCdtAmt1.SetFocus
VchrFrm.cmbDbtAmt1 = ""
MsgBox "Receipt Account Cannot Be Debited Please Reffer to Follow Entery Rule"


End Sub

Open in new window

0
WiseOwl Excel
Asked:
WiseOwl Excel
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Can you post the entire code for that event? The code you posted won't compile as there's a missing "End If".
0
 
WiseOwl ExcelAuthor Commented:
Sir you can put end if and Test.
0
 
Fabrice LambertFabrice LambertCommented:
When you enter a value, the code clear it. Ehence why it trigger the event twice.
Use the before change event, and cancel it if your checks fail.
Something like:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If (TextBox2.Value = "abc") Then
        If (TextBox1.Value <> vbNullString) Then
            Cancel = True
            TextBox1.Value = vbNullString
            MsgBox "No no no !"
        End If
    End If
End Sub

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Wayne Taylor (webtubbs)Commented:
Sorry, I should have noticed it earlier, but besides the missing "End If" you are changing the value of the ComboBox within it's Change() event, which in turns fires it's Change event again. You can avoid the second firing of the event by setting EnableEvents to False as seen in the below code.

Private Sub cmbDbtAmt1_Change()
   Application.EnableEvents = False
    If VchrFrm.CmbAccTyp1.Value = "Receipt" Then
        VchrFrm.cmbCdtAmt1.SetFocus
        VchrFrm.cmbDbtAmt1 = ""
        MsgBox "Receipt Account Cannot Be Debited Please Reffer to Follow Entery Rule"
    End If
    Application.EnableEvents = True
End Sub

Open in new window

0
 
Aleksandr M.EconomistCommented:
Ussually for the purposes of the kind you should declare a userform private variable (bDbtValueSet in the example below) and check it's state to allow _change event proceeding or not. Because Application.EnableEvents are not working in Userforms unfortunately. But make sure that you reset private module level variable in your further code if you're planning to allow a user to change cmbDbtAmt1 value again

Option Explicit
Private bDbtValueSet As Boolean

Private Sub cmbDbtAmt1_Change()
If bDbtValueSet = False Then
    If VchrFrm.CmbAccTyp1.value = "Receipt" Then
        bDbtValueSet = True
        VchrFrm.cmbCdtAmt1.SetFocus
        VchrFrm.cmbDbtAmt1 = ""
        MsgBox "Receipt Account Cannot Be Debited Please Reffer to Follow Entery Rule"
    End If
End If
End Sub

Open in new window

0
 
WiseOwl ExcelAuthor Commented:
Wayne Taylor (webtubbs)

Application.EnableEvents = False
Application.EnableEvents = True

Didnt Worked.
0
 
Fabrice LambertFabrice LambertCommented:
Side note:
I do not recommend changing a control's value on a onChange event, as this lead to the event triggering itself again (and the risk of entering infinite loop).
It is better to perform data validation on a push button.
0
 
WiseOwl ExcelAuthor Commented:
Experts,

No Solution Meet My Criteria Yet Please Advice??

Regards,
0
 
Aleksandr M.EconomistCommented:
@WiseOwl Excel, have you tried my suggestion? Isn't it working on your side?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now