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

Coding a Microsoft Access Form

Is there a better way to handle this...All my continuous forms have the following code in the load event so that they all look have the same look.  In the header section of each form, all my labels are named "Label" with sequential numbers.  The following code works well, with the exception that the Do Until has to be correct for each form so that it is 1 number more than the number of labels. Some forms, such as the one I copied this code from, has 10 labels and some have 3 or 4.  Can this code be modified into a function so that it looks for any control named "LabelX" and assigns it the "lngButtonFont" color? This would help me not have to change the Do Until number in each form.
Thanks experts!
Private Sub Form_Load()
  Dim I As Integer
  Dim lngButtonFont As Long

  lngButtonFont = fGetButtonFont
  
  I = 1
  Do Until I = 11
    Me("Label" & I).ForeColor = lngButtonFont
    I = I + 1
  Loop
   
End Sub

Open in new window

0
Harry Batt
Asked:
Harry Batt
  • 3
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>Can this code be modified into a function so that it looks for any control named "LabelX" and assigns it the "lngButtonFont" color?

'This sets the ForeColor for one label named LabelX
Me.LabelX.ForeColor = lngButtonFont

If you still want the loop, and don't want to hard-code the number of labels on each form, and we can assume that all lables have 'Label' in the first five characters of the name, then you could do ...

Dim ctl as Control

For each ctl in Me.Controls
   If left(ctl.Name, 5) = "Label" then
      ctl.ForeColor = lngButtonFont
   End If
Next
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
(or come to think of it)

If TypeOf(ctl) Is Label
  'code goes here
End If
0
 
Harry BattDirector of DevelopmentAuthor Commented:
Jim,

Can you tell me what is wrong with this code? It doesn't work
Function fLoadForm()
    Dim ctl As control
    Dim lngButtonFont As Long

   
    lngButtonFont = fGetButtonFont

    If TypeOf ctl Is label Then
        ctl.ForeColor = lngButtonFont
    End If

End Function

Open in new window

0
 
IrogSintaCommented:
I believe Jim meant for you to just replace the IF portion.  You still need the For..Next loop.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
correct.  

For Each ctl in Me.Controls
   If TypeOf(ctl) Is Label
     'code goes here
   End If
Next
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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