TextBox text not aligning left

L

LEU

I have a textBox in my UserForm and I have the TextAlign set to
'1-fmTextAlignLeft'. But when the userform is opened and I go to put text
into it the curser starts 5 spaces from the left. Why would this be happening?
 
J

Jay Freedman

LEU said:
I have a textBox in my UserForm and I have the TextAlign set to
'1-fmTextAlignLeft'. But when the userform is opened and I go to put
text into it the curser starts 5 spaces from the left. Why would this
be happening?

I'm not sure how you're measuring the five spaces, but that seems like a lot
more than usual.

Anyway, try setting the text box's SelectionMargin property to False.

The default is True, which provides a little extra space on the left where
the user can place the cursor (which becomes a northeast-pointing arrow) and
click to select the whole content of the box. When it's False, there is no
extra space so the user has to position the cursor very carefully to get the
same function.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

LEU

Jay,

That did not help. When the userform is opened I have to hit the BackSpace
key 5 times to get the curser to the left margin of the textbox. If I tab
through the userform and come back around to the TextBox, the first 5 spaces
comeup blue. Then if I hit the delete key the curser movers to the left
margin. It's like as if when the userform is opened it's loading 5 spaces
into the textbox.
 
J

Jay Freedman

I can't reproduce that behavior. My text boxes always start completely
empty. But...

Do you have any code in the userform's UserForm_Initialize() or
UserForm_Activate() procedure that might insert those spaces? Or in the
macro that calls the userform's .Show method?

The fact that it's "five spaces" makes me suspect that you're loading the
..Result of text form fields in the document into the userform's text boxes.
A text form field does contain five spaces (actually, nonbreaking space
characters) when it's "empty".

If that's what's happening, use the Trim() function on the form field's
..Result before putting the remaining text (if any) into the userform.
 
L

LEU

That was it. Once I put "LTrim" in, the spaces were gone.

In my form I'm getting an error message that says: : “Run-time error: ‘13’
Type Mismatch†at Nbr1 = TextBox1. TextBox1 & 2 could have numbers in them
like 10.75. Below is my macro. What is causing it?

Private Sub TextBox1_Change()
Dim Nbr1 As Double, Nbr2 As Double
Nbr1 = TextBox1
Nbr2 = TextBox2
GTNbr1 = Nbr1 + Nbr2
TextBox29 = GTNbr1
End Sub
 
J

Jay Freedman

As the error message says, it's a type mismatch. The variables Nbr1 and Nbr2
are declared as Double. The TextBox1 and TextBox2 are objects that represent
the textbox controls in the userform. You can sort of get away with using
just TextBox1 because the .Text property is the "default property" for that
object, but it's best to use the complete expression TextBox1.Text, which
returns a String value.

Then you must do the explicit type conversions. There are two ways to do
that:

- The CDbl function takes a string argument and converts it to a Double
value. But if the string doesn't satisfy the rules for a number that can be
converted, you'll get an error. That means you have to use an On Error
statement that goes to a piece of error handling code (look in the help
about On Error) or else if someone enters a meaningless value the userform
will show an error message and stop like it does now.

On Error GoTo ErrHdl
Nbr1 = CDbl(TextBox1.Text)
Nbr2 = CDbl(TextBox2.Text)

....

Exit Sub

ErrHdl:
' put error handling code here
End Sub

- The Val function takes a string argument and converts it to a number
(maybe an Integer or Long or Single or Double, depending on what's in the
string), and then the assignment to the Double on the left of the equal sign
will do a silent conversion to Double. If the string doesn't look like a
number, the Val function returns a zero, so you'd have to look for that and
maybe treat it as an error (depends on what the text box represents).

Nbr1 = Val(TextBox1.Text)
' If Nbr1 = 0 Then .... End If

Nbr2 = Val(TextBox2.Text)
' If Nbr2 = 0 Then .... End If

Finally, after you add the numbers together, you should do the reverse
conversion to put the value into the third text box:

TextBox29.Text = CStr(GTNbr1)

Also, you neglected to declare GTNbr1 As Double; that means it defaults to
being a Variant. It's a good idea to force all variables to be declared --
see http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top