Solved

Pass Label to module

Posted on 2004-10-10
8
230 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now