Setting a variable value based on button text

latex88

Registered User.
Local time
Today, 15:28
Joined
Jul 10, 2003
Messages
198
I am able to set a variable to equal to the caption of a command button. However, if I want to include a foreign translation of the caption on the second line, how do I assign the variable just to the first line only of the command button and ignore the second line?
 
Is the caption actually stored as multiple lines of text in the Caption property of the button in design view? (the only way I could find to do this was via copy and paste)

Or is it just one line and you have the buttons sized so that word wrap moves the translation portion to the "second" line?

If the former, then you can look for the occurrence of Carriage Return and/or Line Feed characters and return only the value to the left of that. For example, let's say you have the following caption on multiple lines in the caption of the command button;

Hello
Hola


in VBA, the following -

MsgBox Me.Command1.Caption

- would return;

Hello
Hola


and any of the following -

MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, Chr(10)) - 1)
MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, Chr(13)) - 1)
MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, vbCrLf) - 1)


- would return;

Hello

If the latter (the caption text is all on one line) then you would need another method to separate the foreign translation portion. You could look for the occurrence of a space but that would only work if each portion was only a single word. Another option would be to put the foreign translation portion in parentheses -

Hello (Hola)

- and the look for the occurrence of the beginning paren. Example;

MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, "(") - 1)

- would return;

Hello
 
Is the caption actually stored as multiple lines of text in the Caption property of the button in design view? (the only way I could find to do this was via copy and paste)

Or is it just one line and you have the buttons sized so that word wrap moves the translation portion to the "second" line?

If the former, then you can look for the occurrence of Carriage Return and/or Line Feed characters and return only the value to the left of that. For example, let's say you have the following caption on multiple lines in the caption of the command button;

Hello
Hola


in VBA, the following -

MsgBox Me.Command1.Caption

- would return;

Hello
Hola


and any of the following -

MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, Chr(10)) - 1)
MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, Chr(13)) - 1)
MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, vbCrLf) - 1)


- would return;

Hello

If the latter (the caption text is all on one line) then you would need another method to separate the foreign translation portion. You could look for the occurrence of a space but that would only work if each portion was only a single word. Another option would be to put the foreign translation portion in parentheses -

Hello (Hola)

- and the look for the occurrence of the beginning paren. Example;

MsgBox Left(Me.Command1.Caption, InStr(1, Me.Command1.Caption, "(") - 1)

- would return;

Hello


Hi Sean,

I was able to create text is two lines by pressing Shift + Enter.
 
Ahh. Never had a reason to have multiple caption lines on a command button before so I wasn't aware of that. Anyway, looking for the occurrence of a Cr and/or Lf character as in the former of my two previous examples should work. Post back if you have more questions.
 
If you have several buttons where you want to show different languages for one reason or another, you could build a table of captions with a captionid, name, lang.

Then load the caption names based on lang.
Just a thought.
 
If you have several buttons where you want to show different languages for one reason or another, you could build a table of captions with a captionid, name, lang.

Then load the caption names based on lang.
Just a thought.

jdraw,

That would actually be ideal if I can build a form to self "autopopulate" the buttons with captions as you described. Do you happen to know how?
 

Users who are viewing this thread

Back
Top Bottom