Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting label caption on subform, via VBA

Status
Not open for further replies.

as0125

Technical User
Jun 3, 2004
70
0
0
US
Can anyone please help me with this vba code? I'm trying to set a label caption on a subform, depending on the "IndxType" value on the main form. This code is on the subform's OnLoad event:

Dim indextype As String
indextype = Forms!FrmSearchIndexMain.IndxType

If (indextype = "Mkt") Then
Me.Label34.Caption = "Week Change"
Else
Me.Label34.Caption = "Period Change"
End If

Regardless of what the "IndxType" value is, this label caption keeps showing "Week Change".

Thanks for any help!
 
Hi as0125

The subform's OnLoad does not fire when you browse through the records on your main form.

You need the code to be triggered in your main form instead.

I would suggest having something like the following in your main form's OnCurrent event ...

Code:
If Me.IndxType = "mkt" Then

Me.frmSubform.Controls("Label34").Caption = "Week Change"

Else

Me.frmSubform.Controls("Label34").Caption = "Period Change"

End If

Hope this helps.

Mac
 
Hi Mac,

I tried your suggestion, but that's not working for me either. Do you know of anything else I can try?

Thanks!
A.
 
Try:

If indextype = "Mkt" Then
Me!SubformName.Form!Label34.Caption = "Week Change"
Else
Me!SubformName.Form!Label34.Caption = "Period Change"
End If


-Gary
 
The subform OnLoad event will only fire one time. Try the main form's OnCurrent event.

-Gary
 

Thanks for the response Gary.

I just realized what part of my problem is....

Usually, if I'm working on a main form and type "Me." a list of possible objects displays for me to choose from. I just noticed that my subform is not part of that list, but it should be. Any suggestions for why the main form isn't recognizing the subform?

Thanks!
 
You should not see the name of the Subform itelf, but the name of the control on the main form. This may or may not be the same.

-Gary
 
Hi Gary,

I get this error message when using your code:

"...can't find the field 'subform name' referred to in your expression."

I can't figure out what's wrong!
 
Can you post the relevant section of your main form's OnCurrent event, as well as the name of your subform control? Not the name of the subform, but the control on the main form (Again, they may be different).

-Gary
 

I *finally* figured it out! Thanks so much for your help Gary. At first I didn't know what you were referring to when you asked for the name of the control for the subform on the main form.

Yes - you were right, the name of the subform control and the name of the subform itself were different. Once I figured that out and corrected the name, the code worked.

Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top