Solved

Pass Label to module

Posted on 2004-10-10
8
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

635 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