Solved

Dynamically set properties of a label at run time

Posted on 2007-04-07
5
155 Views
Last Modified: 2013-11-25
Hi,

I have a vb6 projects with numerous forms and numerous labels on the form.  At run time I want to dynamically be able to set the properties of each lablel i.e.  .caption = "XY"

Each label will have a default value for each property that is hardcoded into app, however on a dymanic basis I want to be able to provide a new property value.  Because there are so many labels and forms I don't want to have to programatically put a line in the code for each property.  I simply want a function that I can pass the form to, then the function will change the label properties, by looking up the labels new properties in a SQL table.  I thought you could do it with something similar to the following:

Dim lblDynamicLabel as Object
lblDynamicLabel  = "lblObjectName"
lblDynamicLabel .caption = "GGG"

However this throws an error.

Any ideas?

thanks
0
Comment
Question by:kinton
  • 2
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 18870021
have a look at the CallByName method
0
 
LVL 2

Author Comment

by:kinton
ID: 18870260
This looks like it calls a method dynamically, how would you use it to update a property where the property and the property value is dynamic?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 18870302
this way:

Private Sub Command1_Click()
Dim strLabelName As String

    strLabelName = "Label1"
   
    CallByName CallByName(Me, strLabelName, VbGet), "Caption", VbLet, "GGG"
End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18872632
*** Not for Points ***

Slightly different syntax:

' ------------------
'  Form1
' ------------------
Private Sub Command1_Click()
    SetProp Me, "Label1", "Caption", "GGG"
End Sub

' ------------------
'  Module1
' ------------------
Public Sub SetProp(ByVal frm As Form, ByVal ctlName As String, ByVal ctlProp As String, ByVal ctlValue As Variant)
    On Error Resume Next
    CallByName frm.Controls(ctlName), ctlProp, VbLet, ctlValue
End Sub
0
 
LVL 2

Author Comment

by:kinton
ID: 18875639
Brilliant.  Thank you very mucg for the quick reply,
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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

813 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

16 Experts available now in Live!

Get 1:1 Help Now