• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

VB5 Passing listboxes & option buttons to a function

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
bemson
Asked:
bemson
  • 6
  • 3
  • 2
1 Solution
 
beckinghCommented:
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
 
beckinghCommented:
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
 
bemsonAuthor Commented:
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
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.

 
beckinghCommented:
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
 
beckinghCommented:
That function is located in a module, and is called from forms and usercontrols.
0
 
brookmanCommented:
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
 
beckinghCommented:
brookman:

I'm pretty sure Subs and Functions are Public by default.
0
 
bemsonAuthor Commented:
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
 
beckinghCommented:
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
 
bemsonAuthor Commented:
Looks like I will use code as suggested above

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

Featured Post

Independent Software Vendors: 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!

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now