Problem with IIf and '['] '

0

In a text box of a form, you can enter a code only with normal characters or characters followed by another code in brackets: [xxxxxxx]. Depending on the type of entry, you must return one chain or another.

If the chain has brackets, they are removed. If it does not take them, it gives an error, because it looks for them equally. The goal is to ensure that this does not happen. In the main part of the program, I have:

If Me!Proveedor = "BIMBO" Then
Me!NombreProducto = Left(BuscaNombre("BIMBO", Mid(Me!Producto, InStr(1, Me!Producto, "[") + 1, (InStr(1, Me!Producto, "]")) - (InStr(1, Me!Producto, "[")) - 1)), 50)

The function that it calls has arguments:

BuscaNombre(ByVal Tipo As String, ByVal Codigo As String) As String

The value of Product, which is a text box that is in the active form, is in this case "zxc". What I think happens with this code is that when using the Mid, inStr and others to give value to the argument, if in the input string it does not find symbols of the type [o] that are what it seems to be looking for, it gives an error.

If this hypothesis is correct, how could it be solved so that in the event that there are no input symbols, the code will correctly enter the function?

One possible solution is this:

Me!NombreProducto = iif(InStr(1, Me!Producto, "["), Left(BuscaNombre("BIMBO", Mid(Me!Producto, InStr(1, Me!Producto, "[") + 1, (InStr(1, Me!Producto, "]")) - (InStr(1, Me!Producto, "[")) - 1)), 50), Left(BuscaNombre("BIMBO", Me!Producto.text), 50)

But it does not work, the VBA Access text editor gives an error. Does anyone have a clue as to why?

    
asked by AccOUCH 16.11.2018 в 10:47
source

1 answer

0

The solution would be to divide the IF according to brackets or not:

If InStr(1, Me!Producto, "[")>0 or InStr(1, Me!Producto, "]")>0 Then
'hacer algo si hay corchetes
Else
'hacer otra cosa cuando no hay corchetes
End If
    
answered by 19.11.2018 / 23:19
source