Solved

Pass Label to module

Posted on 2004-10-10
8
231 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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 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…

912 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