Cannot use parentheses when calling a Sub

前端 未结 3 865
南旧
南旧 2021-01-28 02:15

I have a Classic ASP page that contains the following code to attempt a parametised query;

<%
Set cmdEmail = Server.CreateObject(\"ADODB.Command\")
Set rsEmai         


        
相关标签:
3条回答
  • 2021-01-28 03:07

    Aren't you missing the "Set" statement in there?

    ie:

    <%
    Set cmdEmail = Server.CreateObject("ADODB.Command")
    Set rsEmail = Server.CreateObject("ADODB.Recordset")
    

    UPDATE:

    In response to Neil's comment of:

    Thanks CraigTP. It seems to be creating the instance of ADODB.Command and ADODB.Recordset, but having issues witht he last 4 lines of the code.

    I think the last lines of code, should read something like:

    cmdEmail.CommandText = "SELECT * FROM VWTenantPropertiesResults WHERE ContentID = ?"
    cmdEmail.CommandType = 1
    cmdEmail.ActiveConnection = MM_dbconn_STRING
    Set adoParam = cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form("ContentID"))
    adoParam.Type = [the datatype of the parameter]
    cmdEmail.Parameters.Append(adoParam)
    

    Note that the .CreateParameter method will return a Parameter object. You should assign this returned object to a variable which you then use as a parameter to the .Append method on the Command object's Parameters collection.

    See these links for more information:

    CreateParameter Method (ADO)
    Parameters Collection (ADO)
    Append Method (ADO)

    Note the section headed "Remarks Parameters Collection" where it states:

    You must set the Type property of a Parameter object before appending it to the Parameters collection.

    The .Type property of the Parameter object takes a value from the DataTypeEnum enumeration to specify the data type of the parameter. Replace the [the datatype of the parameter] bit of my code above with the relevant data type enum value.

    UPDATE 2:

    Sorry, didn't notice the question title text had changed!

    Ah.. The old classic "Cannot use parentheses when calling a Sub" error, eh?

    Well, this is explained here:

    Cannot use parentheses when calling a Sub

    In a nutshell:

    You invoked a subroutine without the Call statement, but used parentheses (). When calling a subroutine without the Call statement, do not use parentheses.

    To correct this error:

    • Remove the parentheses from the subroutine invocation.
    • Use the Call statement to invoke the subroutine instead.

    There's also a blog post from Eric Lippert that addresses this common error:

    What do you mean "cannot use parentheses?"

    0 讨论(0)
  • 2021-01-28 03:12

    Did you tried just to remove those parenthesis?

    cmdEmail.CreateParameter "@ContentID", 3, 1, , Request.Form("ContentID")

    As far as I remember, that always happens when you call a function and doesn't use its return value.

    UPDATE: Seems the real problem is the line break:

    
    cmdEmail.Parameters.Append _     '' note this "_" character
        cmdEmail.CreateParameter("@ContentID", 3, 1, , Request.Form("ContentID"))
    0 讨论(0)
  • 2021-01-28 03:18
    set cmdEmail = Server.CreateObject("ADODB.Command")
    

    Try and put some debug statements to check, if it is creating an instance of ADODB.Command.

    e.g.

    If (cmdEmail is Nothing) Then
       Response.Write("could not create the instance")
    Else
       Response.Write("created the instance")
    End If
    

    Also, remove the With block and see if that makes any difference

    dim paramContentID
    
    cmdEmail.CommandText = "SELECT * FROM VWTenantPropertiesResults WHERE ContentID = ?"
    cmdEmail.CommandType = 1
    cmdEmail.ActiveConnection = MM_dbconn_STRING
    
    set paramContentID = cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form("ContentID"))
    cmdEmail.Parameters.Append paramContentID
    
    0 讨论(0)
提交回复
热议问题