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

WiseOwl ExcelFinance ExxcutiveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 ExcelFinance ExxcutiveAuthor 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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WiseOwl ExcelFinance ExxcutiveAuthor 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 ExcelFinance ExxcutiveAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.