简体   繁体   中英

Invalid Variant Operation error

I have an error in my button click, and I can't figure out how to resolve it.

This is my code:

if (ovElements.item(i).name = 'add') and    
(ovElements.item(i).type = 'button') and    
(ovElements.item(i).Value = ' + ') then    
ovElements.item(i).Click;

This is the markup:

<td width="20" align="left"><input class="button" style="width: 30px;" 
 name="add" value=" + " onclick="addLvl();" type="button"></td>

And it gives this error:

Invalid Variant Operation Error

What did I do wrong?

It means an operation on a variant which is executed is invalid. This happens, for example, when a variant containing some text is divided by an integer. Clearly this cannot work, but since the compiler can't check this, it is a runtime error.

Use a temporary variable for the 3 parts in your if statement to see better on which line the error is raised. Then inspect what the values are and what the invalid operation is.

Just a guess:

ovElements.item(i).Value is probably a Variant. If a variant contains a null value you will get that error when you compare it to a string.

Make sure ovElements.item(i) doesn't contain a null value before comparing it.

You can save "ovElements.item(i)" to a local variable and then split your code into multiple line.

obj = ovElements.item(i);
if obj <> nil then
try
  if obj.name = 'add' then
    if obj.type = 'button' then
      if obj.value = ' + ' then    
        obj.click;
except
end;

In this way you can see which line causes this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM