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?
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?