Invalid procedure call or argument: ‘Mid’

asp-classicvbscript

I have a function (see below) and it works perfectly. I recently moved my code to another server and i did not change anything in it. It fails to run on new server.

Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'Mid'
/calculate.asp, line 416 

When i checked the line 416, i got this:

Dim result3: result3 = Mid(o3.responseText, Basla3, Bitir3)

and this is the complete function:

<%
    Function xyz()
    Dim o3: Set o3 = Server.CreateObject("MSXML2.ServerXMLHTTP")
    Dim o_date3: o_date3 = split(EndingDate, ".")
    Dim s_date3
    If (Len(o_date3(2)) = 4) Then
        s_date3 = o_date3(2)
    Else
        s_date3 = "20" & o_date3(2)
    End If
    If (Len(o_date3(1)) = 2) Then
        s_date3 = s_date3 & o_date3(1)
    Else
        s_date3 = s_date3 & "0" & o_date3(1)
    End If
    If (Len(o_date3(0)) = 2) Then
        s_date3 = s_date3 & o_date3(0)
    Else
        s_date3 = s_date3 & "0" & o_date3(0)
    End If
    Dim s3: s3 = "<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""         xmlns:urn=""urn:AntTransferWSIntf-IAntTransferWS""><soapenv:Header/><soapenv:Body><urn:EURCurrency soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><DateStr xsi:type=""xsd:string"">" + s_date3 + "</DateStr></urn:EURCurrency></soapenv:Body></soapenv:Envelope>"
    o3.Open "POST", serviceUrl, False
    o3.setRequestHeader "Content-Type", "text/xml"
    o3.setRequestHeader "Connection", "close"
    o3.setRequestHeader "SOAPAction", " "
    o3.send s3
    Dim hataVarMiBasla3: hataVarMiBasla3 = (InStr(1, o3.responseText, "<faultstring>", vbTextCompare)) + 13
    If (hataVarMiBasla3 > 13) Then
        Dim hataVarMiBitir3: hataVarMiBitir3 = (InStr(1, o3.responseText, "</faultstring>", vbTextCompare)) - hataVarMiBasla3
        Dim hata3: hata3 = Mid(o3.responseText, hataVarMiBasla3, hataVarMiBitir3)
        KurGetir = hata3
    Else
        Dim Basla3: Basla3 = (InStr(1, o3.responseText, """xsd:double"">", vbTextCompare)) + 13
        Dim Bitir3: Bitir3 = (InStr(1, o3.responseText, "</return>", vbTextCompare)) - Basla3
        Dim result3: result3 = Mid(o3.responseText, Basla3, Bitir3)
        xyz = CDbl(Replace(result3, ".", mstrComma))
    End If
    Set o3 = Nothing
End Function
%>

Why am i receiving this error?

Best Answer

Mid struct from MSDN

Mid(string, start[, length])

Not official reference but according to my experience, you get that error if

  1. start is less than or equal to zero.
  2. length is less than zero (if it is not missed in the Mid call)

Have a look at the error line and related ones.

Dim Basla3: Basla3 = (InStr(1, o3.responseText, """xsd:double"">", vbTextCompare)) + 13
Dim Bitir3: Bitir3 = (InStr(1, o3.responseText, "</return>", vbTextCompare)) - Basla3
Dim result3: result3 = Mid(o3.responseText, Basla3, Bitir3)

Lets suppose o3.responseText is empty because your code does not check whether the response is empty.

Basla3 can not be less than 13 according to InStr() + 13, so it's not the problem.
However it seems like Bitir3 can be less then zero according to InStr() - Basla3 (Basla3 evaluated as 13).
Continuing with the assumption, (InStr(1, o3.responseText, "</return>", vbTextCompare)) evaluated as 0, then with - Basla3 it will be evaluated as -13.
Tada! rule 2 violated, length cannot be less than zero.
The problem with your code is, there is no check response length nor response status. If the response is empty, consider the following:

  1. Your new server may have connectivity problems unlike the old one.
  2. The API which you have is authorized for the old server's IP address only.


In a nutshell, you should optimize the code and be sure that there is an xml response.
At least use something like that:

o3.Send
If o3.readyState = 4 And o3.status = 200 Then
    If Len(o3.responseText) > 0 Then
        'response is ready to parse
    Else
        'response status is ok but empty
    End If
Else
    'request failed
End If

BTW, due to your request is a soap call, I'd highly recommend done the job by parsing xml response using DomDocument etc.
Replacing decimal points, using Mid & InStr pair to check node existence are just trouble and bad practice also.

Related Topic