Len Function

  • Thread starter Thread starter PC Datasheet
  • Start date Start date
P

PC Datasheet

How di I get the Len function to return 3 when I type AB(space) in a
textbox?

I have two textboxes and a button on a form. I can enter a string in
MyTextBox and have code in the click event of the button to use the Len
function to output the number of characters I entered in MyTextBox. The
button code is:
Me!MyLenOfString = Len(Me!MyTextBox)

If I type AB(space) in the textbox and click the button, 2 appears in
MyLengthOfString instead of 3.

Steve
 
Access will not store trailing spaces. Is there a good reason to expect a
space to be retained at the end of a field value? There may be other
possible solutions.

When you move focus to the button, the value in the text box becomes right
trimmed.
 
PC Datasheet said:
How di I get the Len function to return 3 when I type AB(space) in a
textbox?

I have two textboxes and a button on a form. I can enter a string in
MyTextBox and have code in the click event of the button to use the Len
function to output the number of characters I entered in MyTextBox. The
button code is:
Me!MyLenOfString = Len(Me!MyTextBox)

If I type AB(space) in the textbox and click the button, 2 appears in
MyLengthOfString instead of 3.

Steve

This has been answered in another ng where you asked the same question.
 
Yes, I know! I meant to cross post to this newsgroup but was too quick on
the trigger finger when I posted to CDMA. With this post I am just looking
for additional expertise in this newsgroup.

Steve
 
Thanks for responding, Duane!

My actual situation is that I have a continuous form with an unbound textbox
in the form header. The recordsource of the form is a query that uses the
value in the unbound textbox in the criteria of one of the fields. I use the
Change event in the textbox to run code to requery the form. I need to be
able to type AB(space)C in the textbox but the space gets trimmed. Any
suggestions for some other possible solution?

Is there any way to check if a space has been entered?

Here is my code in the Change event:
' SaerchFor is the unbound textbox
Me.Requery
Me!SearchFor.SetFocus
If IsNull(Me!SearchFor) = False Then
Me!SearchFor.SelStart = Len(Me!SearchFor) + 1
End If

The problem is in the Me!SearchFor.SelStart ..... statement.
Len(Me!SearchFor) does not include a space.

Steve
 
Because you're using the Change event, I assume that the textbox has the
focus. Thus, try reading the .Text property instead of the .Value property.
 
Ken,

Thanks for responding!

Again, here is my code:
' SaerchFor is the unbound textbox
Me.Requery
Me!SearchFor.SetFocus
If IsNull(Me!SearchFor) = False Then
Me!SearchFor.SelStart = Len(Me!SearchFor) + 1
End If

Where are you suggesting to use the text property?

Steve
 
' SaerchFor is the unbound textbox
Me.Requery
Me!SearchFor.SetFocus
If IsNull(Me!SearchFor.Text) = False Then
Me!SearchFor.SelStart = Len(Me!SearchFor.Text) + 1
End If
 
Put the following under Option Compare Database:
Dim LenOfSearchFor as Integer

Put the following in the form's module:
Private Sub SearchFor_BeforeUpdate(Cancel As Integer)
LenOfSearchFor = Len(Me!SearchFor.Text)
End Sub

Private Sub SearchFor_Change()
Me.Requery
Me!SearchFor.SetFocus
If IsNull(Me!SearchFor.Text) = False Then
If LenOfSearchFor > Len(Me!SearchFor) Then
Me!SearchFor = Me!SearchFor & " "
End If
Me!SearchFor.SelStart = LenOfSearchFor + 1
End If
End Sub

Jim Fortune in CDMA noted that Access won't automatically remove trailing
spaces that have been added through code.

Steve
 
Steve, I think you're doing it the hard way.

Dim a module level variable
Dim tbxContent

In the change event for the code set the variable equal to the .Text
property
tbxContent = Me.tbxSearchFor.Text

Then, any time you need the content of the text box in other code within the
module (without losing the trailing space if there is one), use tbxContent
rather than Me.tbxSearchFor

Simple as that.
 
Yes, made that work too.

Thanks, Randy!

Steve


Randy Harris said:
Steve, I think you're doing it the hard way.

Dim a module level variable
Dim tbxContent

In the change event for the code set the variable equal to the .Text
property
tbxContent = Me.tbxSearchFor.Text

Then, any time you need the content of the text box in other code within
the
module (without losing the trailing space if there is one), use tbxContent
rather than Me.tbxSearchFor

Simple as that.
 
Back
Top