Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Changing Caption of form by name at runtime

  Asked By: Rose    Date: Nov 26    Category: MS Office    Views: 829
  

I am trying to set up a sub routine which basically does the following

But I think I struggling with the types to get it to works





Outline



Called at the start of the display of the form (on active)

SetFormCaption (frmstart)



Which then basically is to go to the sub and take the frmstart name and then
identify the form and give me access to the caption property grab its
content and then basically add a const string / variable string and create a
new caption and set it on the form



Thus giving a way of always updating the captions of the form with the
software version





The code as so far



Module



Const lblCaptionTitle = "Software Version 1"





Sub SetFormCaption (frmstart As ??????)



Dim varFormName as String



varFormName = Userform(frmstart).Caption

varFormName = lblCaptionTitle & ": " & varFormName

Userform (frmstart).Caption = varFormName



End Sub



That's the outline but I cant get it to work for the life of me.



Can someone point out where I going wrong.

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Rachael Ferguson     Answered On: Nov 26


I had a quick glance at your code.
First, I would advise you to call your sub from the initialize event of the
forn and not the activate event.
Second, and only to help you in the future, I would advise you to name your
string
strFormName as string  and not varFormName as String
This should make reading and understanding your code  easier later on.

And now to the point:

What is the name of your form? what is "frmStart "?
is frmStart the name of your form. If so, you should access  its caption  by the
following:
frmStart.caption and not userform(frmStart).caption

I have written a small code to try to make this point clear. I hope this will
help you:
Try the following code. Please note the name of myform is "UserForm1" - this
is the default name of the first form. Change this as per your need.

Private Sub UserForm_Initialize()
Call SetFormCaption
End Sub

Sub SetFormCaption()
Dim strFormName As String

strFormName = UserForm1.Caption
strFormName = lblCaptionTitle & ": " & strFormName
UserForm1.Caption = strFormName

End Sub

 
Answer #2    Answered By: Muhammad Evans     Answered On: Nov 26

It might also be nice to do it as shown below so that the exact same
generic code  could be added to each form.

module code:

Public Const lblCaptionTitle = "Software Version 1"

each userform's code:

Private Sub UserForm_Initialize()
Me.Caption = lblCaptionTitle & ": " & Me.Caption
End Sub

 
Answer #3    Answered By: Kian Evans     Answered On: Nov 26

I see both the point of view and the coding method tips thanks

Can I ask for clarification I understand the "me" keywords and understand
that the userform is using an index value of the form  which are loaded.


Even though you might tell me this is not possible to pass a string  to refer
to a form .. and then alter attributes..


But I look at this article
http://support.microsoft.com/?kbid=213574

I thought this would be useful when you don't know the userform index or the
reference part.

Is it possible if not that's is fair enough.

 
Answer #4    Answered By: Tomas Thompson     Answered On: Nov 26

I would like very much to help you but I do not completely understand what you
want to do.
Could you please explain in simple terms.
How many userforms do you have in your project?
Do you know the name of the form  you are trying to open? These are the most
important points to clarify. Accessing the caption  property of a form is very
simple if you identify the form clearly.

Of course you can identify a form by a variable  name. -as you read in the
article you quoted.
Suppose you have two forms. one of them is called "FrmStart" and the other
"FrmContinue".
Now, according to certain actions (x <> 1 in this example) you decide to open
"FrmContinue"
if x = 1 then
strFormName = "FrmStart"
else
strFormName = "FrmContinue"
now you can call your form with the following:
VBA.UserForms.Add(strFormName).Show

If you want to change the caption of the displayed form, you can do one of the
following:
if the new caption who want is the name of the form & some other string
you do the following in the initialize event of the form (in the module form):

Private Sub UserForm_Initialize()
Me.Caption = Me.Name & " hello"
End Sub
Now, if the caption you want is a string  variable. I do not know how to pass
this string as an argument to the initialize event of the form but I believe
that you can declare this variable in one of your module (for example in
"thisworkbook" module. Declare it as public (so that you may see it from other
modules in the project and in the initialize event of the form use the
following. (in this case use Public strA_String as string in the ThisWorkbook
module)

me.caption = strA_String & " Hello"

I hope it's clear.

And by the way, if somebody knows of a better solution in order to pass an
argument to a form I would be very grateful to learn about it.

 
Answer #5    Answered By: Madeeha Malik     Answered On: Nov 26

From the article you referenced, I think this is more what you are
looking for:

Sub SetFormCaption(frmstart)
Set MyForm = VBA.UserForms.Add(frmstart)
MyForm.Caption = lblCaptionTitle & ": " & MyForm.Name
MyForm.Show
End Sub

There is also an issue with the scope of the MyForm object, so if
you want to Show it outside of the SetFormCaption sub, MyForm must
declared at a higher scope by placing

Public MyForm As Variant

At the top of the module.

 
Didn't find what you were looking for? Find more on Changing Caption of form by name at runtime Or get search suggestion and latest updates.




Tagged: