SelLength and spaces (1 Viewer)

sconly

Registered User.
Local time
Today, 19:39
Joined
Oct 8, 2002
Messages
56
I have a textbox that runs a bit of (sql) code whenever the user inserts a character in it, and then resets the focus back to it using sellength as part of the criteria.

Code:
txtSearch.SetFocus
txtSearch.SelStart = txtSearch.SelLength + 1

The problem is that when the cursor is placed back in the textbox (and if there is a space afetr the last character) it is position after the last character and ignores any spaces.

I guess what I'm trying to ask is is there any way to make SelLength include spaces?

Thanks
 

pono1

Registered User.
Local time
Today, 11:39
Joined
Jun 23, 2002
Messages
1,186
Do you know if in fact the trailing space exists? I ask because if you, say, requery the form in some fashion (it's not clear if you are doing this yet it sounds like you might)...Access chops trailing spaces from data in the form's textboxes.

Another thing to try...

Code:
' Position the cursor in the textbox.
 Me.TxtBoxName.SelStart = Len(Me.TxtBoxName)

Regards,
Tim
 

boblarson

Smeghead
Local time
Today, 11:39
Joined
Jan 12, 2001
Messages
32,059
Yes, Access chops trailing spaces. I don't know of any way to keep that default behavior from happening. And this code you have:

txtSearch.SelStart = txtSearch.SelLength + 1

isn't going to do anything because adding one to the SelLength isn't going to add a space. You could try using this:

txtSearch.Text = txtSearch.Text & Space(1)
txtSearch.SelStart = txtSearch.Text.SelLength

but it would need the focus.
 

pono1

Registered User.
Local time
Today, 11:39
Joined
Jun 23, 2002
Messages
1,186
You can work around the trimmed textbox phenomenon using code. For a working example, create a form and drop two new textboxes on it. Name them txtX and txtY. Set the form's Key Preview property to Yes. Then paste the following in the form's code module.

Code:
Option Compare Database
Option Explicit

Private SpaceBarPress As Boolean

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  
  ' We only care about the txtX textbox.
  If Screen.ActiveControl.Name = "txtX" Then
    ' And we only need to track txtX's space bar presses.
    If KeyCode = 32 Then SpaceBarPress = True
  End If

End Sub
' Preserves spacebar presses.
Private Sub txtX_Change()
    
  ' Requery a form just for fun.
  Me.Requery
  
  ' Nothing to do if nothing is entered in the textbox.
  If IsNull(Me.txtX) Then
    Exit Sub
  Else
    ' Because Access removes trailing spaces when requerying,
    ' add back a space bar press when necessary.
    If SpaceBarPress = True Then
      Me.txtX = Me.txtX & " "
      ' Reset the space bar press indicator.
      SpaceBarPress = False
    End If
    ' Re-position the cursor in the textbox.
    Me.txtX.SelStart = Len(Me.txtX)
  End If

End Sub

' Won't preserve spacebar presses.
Private Sub txtY_Change()

  ' Requery a form just for fun.
  Me.Requery
  
  ' Re-position the cursor in the textbox.
  Me.txtY.SelStart = Len(Me.txtY)

End Sub

The example "fixes" txtX when a space is entered but doesn't fix txtY. Tested in A2K only.

Regards,
Tim
 

Hubert

New member
Local time
Today, 13:39
Joined
Jun 21, 2023
Messages
3
Good morning, friends of the forum, I solved it by adding a KeyDown:
Private Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeySpace
txtSearch = txtSearch + " "
KeyCode = 0
End Select
txtSearch.SetFocus
Me.txtSearch.SelStart = Len(txtSearch.Text)
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:39
Joined
Feb 28, 2001
Messages
27,523
Hello, Hubert, and thank you for your contribution. However, you should look at the posting dates. You have answered a 12-year-old question that had gone dormant for quite a while.
 

Users who are viewing this thread

Top Bottom