Search code examples
vbams-word64-bitword-2016

Always Same Custom Error Message - VBA Word 64bit


I am creating a program in VBA for Word 2016 64bit.

If someone inputs letter into a TextBox formatted for integers, it shows a error message.

How can I make it not allow the user to input anything other than an integer? Can I have it check when the user clicks the Submit button? Here is an example of the code of one TextBox:

Private Sub CurrentRate_AfterUpdate()
    If CurrentRate.Text - CurrentRate.Text > 0 Then
        CurrentRate.Value = Format(CurrentRate.Value, "Percent")

    Else
        CurrentRate.Text = CurrentRate.Text / 100
        CurrentRate.Value = Format(CurrentRate.Value, "Percent")
    End If
End Sub

Basically it is taking the value of currentRate and doing a math operating and then formatting it as a percent. If it is not an integer it can not do the math or format it as a percent which is the cause of the error.

Thanks in advance for your help!


Solution

  • Going off of what Andy G said, I found I should check if the value is a number, if it is do the formula, if it is not do a message box.

    Private Sub CurrentRate_AfterUpdate()
        If IsNumeric(CurrentRate.Text) Then
    
            If CurrentRate.Text - CurrentRate.Text > 0 Then
                CurrentRate.Value = Format(CurrentRate.Value, "Percent")
    
            Else
                CurrentRate.Text = CurrentRate.Text / 100
                CurrentRate.Value = Format(CurrentRate.Value, "Percent")
            End If
    
        Else
            MsgBox("Invalid Entry, Please Try Again")
            CurrentRate.Text = ""
    
        End If
    End Sub