View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default VB6 to VBA conversion. Form vs. UserForm differences. Object variable types

Rick,

In Excel/VBA there are two distinct objects named ListBox. The
first is in the Excel typelib is the list box that is available
on the Forms command bar. The second is in the MSForms typelib,
and it the one you use on UserForms. Because the Excel typelib
has a higher priority in the compiler than the MSForms typelib,
the compiler uses Excel's ListBox. You need to qualify ListBox
with the name of the typelib in which it resides.

Change

Private m_listBox1 As ListBox
to
Private m_listBox1 As MSForms.ListBox

Using 'As Object' works because Object can hold any sort of
object, and the assignment is made at run time, not compile time.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Rick Labs" wrote in message
om...
Hi,

I'm converting some VB6 forms and classes to run in VBA. Having

some real
trouble with converting forms in general and the class modules

behind the
forms. Below is a highly simplified example of the trouble -

basically
a "Type Mismatch" situation.

I've been doing a ton of F8'ing and looking at both the Locals

Window and
the Watches window to try to see why there is a mismatch. Also

fooled
around with:

varname1 Is varname2 [tests if both point to same object]
IsObject(identifier) [simple true/false]
TypeName(varname) [returns the type name of the variable,

"object" if
object.]

In VB you Dim as Form
In VBA you Dim as userForm or

"the-actual-name-of-the-form-class"

Before I throw in the towel and just go with a generic "object"

type take a
look at this and see if you can point the way out. (example

below)

Any additional info on VBA vs. VB (Forms vs. UserForms) would

be greatly
appreciated. I'm new to class modules and event processing and

its been a
steep learning curve. Any other great references, books,

tutorials very
much appreciated.

Yes, after I "down grade" the code from VB6 to VBA (Office

2000) I need to
upgrade it to VB.NET. Fun!

THANKS!!!

Rick


[create a new form with one ListBox1 on it]

Private i As Integer
Private m_listBox1 As ListBox '<=== why doesn't this work?
'Returns run time error 13 type mismatch???
'Private m_listBox1 As Object '<=== yet this does

Private Sub UserForm_Initialize()
i = 0
Set m_listBox1 = Me.ListBox1
End Sub

Private Sub UserForm_Click()
i = i + 1
m_listBox1.AddItem (i)
End Sub