Kelly said:
Dirk,
I'm new to Access and tried the code and had the same issue JohnC had with
spaces. Could it be the """" in the replace string? Your help would be
greatly appreciated.
I've looked at the code -- from way back in 2005! -- and made some further
tests, and it's definitely got a problem with the entry of spaces at the end
of the text. The reason, it turns out, is that trailing spaces are
truncated when a string is assigned to the .Text property. I was unaware of
this behavior when I wrote the code, but it makes some sense when you
consider that a text box automatically truncates trailing spaces when you
enter them manually.
I've written a different approach that may work better, but again I haven't
tested it exhaustively. The code below uses more than one event, and a
couple of module-level variables, so you will have to merge it with your
existing code for the form (but remove the ToEquipment_KeyPress procedure
that you currently have).
'----- start of code for form module -----
Option Compare Database
Option Explicit
Dim mRst As DAO.Recordset
Dim mintLastLen As Integer
Private Sub Form_Load()
Set mRst = Me.RecordsetClone
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set mRst = Nothing
End Sub
Private Sub ToEquipment_Change()
On Error GoTo Err_ToEquipment_Change
Dim intLen As Integer
Static fWorking As Boolean
If mRst.RecordCount = 0 Then Exit Sub
If Not fWorking Then
With Me.ToEquipment
intLen = Len(.Text)
If intLen > mintLastLen Then
mRst.FindFirst _
"ToEquipment Like " & _
Chr(34) & .Text & "*" & Chr(34)
If Not mRst.NoMatch Then
fWorking = True
.Text = mRst!ToEquipment
.SelStart = intLen
.SelLength = 255
fWorking = False
End If
End If
End With
End If
Exit_ToEquipment_Change:
mintLastLen = Len(Me.ToEquipment.Text)
Exit Sub
Err_ToEquipment_Change:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_ToEquipment_Change
End Sub
Private Sub ToEquipment_GotFocus()
mintLastLen = 0
End Sub
'----- end of code for form module -----
Try that and see if it gives you better results.