cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in using OCX in VB.Net - error information "Bad variant type"

Former Member
0 Kudos

Hi,

I'm using OCX components in VB.Net,to connect to SAP and call BAPIs or ABAP functions,just like in VB(because I don't know how to use .Net Connect).Now I have the problems as described following:

When I test my project in a VB.Net windows form,it works very well.The test code(test no.1):

<b> Dim ojob As SAPJobInventory

ojob = New SAPJobInventory

ojob.Start()</b>

But when I create a new thread to run this code,the error occurs.The test code(test no.2):

<b> Dim ojob As SAPJobInventory

Dim othr As ThreadStart, o As Thread

ojob = New SAPJobInventory

othr = New ThreadStart(AddressOf ojob.Start)

o = New Thread(othr)

o.Start()</b>

The error raised at the second line in the code below:

<b>oSAPFunction = CreateObject("SAP.Functions")

oSAPFunction.Connection = oSAPConnection</b>

I have set logon parameters to oSAPConnection and call the Logon method successfully,and <b>the same code excecute correctly in test No.1,but error in test No.2</b>

The detail error message is:

err.Number:458

err.Description:Bad variant type

err.Source:wdtaocx

Could anybody have any ideas about it,or have ever met the problem just like so,or could give me some suggestions?

Thanks very much!

Best Regards.

Richie

Message was edited by: cloud yin

Message was edited by: cloud yin

Message was edited by: cloud yin

Message was edited by: cloud yin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

An ActiveX control must be run in an STA apartment. Because the attribute [STAThread] is applied to the Main method of a WinForm class by default with a WinForms Project, the main thread of your app will run in STA mode. This is why your test no.1 works. However, if you want to use the ActiveX Controls in a programatically created thread, you need to set the thread to STA mode manually before you starting the thread:

o = New Thread(othr)

o.ApartmentState = System.Threading.ApartmentState.STA

o.Start()

Hope it helps,

Guangwei

Former Member
0 Kudos

Dear Guangwei,

Thanks a million!It solved my problem!

Actually my problem is mostly relative to the field of .Net or Thread in programing.I was not sure that it could be solved here,and turned to Microsoft for help.

Appreciated!

Best Regards.

Richie

Former Member
0 Kudos

Guangwei,


Would you please give more details?


I do not know where this code is supposed to be placed or the meaning of "o" and "othr".


Dave

Answers (0)