Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing Controls to Active X Components

Posted on 2001-06-21
10
Medium Priority
?
246 Views
Last Modified: 2013-11-25
Is it possible to pass a control, say List Box, to an Active X to populate?

e.g The Active X populates List1 with numbers 1 - 5, I want to call this several times in several apps but pass different listbox references to List1.

Cheers,
R
0
Comment
Question by:rallsaldo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6214479
Yes, it is possible...
Have you some specific problem with this?
You might note that the parameter type should be Control and not Listbox...

Public Sub FillListBox ( list as Control )
  dim l as ListBox
  set l = list

  l.AddItem "1"
  l.AddItem "2"
  ...

End Sub

CHeers
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6214609
Cheers a,

I have tried the control approach....

What exactly I am trying to do is have a private reference to mlstListBox in my class. I have a Let statement which allows me to set the value of mlstListBox to whatever Listbox name I choose, then a public sub which populates this mlstListBox.

Thus enabling me to have a dll which populates a listbox with a set of items regardless of what listbox I pass.

Currently it is falling over at the Let statement with the comment "Private object modules cannot be used in public object models as parameters or return types for public procedures, as public data members, or as fields of publuc user defined types"

Cheers,
R
0
 
LVL 15

Expert Comment

by:ameba
ID: 6214756
There are two ways to solve this.

' 1. Pass Listbox 'As Object' - this is Late bound call (slow)
' Class1 in ActiveX Dll
Option Explicit

Public Sub Populate(List As Object)
    List.Clear
    List.AddItem "Hello"
End Sub

' usage:
Private Sub Form_Click()
    Dim x As Project2.Class1
    Set x = New Class1
    x.Populate List1
End Sub



' 2. Early bound solution - add variable in Class1 declarations
' Class1 in ActiveX Dll
Option Explicit
Public List As ListBox

Public Sub Populate()
    If Not List Is Nothing Then
        List.Clear
        List.AddItem "Hello"
    End If
End Sub

' usage:
Private Sub Form_Click()
    Dim x As Project2.Class1
    Set x = New Class1
    Set x.List = List1
    x.Populate
End Sub
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6214759
First of all, to assign a Listbox, you need to use the SET statement...

Maybe you post your relevant code, so we can correct it?

---------Class code-------
private mlstListBox as ListBox

Public Property Set WorkListBox ( nv as Control )
  set mlstListBox = nv
End Property

Public Sub PopulateListBox()
  with mlstListBox
     .AddItem "1"
     .AddItem "2"
     ...
  end with
End Sub

--- Other code ----

with YourClass
  Set .WorkListBox = listbox1
  .Populate
  Set .WorkListBox = listbox2
  .Populate
end with



Cheers
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6214907
Cheers folks... I've pretty much tried this anyhow.

This is the code:

---Class Code---
Private mlstListBox As ListBox

Public Property Set MyListBox(vListBox As Control)
   
    Set mlstListBox = vListBox
       
End Property

This is were it is failing. If you paste it into a new Class you'll see what I mean when trying to compile.

This is probably blindingly obvious but got me stumped!!

Cheers,
R
0
 
LVL 15

Accepted Solution

by:
ameba earned 300 total points
ID: 6214923
I tested both solutions I posted and they worked. I suggest 2nd solution.

' usage (code in a form with a listbox):
Private Sub Form_Click()
   Dim x As Project2.Class1
   Set x = New Class1
   Set x.List = List1
   x.Populate
End Sub
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6215242
Hi Ameba,

Yep this works fine ~ thanks ~ I've always declared variables as private in a class and then used the property let, property get to expose them to users. I take it this is different with controls?

Cheers,
R
0
 
LVL 15

Expert Comment

by:ameba
ID: 6215293
>I take it this is different with controls?

Well, if you want early binding, and if code is in a different project, yes, that is the syntax that is supported by VB.

Public X As ListBox
or:
Public WithEvents X As ListBox
0
 
LVL 15

Expert Comment

by:ameba
ID: 6217727
B?  What was wrong with my answer?  You got perfect expertize on how to do what you want with code samples (both code samples required creating program group)

Do not expect any further help from me!
0
 
LVL 4

Author Comment

by:rallsaldo
ID: 6218701
Ameba,

It was my first question asked and I was simply following the guidleines set out on the response page...

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

If you don't think that I awarded fairly then I apologise, I am grateful for your help, perhaps I should award A's to everyone... then everyones happy.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question