Solved

Pass Label to module

Posted on 2004-10-10
8
232 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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…

770 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