View Full Version : How to refer to a label's caption


flemmo
05-04-2006, 12:10 PM
Hi,

I'm brand new to VB, but here goes :)

Could someone tell me the syntax to reference and change a label's caption?
Basically I want a little text box that to easy test that variables are being passed around correctly.

Just to further explain the structure, I have

Form > Label > Caption.

Thanks!

Uncle Gizmo
05-04-2006, 01:25 PM
This code in VBA: Label13.Caption = "XXX"

will change the label named label13 to read "XXX"

flemmo
05-07-2006, 03:40 AM
Cheers!
This works fine if the command and label are on the same form, but how could I reference the label in another form?

is it something like:

Form with Command Button onClick
formname.Labelx.Caption = "hello"

I tried this and it said 'Object Required'.

Uncle Gizmo
05-07-2006, 03:56 AM
Off the top of my head I think it's something like:


Forms_formname.Labelx.Caption = "hello"

But I need to check ................

Uncle Gizmo
05-07-2006, 04:17 AM
You might find these posts helpful:

How to open a form at specific record? (http://www.access-programmers.co.uk/forums/showthread.php?t=80660)

Opening Form and Passing Variables (http://www.access-programmers.co.uk/forums/showthread.php?t=77525)

I thought I ought to explain the second example as at first glance it looks complicated however it is quite simple once you get the hang of it.

' This stores your form name as string
strDocName = "FormB"

'if the form is not already open you will have to open it
DoCmd.OpenForm strDocName

'this is a "with block" it just saves you typing the name of the form every time.
With Forms(strDocName)
.Caption = "Caption set from FormA" 'Form caption not a label!
.addrb1 = Me.Addr1
.addrb2 = Me.Addr2
.addrb3 = Me.Addr3
End With

"Addr1" data is extracted from this control on this form, and transferred to this control "addrb1" in the form you have opened, in this case "FormB"

flemmo
05-07-2006, 04:46 AM
Yep, works perfectly using your method
Thanks very much :)