Solved

Passing Controls to Active X Components

Posted on 2001-06-21
10
243 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 100 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 8 hours left to enroll

636 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