Consulting

Results 1 to 5 of 5

Thread: Solved: prevent msgbox display

  1. #1

    Solved: prevent msgbox display

    How can I prevent a msgbox from displaying if I'm still focused in the textbox? Currently, if I enter a value over 75.00, I get the msgbox (which works properly when I exit the textbox), but if I never exit the textbox, but close the userform, the msgbox still displays. How can I suppress this from displaying if I'm still focus on the textbox?



    [VBA]Private Sub Amnt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If CCur(Amnt) > "$75.00" Then
    MsgBox "Expenses over $75.00 requires to be substantiated.", vbInformation, "PerDiem Traveler"
    frmReceipt.Show
    End If
    End Sub[/VBA]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you don't exit the textbox then the MsgBox will not display. So, I don't see the issue. Why not use another textbox or static text control to show a message? You could also use another Userform to show the message so that you can have more control.

  3. #3
    I've tried this code, but doesn't work. Is there a different way to write this

    [VBA]If Amnt.SetFocus Then Exit Sub[/VBA]

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Aviator,

    I'd echo Kenneth in considering another label or something for the warning. That said and as you experienced, the exit event will fire if the control has focus when the form is closed. Maybe set a flag?
    Option Explicit
        
    Dim bolClosing As Boolean
        
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        If Len(TextBox1.Value) < 4 And Not bolClosing Then
            MsgBox "Expenses over $75.00 requires to be substantiated.", vbInformation, "PerDiem Traveler"
            'frmReceipt.Show
        End If
    End Sub
        
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        bolClosing = True
    End Sub

  5. #5
    GTO, it's exactly what I was looking for. Thank you

Posting Permissions

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