Compiled Error

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,075
Office Version
  1. 2019
Platform
  1. Windows
Is there a better or correct way to write this code? Currently I'm getting a compiled error.

Code:
If textbox1.setfocus then exit sub
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I have this code...

Code:
Private Sub Amnt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If CCur(Amnt) > "$0.00" And CCur(Amnt) <= "$75.00" Then
    If MsgBox("Would you like to substantiate this expense?", vbQuestion + vbYesNo, "PerDiem Traveler") = vbYes Then
        frmReceipt.Show
    End If
ElseIf CCur(Amnt) > "$75.00" Then
    MsgBox "Expenses over $75.00 requires to be substantiated.", vbInformation, "PerDiem Traveler"
    frmReceipt.Show
End If
End Sub

if I'm focused in the textbox and close the userform, the msgbox continue to display after I've already close the userform. While the I still want the msgbox to appear as appropriate, I don't want them to appear if I close the userform and I'm focused in the textbox.
 
Upvote 0
Maybe like this:
Code:
Private Sub Amnt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Const sTitle  As String = "PerDiem Traveler"
 
    If Not Me.ActiveControl Is Me.TextBox1 Then
        Select Case CDbl(Me.Amnt)
            Case Is > 75
                MsgBox Prompt:="Expenses over $75.00 must be substantiated.", _
                       Buttons:=vbInformation, _
                       Title:=sTitle
                frmReceipt.Show
 
            Case Is > 0
                If MsgBox(Prompt:="Substantiate this expense?", _
                          Buttons:=vbQuestion + vbYesNo, _
                          Title:="PerDiem Traveler") = vbYes Then
                    frmReceipt.Show
                End If
        End Select
    End If
End Sub
 
Upvote 0
Is there a better or correct way to write this code? Currently I'm getting a compiled error.

Code:
If textbox1.setfocus then exit sub
Just to provide some information which might help you in the future:-

.SetFocus is a 'method' - it's something you do to a control.

You're trying to use it as a property - a value associated with a control.

It's like the difference between washing a car (method) and the colour of the car (property): one is something you do to it, the other describes something about it.

You wouldn't say, "What's the wash of the car".
 
Upvote 0
You could move the validation to another event, perhaps even a command button that closes the form.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top