The error message "Execution phase error '94', incorrect use of null" appears because the IsNull function is not used correctly. The IsNull function returns True if the expression is Null; otherwise, it returns False. In your code, the first IIf statement checks if the value of race is Null. If it is Null, then the value of Chk_Flag is set to True. If it is not Null, then the second IIf statement checks if the value of race is less than 0 or greater than 3. If it is less than 0 or greater than 3, then the value of Chk_Flag is set to True. Otherwise, the value of Chk_Flag is set to False.
The problem is that you are trying to use the Val() function on a string. Val() can only be used on numbers, so when you try to use it on a string, you get an error.
To fix this, you need to convert the string to a number before you use the Val() function. You can do this using the CStr() function. For example, you could use the following code:
Chk_Flag = IIf(IsNull(race), True, IIf(CStr(race) < 0 Or CStr(race) > 3, True, False))
The CStr() function converts a string to a number. So, in the example code, the CStr() function will convert the value of the race variable to a number. Then, the Val() function will be used to check if the number is less than 0 or greater than 3. If it is, then the Chk_Flag variable will be set to True. Otherwise, it will be set to False.
Or.
Here's how you can modify your code:
Chk_Flag = IIf(IsNull(race), True, IIf(Nz(race) < 0 Or Nz(race) > 3, True, False))
The error message appears because you are trying to use Val() function on a Null value. You can use Nz() function instead of Val() function to handle Null values. Nz() function returns a specified value if a Variant is Null; otherwise, it returns the value of the Variant.
IsNull function (Visual Basic for Applications) | Microsoft Learn. https://learn.microsoft.com/en-u ... lp/isnull-function.
VBA IsNull Function - Automate Excel. https://www.automateexcel.com/vba/isnull-function/. |