Solved

VB5 Passing listboxes & option buttons to a function

Posted on 2001-06-12
11
262 Views
Last Modified: 2008-03-06
VB5

I want to pass various objects to a function or sub

A sample of what I want to do is shown below:

##########################

Sub Form_Load()

     dim ActiveForm as Form

     Set ActiveForm = me

End Sub

##########################

Sub Anything()

     ShowDetails(lstDisplay,optChoice())
End sub

###########################

Sub ShowDetails(ListBox as listbox, OptionButton() as optionbutton)

     for j = 0 to activeform.ListBox.listcount -1
     
     if ActiveForm.OptionButton(0).value = true then
          ' do something
     else
          ' do something else
     end if

     Next j

End sub

##################################


the sub "ShowDetails" I want in a module useable by all forms, depending upon which one is active

What I want to do is vary the listboxes & optionbutton names because I have same info required on different forms & tab controls.

I have had some limited success but having problems with identifying the index for option buttons

How do I retain the original forms option button index?
0
Comment
Question by:bemson
[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
  • 6
  • 3
  • 2
11 Comments
 
LVL 4

Expert Comment

by:beckingh
ID: 6183713
Your problem may be that you are trying to access your parameter "OptionButton" through your ActiveForm variable when you should just access it directly.

i.e. try:

    if OptionButton(0).value = true then
         ' do something
    else
         ' do something else
    end if

also, do you want to access the jth OptionButton or always the first one (index 0)?
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6183715
Try this:

Sub ShowDetails(ListBox as listbox, OptionButton() as optionbutton)

    for j = 0 to activeform.ListBox.listcount -1
   
       if OptionButton(j).value = true then
         ' do something
       else
         ' do something else
       end if

    Next j

End sub
0
 

Author Comment

by:bemson
ID: 6183758
Sorry, but you must have missed the point, I cannot access the option button from a MODULE without referring to the form, hence the use of active form.

Also I need to use same code (written once only) but used by various forms/tabs hence the need for code in the module, being accessible from all forms/tabs
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 4

Accepted Solution

by:
beckingh earned 100 total points
ID: 6183767
But if you pass the Listbox and the OptionButton() as parameters to your Sub why can't you access them?  For example, I have a generic function I call which I use to disable any control.  It looks like this:

Public Function DisableField(ByRef aoControl As Object)
    aoControl.Enabled = False

    If not TypeOf aoControl Is CheckBox Then
        aoControl.BackColor = vbButtonFace
    End If
End Function

As you can see, I am passing a control to the function and then accessing it without referencing the form.  You should be able to do something very similar.
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6183773
That function is located in a module, and is called from forms and usercontrols.
0
 
LVL 2

Expert Comment

by:brookman
ID: 6183820
I think the answer you are looking for is simpler than beckingh is trying to describe.  instead of

private sub ....


change it to

public sub ......

then you can call it from its form name.object name
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6183840
brookman:

I'm pretty sure Subs and Functions are Public by default.
0
 

Author Comment

by:bemson
ID: 6184035
For beckingh Thanks for comments so far, also brookman.

I have the problem I want to solve in a major app & didn't want to mess with it, so I built a new project with just the deatil I needed to try it out.

My original idea using activeform.etc has been the result of how I was taught & has always worked so far, obviously there is more than one way to acheive same result.

Using my deductions from your sample code I have re-engineered the problem as follows:

the key seems to be 'byref'

Sub CheckOption(ByRef sample As Object)

    If Not TypeOf sample Is OptionButton Then
        If sample(0).Value = True Then
            MsgBox sample(0).Caption
        Else
            MsgBox sample(1).Caption
        End If
    End If
   
End Sub

will leave question open for any other suggestions at the moment

thanks again
to me it seems a strange way of getting the result I wanted but it works
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6184058
I think its likely you can skip the line:

   If Not TypeOf sample Is OptionButton Then

I just put that in cause it was in my code :)  But the rest of it makes sense.  

Basically you're passing in a reference to the actual object on your form, so you can avoid having to reference the form itself - which is what was making your original code complicated.
0
 

Author Comment

by:bemson
ID: 6184578
Looks like I will use code as suggested above

thanks to both
0
 
LVL 2

Expert Comment

by:brookman
ID: 6186495
not to be a di** but subs are private by default. I checked to make sure before I made the comment!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…

739 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