yassin092898
asked on
VB COM in asp file
I created VB COM I want to use in my web based application.
the COM poject is ActiceX dll and it is called COM_ASST.
the only class in the COM is Called
C_ASST
and the class contains one function
here is my COM class definition
-------------------------- ----------
Public Function GetNextRecordNumber(ObjCon As ADODB.Connection, companyName As String) As Long
Dim rs As Recordset
Dim cmd As Command
Dim param As Parameter
Set cmd = New Command
With cmd
.ActiveConnection = con
.CommandText = "spGetNextRecordNumber"
.CommandType = adCmdStoredProc
End With
Set param = cmd.CreateParameter("compa nyName", adVarChar, adParamInput, 40, companyName)
cmd.Parameters.Append param
Set rs = cmd.Execute
GetNextRecordNumber = rs!NextRecord
rs.Close
Set rs = Nothing
End Function
------------- end -------------------
I have written a small VB test program and it works fine.
When I use it in an asp file
-------------------------- ---------- -
<%@ Language=VBScript %>
<%
dim obj
num = 0
Set objConn = Server.CreateObject("ADODB .Connectio n")
objConn.Open "testDB", "sa", ""
set obj = server.CreateObject("COM_A sst.C_Asst ")
num = obj.GetNextRecordNumber objConn, "Customers"
Response.write num
%>
-------------------------- -------
I get the following error
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'GetNextRecordNumber'
/p.asp, line 11
the COM poject is ActiceX dll and it is called COM_ASST.
the only class in the COM is Called
C_ASST
and the class contains one function
here is my COM class definition
--------------------------
Public Function GetNextRecordNumber(ObjCon
Dim rs As Recordset
Dim cmd As Command
Dim param As Parameter
Set cmd = New Command
With cmd
.ActiveConnection = con
.CommandText = "spGetNextRecordNumber"
.CommandType = adCmdStoredProc
End With
Set param = cmd.CreateParameter("compa
cmd.Parameters.Append param
Set rs = cmd.Execute
GetNextRecordNumber = rs!NextRecord
rs.Close
Set rs = Nothing
End Function
------------- end -------------------
I have written a small VB test program and it works fine.
When I use it in an asp file
--------------------------
<%@ Language=VBScript %>
<%
dim obj
num = 0
Set objConn = Server.CreateObject("ADODB
objConn.Open "testDB", "sa", ""
set obj = server.CreateObject("COM_A
num = obj.GetNextRecordNumber objConn, "Customers"
Response.write num
%>
--------------------------
I get the following error
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'GetNextRecordNumber'
/p.asp, line 11
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Marine - that's not correct. You only need to pass things ByRef if you want to be able to read the passed values back from the COM component. This applies to simple types such as String, Integer etc. Objects are always passed ByRef because you are actually passing a pointer (if you try and compile a VB component with an Object passed ByVal, it generates an error). In VB, if you don't specify a passing type (like yasin has done), it is considered to be ByRef anyway, so changing it to ByRef will have no effect. Yasin's problem has more to do with the fact that VBScript does not support typed variables, only variants.
Incidentally, strings are also pointers so it is normally faster to pass strings ByRef than ByVal. This is however not necessarily true for COM components because cross-component marshalling typically takes longer than just a single marshalling call to make a copy of the string when the COM Component's method is called. The same applies to thread marshalling calls, so if you are writing an apartment threaded component for use within a muti-threaded Visual Basic Application and you don't need to read back strings, rather pass them ByVal - it is faster. If you do need to read the parameter back from the method, then make a copy of the string into a local variable and operate on the local variable. Before you exit the method, assign the local variable to the passed one.
Incidentally, strings are also pointers so it is normally faster to pass strings ByRef than ByVal. This is however not necessarily true for COM components because cross-component marshalling typically takes longer than just a single marshalling call to make a copy of the string when the COM Component's method is called. The same applies to thread marshalling calls, so if you are writing an apartment threaded component for use within a muti-threaded Visual Basic Application and you don't need to read back strings, rather pass them ByVal - it is faster. If you do need to read the parameter back from the method, then make a copy of the string into a local variable and operate on the local variable. Before you exit the method, assign the local variable to the passed one.
Sorry , sorry i meant to say byVal.
Public Function GetNextRecordNumber(byref ObjCon As ADODB.Connection, byRef companyName As String) As Long