user1342164 user1342164 - 2 years ago 68 Question

Input string was not in a correct format when doing string comparison

I am trying to evaluate if a user account equals a certain string that it does not continue. For example, if the user account is AMTEST or PMTEST that it does not execute the code in the if statement. Using the code below I am getting the error that "Input string was not in a correct format."

Something wrong with my syntax?

If UserAccount <> "AMTEST" Or "PMTEST" Or "TESTUSER" Then
' do something
End If

Answer Source

You have to use this syntax:

If UserAccount <> "AMTEST" AndAlso UserAccount <> "PMTEST" AndAlso UserAccount <> "TESTUSER" Then

End If

easier to read and to maintain would be this approach:

Dim testAccs = { "AMTEST", "PMTEST", "TESTUSER" }
If Not testAccs.Contains( UserAccount ) Then

End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download