Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code efficiency refer to all checkboxes 1

Status
Not open for further replies.

NewTexican

Technical User
Dec 10, 2004
95
US
I'm still getting up to speed on vb and programming in general. I have a bunch of checkboxes. when user checks a box a record is created in a table. Right now I have

Private Sub Check19_Click()
dim vari as long
vari =1
code

Private Sub Check20_Click()
dim vari as long
vari =2
code

Private Sub Check21_Click()
dim vari as long
vari =3
code
.
.
.
etc.

all these subs call another sub and pass vari (and more) to it.

What would the code that bunched all these check clicks together look like? Thanks.
 
There is no way to bunch them together. Each control must have its own click event. The only saving you can make is to define vari at module level (below the OptionExplicit statement)
 
Well, FancyPrairie advocates a method of creating a public function, then use that in the event property of the controls (you may select all the controls at a time, then the property dialog allows you to alter properties for them all):

[tt]=mytoggle()[/tt]

and in the function, if the values and names are as consistent as you've showed, perhaps something like this?

[tt]public function mytoggle()
vari = val(right(screen.activecontrol.name,2)) - 18
' continue with other stuff
end function [/tt]

I suppose I'd tried using a naming convention consistent with the values you wish to pass. There might be some more hints here too faq702-5010.

Roy-Vidar
 
ok I guess this is going to bring me to a whole new level here. what is the , 2 and the - 18?
 
2 is the second arguement to the right function (should have used right$() which is more efficient on strings, though) to retrieve the 2 rightmost characters from the controlname. 18 is the difference between the two rightmost characters (numbers 19, 20, 21 ) of the control name and the numbers you indicate you are after (numbers 1, 2, 3).

Roy-Vidar
 
Ok, that's pretty cool and clever, thanks.
The = mytoggle() where does that go?

My guess would be open the form in design view and put it in the On click event.
 
Ok that's cool and clever thanks. I responded, but my response didn't show up. So I'm responding again.

where whould the =mytoggle go?

My guess would be that I should open my form in design view go to the checkboxes and put it in the onclick? I didn't know you could do this, if that's it.
 
Ok, nevermind I know where the mytoggle button goes. But know I've got a wierd one.
I'm typing in screen.activecontrol.name when I get to the .name part I'm not given .name as a possible option. I'm also going to want activecontrol.value and I'm not given that option either. Is there a special library I need to have loaded?
 
where whould the =mytoggle go?
In the event property of each checkbox.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top