[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ActiveX Form problems...

Posted on 2004-11-05
6
Medium Priority
?
172 Views
Last Modified: 2010-05-02
Hello,

I am working on an activex form in VB6 and have run up against a problem that I am certain is due to my lack of understanding of how classes work. The form is called from a class (clsMain) with this function:


Public Function get_form(filename As String, vData As Variant) As Variant

Dim frm As frmMain
varData = vData
Set frm = New frmMain
frm.Show vbModal
get_form = varReturn
Unload frm
Set frm = Nothing

End Function

The form (frmMain) is made up of listboxes that are filled from a database. In order to simplify the setting and returning of different configurations of selected values in these listboxes I have added another class named clsSettings. The object that instantiates the class holds the listboxes’ current values as a variant which is set/updated by a method (named Update). In order to get the current values – I iterate through the form’s controls

i.e.:
For Each ctl In frmMain.Controls
[if listbox then get current value and add to array]
next ctl

then I set a public property in the clsSettings to the result.

This works as expected when I run the program from VB or as a compiled exe. I can change the values in the lists and each time I test the Update method of the clsSettings object, the new values are returned. However, when I invoke the dll with get_form (from clsMain) then the object only returns the values that were set when the form (frmMain) was originally called. The Update function iterates through the controls as expected but doesn’t recognize that the listbox values have changed.

Have I explained this well enough for anyone to see my error? If I can’t manage to save/put the settings with a class, then I guess that I will need to move all of the code/variables from the clsSettings directly into the frmMain code (which will be simple enough). It seems to me, though that the approach that I am taking now is the better one and certainly will be simpler to maintain – so I’ll hope for some advice/insights from the experts in this forum.

Thanks in advance,

Peter
0
Comment
Question by:petersciganek
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12504339
your code is never passing the values to the form.  A form is a Class, and as such has properties that can be set and read.

Try this code in the form, and the the routine that you showed here:

in the form's code:

in frmMain.frm

Dim m_Data as Variant

Public Property Get vData() as Variant
    set vData = m_Data
End Property

Public Property Set vData(RHS as Variant)
    Set m_Data = RHS
End Property

Private Sub Form_Load()

'  here you would use m_Data to do whatever you need to do with the data when the form is loaded

End Sub


then, revise your posted code like this:


Public Function get_form(filename As String, vData As Variant) As Variant

Dim frm As frmMain
varData = vData
Set frm = New frmMain

' now pass the data to the form, as a REFERENCE...
Set frm.vData = varData

' when the form is shown, you can the change the data as necessary
frm.Show vbModal

'now you get the changed data back from the form -- since the REFERENCE was passed, all changes made using the form, are reflected in the 'local' vData instance
get_form = vData

Unload frm
Set frm = Nothing

End Function

see if that gets you what you want.

AW
0
 

Author Comment

by:petersciganek
ID: 12505593
Arthur,

Thank you for taking time to look at this - and thank you for the advice on passing and returning data to the form. I am using a public variable defined in a module to do this for the 'filename' argument (which I left out of the code section that I posted) and varReturn is also defined as a public variable in the same module. Because the filename variable is used in a number of different classes and forms that are called from and used by frmMain - this seems to be the best way to store that variable(?). I will modify the code to as you suggest for the vData variable.

However, the problem isn't in passing values back and forth between clsMain and frmMain - rather, it's in getting the clsSettings object (created in frmMain) to have access to the current state of frmMain (i.e. - the listbox values). While the value that fmMain will return to clsMain is ultimately derived from the clsSettings object - this seems to be unrelated to my current question (though please correct me if I'm wrong). My guess is that I need to change the way that I gather the listbox values from the clsSettings class [ For Each ctl In frmMain.Controls ... Next ctl]. It is as if the frmMain being iterated by the clsSettings function is a different object (or collection) than the one that is visible on the screen when frmMain is invoked from another class. I'm missing something here - just don't know what...

Peter
0
 

Author Comment

by:petersciganek
ID: 12521459
Arthur,

You pointed me in the right direction, I managed to figure out what I was doing wrong over the weekend...

I added the form (frmMain) as a property to clsSettings (i.e.):

Private mvarmForm As Object 'local copy

Public Property Set mForm(ByVal vData As Object)
    Set mvarmForm = vData
End Property

Public Property Get mForm() As Object
    Set mForm = mvarmForm
End Property

then set this property in the frmMain's code before invoking the Update method:

Private Function TestSettings()
    Dim oSet As clsSettings
    Dim s$
   
    Set oSet = New clsSettings
   
    Set oSet.mForm = Me  '*****set form here....
    oSet.Update
    s$ = oSet.StringValue
   
    MsgBox s$

   
    Set oSet = Nothing
   
End Function

and now it works as expected. Note that if I use

 Set oSet.mForm = frmMain

instead of

 Set oSet.mForm = Me

that it will not return the current settings. On a recent project I discovered that I needed to use 'Me' when referencing the activeX form rather than by name - though I would have thought that they both point to the same object.

Thanks for taking the time to look at this -

Peter
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 750 total points
ID: 12527892
ok, I guess.  I don't see how you are using

mvarmForm

in your code, but if you are getting what you want, then glad to be of assistance.

AW
0
 

Author Comment

by:petersciganek
ID: 12530803
mvarmForm is the local (private) variable pointing to frmMain.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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