Solved

Pass Label to module

Posted on 2004-10-10
8
234 Views
Last Modified: 2010-05-02
Hi - I'm trying to pass a label name from a form to a module.

I can pass the form name without any problems but label part is working properly.

Any suggestions?  Also, I may pass the label name from another module.

Thanks,

'calling from form
Private Sub frmMain_Load()
   Call AdjustLabelSize(Me, Label1)
End Sub

'call from another module
Function getInformation()
    Call AdjustLabelSize(frmMain, Label1)
End Function

Function AdjustLabelSize(iForm As Form, iLabel As Label)
      iForm.ilabel.Width = 1300
End Function
0
Comment
Question by:eciabattari
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:AjithJose
ID: 12273414
Call the function as

Call AdjustLabelSize(frmMain,frmMain.Label1)


0
 
LVL 32

Expert Comment

by:Erick37
ID: 12273458
You do not need to pass in the form name

'Form Code
Call AdjustLabelSize(Me.Label1)

'Module code
Function AdjustLabelSize(iLabel As Label)
      ilabel.Width = 1300
End Function

Hope it helps!
0
 

Author Comment

by:eciabattari
ID: 12273537
It works.  However, I have 25 different labels that need to be adjusted and move.  

Any suggestion on how to pass "frmMain.Label1", "FrmMain.Label2" ... "FrmMain.Label25" to Function AdjustLabelSize?

Thanks
0
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.

 
LVL 32

Expert Comment

by:Erick37
ID: 12273650
If all the labels need to be 1300 twips in width, then you can set them in design time.  Click on a label, then in the Properties pane, select Width, and set it to 1300.  Do this for each label.

If you need to set the width according to some variable during runtime, then maybe you can make a control array and then loop throught the array and set each control.

e.g.
'Assuming Label1 is a control array
Dim i as Long
For i = Label1.Lbound to Label1.Ubound
    Call AdjustLabelSize( Label1( i ) )
Next
0
 
LVL 2

Expert Comment

by:Drifter88zxtW
ID: 12273753
Use the tag property on the control properties page.

Set the tag property to "adjust" (just type it in) during design time.

Then in form just

--Form1--

Form1_Load

Call AdjustLabels

End SUb

--Module1--

Public SubAdjustLabels()

Dim aLabel as Label

For Each aLabel in Form1.Controls
       If aLabel.Tag = "Adjust" Then
               aLabel.Width = 1300
       End IF
Next

End Sub
0
 
LVL 2

Assisted Solution

by:Drifter88zxtW
Drifter88zxtW earned 250 total points
ID: 12273761
If you want to apply that to different forms and use different sizes then... use code like this...


--Form1---

Form1_Load

    Call AdjustLabels(Me, 1300)

End Sub

--Form2---

Form2_Load

    Call AdjustLabels(Me, 1600)

End Sub

--Module1--

Public SubAdjustLabels(frm as Form, AdjustToSize as Long)

Dim aLabel as Label

For Each aLabel in frm.Controls
       If aLabel.Tag = "Adjust" Then
               aLabel.Width = AdjustToSize
       End IF
Next

End Sub

This Module code is more reusable
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 12273831
Some good suggestions already.  This may not be the best approach but it demonstrates a technique not presented yet:

' -----------------------------------------------------------------------------------
' frmMain
' -----------------------------------------------------------------------------------
Private Sub Form_Load()
    Dim i As Integer
    Dim lblName As String
   
    For i = 1 To 25
        lblName = "Label" & i
        AdjustLabelSize Me, lblName
    Next
End Sub

' -----------------------------------------------------------------------------------
' Module1
' -----------------------------------------------------------------------------------
Public Sub AdjustLabelSize(ByRef iform As Form, ByVal iLabel As String)
    Dim lbl As Label
    Set lbl = getControlByName(iform, iLabel)
    If Not (lbl Is Nothing) Then
        lbl.Width = 1300
    End If
End Sub

Private Function getControlByName(ByRef iform As Form, ByVal controlName As String) As Control
    On Error GoTo noSuchControl
    Set getControlByName = iform.Controls(controlName)
    Exit Function
   
noSuchControl:
    MsgBox "Form = " & iform.Name & vbCrLf & "Control = " & controlName, vbCritical, "Control Not Found"
    Set getControlByName = Nothing
End Function
0
 

Author Comment

by:eciabattari
ID: 12275801
Thanks for the responses... I ended up using a couple of people's suggestions to work the problem out and will split points accordly.

Thanks again for all the help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 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