Click to See Complete Forum and Search --> : ComboBox text entry


Andrew
October 25th, 1999, 06:12 PM
I am using the keypress routine in a control array of Combo boxes to match the entered text with similar entries in the database. It is like an MSAccess field that has a foreign key relation behaves when the user presses the key "R" and the foreign field has a field with the value "Radio;" in the combo box the "R" appears as entered text and "adio" appears as highlighted text that can change if the next key entered is not an "a."



private Sub ComboRow_KeyPress(Index as Integer, KeyAscii as Integer)
Dim CaseCount, ValidateIndex as Integer
Dim Char, strQuery as string

Call RowOptions(strQuery)
Char = Chr(KeyAscii)

for CaseCount = 0 to Index
With ComboColumnaComparación
Select Case Index

Case 0
ValidateIndex = 0
Call ValidateEntry(Char, strQuery, _
ValidateIndex, KeyAscii)

Case 1
ValidateIndex = 1
Call ValidateEntry(Char, strQuery, _
ValidateIndex, KeyAscii)

Case 2
ValidateIndex = 2
Call ValidateEntry(Char, strQuery, _
ValidateIndex, KeyAscii)

Case 3
ValidateIndex = 3
Call ValidateEntry(Char, strQuery, _
ValidateIndex, KeyAscii)

End Select
End With
next
End Sub

public Sub ValidateEntry(byval Char, byval strQuery, _
byval ValidateIndex, cKeyAscii)
Dim Comparison as string
Dim Length as Integer

If len(ComboRow(ValidateIndex).Text) = 0 then
Comparison = Char
else
Comparison = ComboRow(ValidateIndex).Text & Char
End If

If rsMatchEntry.State = adStateOpen then
rsMatchEntry.Close
End If

rsMatchEntry.Open strQuery, cn, adOpenKeyset

If rsMatchEntry.BOF = false And rsMatchEntry.EOF = false then
rsMatchEntry.MoveFirst
Do Until rsMatchEntry.EOF
Length = len(ComboRow(ValidateIndex).Text) + 1

If rsMatchEntry.Fields(0).Value Like Comparison & "*" then
ComboRow(ValidateIndex).Text = _
Comparison & Right(rsMatchEntry.Fields(0).Value, len(rsMatchEntry.Fields(0).Value) - Length)
cKeyAscii = 0
ComboRow(ValidateIndex).SelStart = Length
ComboRow(ValidateIndex).SelLength = _
len(ComboRow(ValidateIndex).Text) - Length

rsMatchEntry.Close
Exit Sub
End If

rsMatchEntry.MoveNext

Loop

End If

rsMatchEntry.Close

End Sub





The problem is this: the procedure only works with every other keystroke. The user hits "R" and the word "Radio" appears with the "adio" highlighted, like it is supposed to do. If the next keystroke is an "a" then rather than showing "Radio" with the "dio" highlighted, the combo box shows only "Ra." But if the third keystroke is a "d" then the box shows "Radio" again with the "io" highlighted.

Anyone know what is causing this?

AndyK
October 25th, 1999, 11:25 PM
This code:

SendKeys "{home}+{end}"



will highlight all the text, so if you will plug it after the keystroke then whole text should become highlighted!

Ravi Kiran
October 26th, 1999, 05:19 AM
Hi,

Put a Debug.Print statement in your ValidateEntry fn, after the lines:

If len(ComboRow(ValidateIndex).Text) = 0 then
Comparison = Char
else
Comparison = ComboRow(ValidateIndex).Text & Char
End If
Debug.print Comparison




It turns out that, Comboxx.Text , on the Key Press event will be having a value that is the Text that was showing before the key press. I tested an example, and it shows
that every other key-press only it takes the
correct comparisoin string. ( and then it will match properly, and hilite too)

So, what you need to is remember the previous search string or take only the string to the left of position at which key was pressed, or something like that. May be more logic is required.

ANother thing, it may be easier to search in the Combo Box's List items itself. ( you need to populate it before ) instead of record search every time. Or have a local array of strings !

RK