Hi Guys,
I have a textbox(txtkcnamesearch2) that has an auto search sub attached to it, This auto search uses a datacontrol(Data2) to find the records i want to compare to bring up the closest match letter by letter as the user types in ther the textbox, the results are sent to a list box when you hit a command button(cmdFind2).
This all works fine, in fact it works realy well, Im a newbie to VB and was amazed I got it running
The thing thats puzzling me is if you type in a sequence of letters that dont match any of the names in the field being searched it crashes the program.
I have tried to add "On Error Resume Next" but it doesnt help???
Can anyone advice me on a work around for this issue?
Many thanks
Les
'start of autocomplete peds search
Private Sub txtkcnamesearch2_KeyUp(KeyCode As Integer, shift As Integer)
On Error Resume Next
Dim LengthText As Integer
Dim NewText As Integer
Dim Answer As String
Dim LeftText As String
'Get the length of the typed text.
LengthText = Len(Replace$(txtkcnamesearch2, "'", "''"
)
'Handle Backspace and Delete keys.
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or KeyCode = vbKeyReturn Then
Exit Sub
Else
On Error Resume Next
'Set variable to the typed text.
LeftText = Replace(txtkcnamesearch2, "'", "''"
'Query database for items in desired
'field that begin with your typed charac
' ters.
Data2.RecordSource = "Select [field_kcname] From " & _
"[sbt] " & _
"Where [field_kcname] Like '" & LeftText & "*'"
Data2.Refresh
'If no match, leave blank.
If LeftText = "" Then
Answer = ""
Else
'If match, set variable to result.
Answer = Data2.Recordset.Fields("field_kcname"
End If
End If
'Get length of result.
NewText = Len(Answer)
'If no match, fill Text box with
'typed characters only.
If NewText = 0 Then
txtkcnamesearch2 = LeftText
Else
'If query returns result,
'fill Text box with result.
txtkcnamesearch2 = Answer
'Set cursor to end of typed characters
txtkcnamesearch2.SelStart = LengthText
'Select and highlight portion of
'result text that wasn't typed.
txtkcnamesearch2.SelLength = Len(Answer) - LengthText
End If
'Close connection so you can
'connect to another text box.
Data2.RecordSource = "Select * From [qry_sbt_FULL] "
Data2.Refresh
End Sub
' end of auto complete peds search
Private Sub cmdFind2_Click()
Dim DB As Database
Dim rs As Recordset
staffylist2.Clear
Set DB = OpenDatabase("C:\Documents and Settings\webstaff\Desktop\sbtProgramme\sbt.mdb"
Set rs = DB.OpenRecordset("select * FROM sbt " & _
" Where field_kcname " & _
" LIKE '*" & Replace$(txtkcnamesearch2.Text, "'", "''"
& "*'"
Do Until rs.EOF
staffylist2.AddItem rs.Fields("field_title"
& ", " & _
rs.Fields("field_kcname"
& _
" [" & rs.Fields("field_sex"
& "]"
staffylist2.ItemData(staffylist2.NewIndex) = rs.Fields("field_id"
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
I have a textbox(txtkcnamesearch2) that has an auto search sub attached to it, This auto search uses a datacontrol(Data2) to find the records i want to compare to bring up the closest match letter by letter as the user types in ther the textbox, the results are sent to a list box when you hit a command button(cmdFind2).
This all works fine, in fact it works realy well, Im a newbie to VB and was amazed I got it running
The thing thats puzzling me is if you type in a sequence of letters that dont match any of the names in the field being searched it crashes the program.
I have tried to add "On Error Resume Next" but it doesnt help???
Can anyone advice me on a work around for this issue?
Many thanks
Les
'start of autocomplete peds search
Private Sub txtkcnamesearch2_KeyUp(KeyCode As Integer, shift As Integer)
On Error Resume Next
Dim LengthText As Integer
Dim NewText As Integer
Dim Answer As String
Dim LeftText As String
'Get the length of the typed text.
LengthText = Len(Replace$(txtkcnamesearch2, "'", "''"
'Handle Backspace and Delete keys.
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Or KeyCode = vbKeyReturn Then
Exit Sub
Else
On Error Resume Next
'Set variable to the typed text.
LeftText = Replace(txtkcnamesearch2, "'", "''"
'Query database for items in desired
'field that begin with your typed charac
' ters.
Data2.RecordSource = "Select [field_kcname] From " & _
"[sbt] " & _
"Where [field_kcname] Like '" & LeftText & "*'"
Data2.Refresh
'If no match, leave blank.
If LeftText = "" Then
Answer = ""
Else
'If match, set variable to result.
Answer = Data2.Recordset.Fields("field_kcname"
End If
End If
'Get length of result.
NewText = Len(Answer)
'If no match, fill Text box with
'typed characters only.
If NewText = 0 Then
txtkcnamesearch2 = LeftText
Else
'If query returns result,
'fill Text box with result.
txtkcnamesearch2 = Answer
'Set cursor to end of typed characters
txtkcnamesearch2.SelStart = LengthText
'Select and highlight portion of
'result text that wasn't typed.
txtkcnamesearch2.SelLength = Len(Answer) - LengthText
End If
'Close connection so you can
'connect to another text box.
Data2.RecordSource = "Select * From [qry_sbt_FULL] "
Data2.Refresh
End Sub
' end of auto complete peds search
Private Sub cmdFind2_Click()
Dim DB As Database
Dim rs As Recordset
staffylist2.Clear
Set DB = OpenDatabase("C:\Documents and Settings\webstaff\Desktop\sbtProgramme\sbt.mdb"
Set rs = DB.OpenRecordset("select * FROM sbt " & _
" Where field_kcname " & _
" LIKE '*" & Replace$(txtkcnamesearch2.Text, "'", "''"
Do Until rs.EOF
staffylist2.AddItem rs.Fields("field_title"
rs.Fields("field_kcname"
" [" & rs.Fields("field_sex"
staffylist2.ItemData(staffylist2.NewIndex) = rs.Fields("field_id"
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub