Solved

VB5 Passing listboxes & option buttons to a function

Posted on 2001-06-12
11
253 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now