Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing and Returning Objects

Status
Not open for further replies.

EnS

Programmer
Oct 29, 2002
58
US
Hi all,

I'm not sure if this is possible, but I want to return an object from a user-defined OCX control. Can anyone help me with this?

Background info:

I created an OCX control the receives 2 objects (form and control) and a string. I would like this OCX control to return the form instance when it's processing has been completed.

Code:
OCX initiation from calling program ...
Private Sub Form_Load()
Dim objReturn As Object
Set objReturn = AlltelLogon1.LogonToAlltel(frmDisplay, frmDisplay.Display1, strAppName) 'User-defined OCX control
If objReturn Is Nothing Then
End
Else
Unload Me
objReturn.Show
End If
End Sub

OCX code ...
Function LogonToAlltel(DisplayForm As Object, MainFrameDisplay As Object, _
ApplicationName As String) As Object

'* Controls public object variables
Set FScrn = DisplayForm
Set MScrn = MainFrameDisplay
strAppName = ApplicationName
intLoginTries = 1
AttemptLogon '* Processing functions
'* Return nothing if login fails, else return form instance
If Stat <> 0 Then
Set LogonToAlltel = FScrn
Else
Set LogonToAlltel = Nothing
End If
End Function

Problem:
I get an &quot;Object required&quot; error in the calling program when I use this method. If I change the OCX function to return as long, then the form instance is unloaded after the login has been completed successfully.

Any ideas? Your help will be greatly appreciated.

Thanks in advance.

ERM
 
Assuming the exe has a reference to the OCX, you need something like:

Dim objLogon As AlltelLogon1
Set objLogon = New AlltelLogon1

Before calling the LogonToAlltel method in form load.

Paul Bent
Northwind IT Systems
 
paulbent,

Thanks for the quick reply. I made the changes you suggested and was able to declare &quot;objLogon As AlltelLogon&quot;, but now I am getting &quot;Invalid use of New keyword&quot; with &quot;Set objLogon = New AlltelLogon&quot;. AlltelLogon is not represented in the parameter list of the New keyword like it is in the As keyword.

I doubled check the project reference and everything looks good. Is there anything I'm missing?

Thanks again for your help.

ERM
 
Make sure the class is declared as public (click on the class in your project window, and check in the properties window)

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I think I misunderstood. If the OCX is a project component and is inserted on the form you don't need to instantiate it with New, just start from a reference to the control to call a method. I see now that AlltelLogon1 is probably the name of the control and your code looks fine.

Which line in Form Load does the object required error occur on?



Paul Bent
Northwind IT Systems
 
chiph, thanks for the input, but the ocx control is currently set to public.

paulbent,
I was receiving the &quot;object required&quot; error on this line:
&quot;Set objReturn = AlltelLogon1.LogonToAlltel(frmDisplay, frmDisplay.Display1, strAppName)&quot;

But thanks to your earlier post, declaring the &quot;objReturn&quot;, or as you put &quot;objLogon&quot;, as AlltelLogon gave me a different point of view which allowed me to try something a bit different and it works great.

Just in case your courious, I moved the method call from the form_load event to a module. The overall structure looks like this:


Sub Main()
'General steps
ResetVariables
Load frmSplash
frmSplash.Show
Do Until frmSplash.Visible = False
DoEvents
Loop

'Run Login script
Login

'Used for Debuging
Do
DoEvents
Loop
End Sub

Private Sub Login()
Dim lngReturn As Long
Dim frmLogon As frmAlltelLogon
Set frmLogon = New frmAlltelLogon

'Run Alltel OCX Login script
lngReturn = frmLogon.AlltelLogon1.LogonToAlltel(frmDisplay, frmDisplay.Display1, strAppName)

'If script returns 0 then exit program
If lngReturn = 0 Then End
Set frmLogon = Nothing
End Sub

As you can see, I changed the return type of the ocx function to long; which will be easier to work with in the long run. Also, by instanciating the logon form (&quot;frmAlltelLogon&quot;) in this manner, this allowed me to keep the instance of the display form (&quot;frmDisplay&quot;) which the ocx creates if a successful logon is completed.

Thanks again paulbent, for helping me peek outside this box.


ERM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top