Solved

Dynamically set properties of a label at run time

Posted on 2007-04-07
5
152 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 69

Expert Comment

by:Éric Moreau
Comment Utility
have a look at the CallByName method
0
 
LVL 2

Author Comment

by:kinton
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
*** 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
Comment Utility
Brilliant.  Thank you very mucg for the quick reply,
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

17 Experts available now in Live!

Get 1:1 Help Now