Writing more efficient code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two questions about writing more efficient code:

1. Whenever I have combo boxes on a form, I almost always add
Me.<FieldName>.Dropdown to the On Enter event for each. This can get tedious
if I have many combo boxes. Is there a more efficient way to handle this?
(Like, maybe tag each combo box and write code that says to drop down every
tagged control upon enter. Or, use a function somehow that automatically
drops down any combox box when it's entered?

2. If a user selects “Yes†from a combo box, I would like to enable a
collection of controls that are related to that Yes/No question. (The
controls could be checkboxes, text boxes, or both). If the user selects “No,â€
I would like to disable these controls. I always end up putting something
like this in the After Update event for the combo box:

If Me.Bruises.Value = "Yes" Then
Me.Hands.Enabled = True
Me.Hands.Locked = False
Me.Face.Enabled = True
Me.Face.Locked = False
Me.Neck.Enabled = True
Me.Neck.Locked = False
Else
Me.Hands.Enabled = False
Me.Hands.Locked = False
Me.Face.Enabled = False
Me.Face.Locked = False
Me.Neck.Enabled = False
Me.Neck.Locked = False
End If

This can get tedious if there are many controls related to the combo box, or
if I need to repeat this process for subsequent Yes/No questions. Is there a
more efficient way to handle this? Like, somehow assign a series of controls
as a collection, and then refer to that collection?

Thanks!

Kurt
 
Kurt said:
I have two questions about writing more efficient code:

1. Whenever I have combo boxes on a form, I almost always add
Me.<FieldName>.Dropdown to the On Enter event for each. This can get
tedious if I have many combo boxes. Is there a more efficient way to
handle this? (Like, maybe tag each combo box and write code that says
to drop down every tagged control upon enter. Or, use a function
somehow that automatically drops down any combox box when it's
entered?

You could create the following function in a standard module:

'----- start of code -----
Function DropdownCombo()

On Error Resume Next
Screen.ActiveControl.Dropdown

End Function
'----- start of code -----

Then you could set the OnEnter *property* of each combo to this function
expression:

=DropdownCombo()

Note that I'm not talking about calling the function from and event
procedure. Instead, you put the function expression directly into the
"On Enter" line in the property sheet. That will save you having to
create a separate event procedure for each combo box.
2. If a user selects "Yes" from a combo box, I would like to enable a
collection of controls that are related to that Yes/No question. (The
controls could be checkboxes, text boxes, or both). If the user
selects "No," I would like to disable these controls. I always end up
putting something like this in the After Update event for the combo
box:

If Me.Bruises.Value = "Yes" Then
Me.Hands.Enabled = True
Me.Hands.Locked = False
Me.Face.Enabled = True
Me.Face.Locked = False
Me.Neck.Enabled = True
Me.Neck.Locked = False
Else
Me.Hands.Enabled = False
Me.Hands.Locked = False
Me.Face.Enabled = False
Me.Face.Locked = False
Me.Neck.Enabled = False
Me.Neck.Locked = False
End If

This can get tedious if there are many controls related to the combo
box, or if I need to repeat this process for subsequent Yes/No
questions. Is there a more efficient way to handle this? Like,
somehow assign a series of controls as a collection, and then refer
to that collection?

The easiest way I've found to assign controls to groups for special
handling is to use the controls' Tag property. I give all controls in
the group the same Tag value, or include that value in a delimited list
of values assigned to the control's Tag (allowing the same control to
belong to multiple groups). Then I have code that loops through all
controls on the form and does something to those controls whose Tag
contains the group name that I'm interested in.

You could easily write a function to which you pass the Tag value to
look for and the settings you want, and it loops through the controls
and applies those settings to the controls identified by the tag.

You might even be able to call this function from a control's event
property.
 
Me.<FieldName>.Dropdown to the On Enter event for each. This can get
tedious
if I have many combo boxes. Is there a more efficient way to handle this?

What you could do is write a general piece of code, and place it in a
standard module.

Lets call it

Public Function MyDrop()

Screen.ActiveControl.Dropdown

End Function


Now, lets open up a form in DESIGN mode with 5 combo boxes (but NONE YET
HAVE any code to "drop" the combo).

While in design mode, highlight ALL 5 of the combo box es...
(hold down the shift key..and click on each combo box ...one by one...).

At this point we have 5 combo boxes selected.

Bring up the properties sheet..and in the "on Enter" event, simply type in
the name of the public function as follows

=MyDrop()

Note that we did not have to open the code window..and we just set the
on-enter event for all 5 combo boxes to our global code...

So, the above is not perfect...but you just eliminate having to open up
the code editor for the 5 combo boxes..and can do this one shot for any
form(s) you have....
2. If a user selects "Yes" from a combo box, I would like to enable a
collection of controls that are related to that Yes/No question. (The
controls could be checkboxes, text boxes, or both). If the user selects
"No,"
I would like to disable these controls.

Simply build a routine to do this. This code is placed in the FORMS module
code.

Sub ShowParts(b As Boolean)

Dim v As Variant
Dim i As Integer

v = Split("Hands,Face,Neck", ",")

For i = 0 To UBound(v)
Me(v(i)).Enabled = b
Me(v(i)).Locked = False
Next i


End Sub


Now, in your after update event, go

If Me.Bruises.Value = "Yes" Then
call ShowParts(True)
else
call ShowParts(false)
end if


You could even build up a few lists of strings..and pass that to the
routine..

(in your forms def, you could go

myBodyPartsList = "Neck, Hands, Nose"
myFeetPartsList = "Toe, BloodCount, Fracture"

Then you can call the routine like

Call myshow(myBoardPartsList,True).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top