Solved

VB5 Passing listboxes & option buttons to a function

Posted on 2001-06-12
11
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That function is located in a module, and is called from forms and usercontrols.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:brookman
Comment Utility
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
Comment Utility
brookman:

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

Author Comment

by:bemson
Comment Utility
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
Comment Utility
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
Comment Utility
Looks like I will use code as suggested above

thanks to both
0
 
LVL 2

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now