|
-
Apr 18th, 2008, 06:04 PM
#1
Thread Starter
Hyperactive Member
Autocomplete textbox question
Hello all,
I the following code (with the help of others) which looksup info in my listbox.
vb Code:
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub txtLookup_Change()
Dim strText$
strText = Trim$(txtLookUp.Text)
If strText <> "" Then
lstRadioSerials.ListIndex = FindItem(lstRadioSerials, strText)
Else
MsgBox "No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match"
End If
End Sub
Private Function FindItem(lst As ListBox, strText As String) As Integer
Dim iIndex As Integer
iIndex = SendMessage(lst.hwnd, LB_FINDSTRINGEXACT, -1, ByVal strText)
FindItem = iIndex
End Function
This works for looking up the certain serials in my listbox, but what I would like to do is refine it a little more.
What I would like to add is this: when a user starts to type in the serial number and he types a wrong number (ie. he is looking for 1234 and he types 1224), then I would like an error message to state that there is no record with that serial number.
Right now when the user types the wrong number, the only thing that happens is that the highlighted part will not move anymore in the listbox.
I hope that I am not too confusing?
-
Apr 18th, 2008, 11:55 PM
#2
Member
Re: Autocomplete textbox question
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim o2 As Object, o1 As Object,NotOnList as Boolean
Set o2 = Text1
Set o1 = WhateverListBoxName
If KeyAscii = 8 Then 'Backspace key
CheckComboInput o1, o2, KeyAscii
Exit Sub
End If
CheckComboInput o1, o2, KeyAscii,NotOnList:KeyAscii = 0
If NotOnLIst=True then …….give the MESSAGE or Whatever
End Sub
Public Sub CheckComboInput(c1 As Object, c2 As Object, N As_ Integer,NotOnList)
'c1 as object the object is the ListBox that holds the list eg SerialNumbers
'c2 as object is the Text control
' N as Integer is the Ascii val of the last character entered by the user
'This procedure is accessed by Textbox keypress proceedures to check if what
'is being entered is on the ListBox list
Dim L As Integer, S As Integer, T As String, N2 As Integer
N2 = N ' N2 holds keyascii so keyascii is returned unchanged as is on next line
If N = 8 Then N = 0
S = c2.SelStart: L = c2.SelLength: T = c2.Text
T = Left(T, S) & Chr(N) & Right(T, Len(T) - S)
S = S + 1: I = 0
Do While (I < c1.ListCount - 1) And StrComp(Left(T, S), Left(c1.List(I), S), vbTextCompare) <> 0
I = I + 1
Loop
If UCase(Left(T, S)) = UCase(Left(c1.List(I), S)) Then
T = c1.List(I): L = Len(T) - S
c1.Selected(I) = True
Else
For I = 0 To c1.ListCount - 1
If c1.Selected(I) = True Then
c1.Selected(I) = False: NotOnList=true
else
NotOnList=true
Next
T = Left(T, S): L = 0
End If
c2.Text = T: c2.SelStart = S: c2.SelLength = 1
N = N2
End Sub
Last edited by dasjoe; Apr 19th, 2008 at 09:48 PM.
-
Apr 19th, 2008, 05:55 AM
#3
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
dasjoe,
Thanks for the reply; I'll give your code a try.
Also, is there anyway that I can just add to the code that I am already using?
-
Apr 19th, 2008, 06:16 AM
#4
Member
Re: Autocomplete textbox question
The CheckComboInput o1, o2, KeyAscii,NotOnList routine can be placed on a
module, just make sure it is public. You can then call it from any list box
keypress. It is basically an auto complete routine, it will complete the entry
if it is listed on the list box. The NotOnList returns true if as soon as a key is pressed that does not make up what ever is listed in the list box. Only use the
NotOnList if its needed otherwise use it as an autocomplete.
-
Apr 19th, 2008, 07:30 AM
#5
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
dasjoe,
I get an error in the following block of code:
vb Code:
S = c2.SelStart: L = c2.SelLength: T = c2.Text
Error: "Object doesnt support this property or method"
Do I need to reference this? Not sure what to do.
Thanks
-
Apr 19th, 2008, 08:48 AM
#6
Re: Autocomplete textbox question
 Originally Posted by cfd33
This works for looking up the certain serials in my listbox, but what I would like to do is refine it a little more.
What I would like to add is this: when a user starts to type in the serial number and he types a wrong number (ie. he is looking for 1234 and he types 1224), then I would like an error message to state that there is no record with that serial number.
Right now when the user types the wrong number, the only thing that happens is that the highlighted part will not move anymore in the listbox.
I hope that I am not too confusing?
A very simple solution for you is to throw away the listbox and the textbox, replace both of them with a single combobox.
A combobox has 2 parts, a listbox and a textbox. It allows user to select an item in the list, it also allow user to type in.
You can set MatchRequire to True, it will do the job for you and it also do autocomplete.
-
Apr 19th, 2008, 12:15 PM
#7
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
anhn,
Thanks for the reply, but I would like to keep my setup the way it is now (with the listbox and textbox).
-
Apr 19th, 2008, 09:31 PM
#8
Member
Re: Autocomplete textbox question
Sorry, Missed KeyAscii = 0
also set objects reversed
Try now, I have amended
Also added Space Key provisions
The List Box can be invisible if you like or add code so double clicking the listbox item will populate the test box
Last edited by dasjoe; Apr 19th, 2008 at 09:43 PM.
-
Apr 19th, 2008, 09:51 PM
#9
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
Thanks,
I will try it when I get back to work on Monday.
-
Apr 21st, 2008, 02:29 PM
#10
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
dasjoe,
I get an error message at this code:
vb Code:
Private Sub txtLookup_KeyPress(KeyAscii As Integer)
Dim o2 As Object, o1 As Object, NotOnList As Boolean
Set o1 = lstRadioSerials
Set o2 = txtLookup
If KeyAscii = 8 Then 'Backspace key
[COLOR="SeaGreen"]ChkListBox [/COLOR]o1, o2, KeyAscii <---Eror Here on ChkListBox!!
Exit Sub
End If
ChkListBox o1, o2, KeyAscii, NotOnList: KeyAscii = 0
If NotOnList = True Then
Call MsgBox("No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match")
End If
End Sub
"Argument not optional"
Also, if a person types a serial that is not in the db, then where would I put a msgbox telling the users that there is no serial number like that in the db?
dasjoe, I removed this part of the IF statement and now it allows backspace:
vb Code:
If KeyAscii = 8 Then 'Backspace key
'ChkListBox o1, o2, KeyAscii
Exit Sub
End If
Is this ok to do?
Is this part the part of code that tells the user that there is no record match if they should type a serial that is not in the database?
vb Code:
If NotOnList = True Then
Call MsgBox("No matching record found; Please check your criteria!", vbInformation + vbOKOnly, "No Record Match")
End If
if it is, it is not working. (ie. 1234 and I type 1233, then I want a message box to appear stating that there is no serial like that)
____________________________________________________________________________________________________ ________________________________
Ahhh, never mind on the messagebox! I placed the code for the messagebox in the module instead of the Keypress event and now it works just fine.
Last edited by cfd33; Apr 21st, 2008 at 02:43 PM.
-
Apr 21st, 2008, 05:13 PM
#11
Member
Re: Autocomplete textbox question
The messagebox is better left where it is, as this provides flexibility.
The CheckListBox routine can be called from any text box/List Box combination. Good to have a flexible adaptable working routine that can be added to any project, as the message from message box may not always be the same let alone the same style.
However after checking the original code I provided the was an error
Missing end if onCheckComboBox
Also I Noted the Ascii =8 'Space Bar
incorrect, code is correct but Ascii 8 is backspace key. This line of code is required because it it isn't a backspace will invoke a not on list message.
so when a backspace is entered, also you can not deleted using backspace because after call the CheckCombo routine Ascii is set to 0 meaning no
backspace as backspace=8
I have re-supplied ammended code, Sorry for not checking and running the code earlier, but did not have time.
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim o2 As Object, o1 As Object, NotOnList As Boolean
Set o2 = Text1
Set o1 = List1
If KeyAscii = 8 Then 'Backspace key
CheckComboInput o1, o2, KeyAscii, NotOnList
Exit Sub
End If
CheckComboInput o1, o2, KeyAscii, NotOnList: KeyAscii = 0
If NotOnList = True Then
Dim msg As String, Style As String, Title As String
msg = " Item not listed on Data Base."
Style = vbInformation + vbOKOnly
Title = "Listing not found"
MsgBox msg, Style, Title
End If
End Sub
Public Sub CheckComboInput(c1 As Object, c2 As Object, N As Integer, NotOnList)
'c1 as object the object is the ListBox that holds the list eg SerialNumbers
'c2 as object is the Text control
' N as Integer is the Ascii val of the last character entered by the user
'This procedure is accessed by Textbox keypress proceedures to check if what
'is being entered is on the ListBox list
Dim L As Integer, S As Integer, T As String, N2 As Integer
N2 = N ' N2 holds keyascii so keyascii is returned unchanged as is on next line
If N = 8 Then N = 0
S = c2.SelStart: L = c2.SelLength: T = c2.Text
T = Left(T, S) & Chr(N) & Right(T, Len(T) - S)
S = S + 1: I = 0
Do While (I < c1.ListCount - 1) And StrComp(Left(T, S), Left(c1.List(I), S), vbTextCompare) <> 0
I = I + 1
Loop
If UCase(Left(T, S)) = UCase(Left(c1.List(I), S)) Then
T = c1.List(I): L = Len(T) - S
c1.Selected(I) = True
Else
For I = 0 To c1.ListCount - 1
If c1.Selected(I) = True Then
c1.Selected(I) = False: NotOnList = True
Else
NotOnList = True
End If
Next
T = Left(T, S): L = 0
End If
c2.Text = T: c2.SelStart = S: c2.SelLength = 1
N = N2
End Sub
Last edited by dasjoe; Apr 21st, 2008 at 08:04 PM.
-
Apr 21st, 2008, 07:47 PM
#12
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
dasjoe,
I must not understand about the msgbox; when I try the code like you have it below:
vb Code:
If NotOnList = True Then
Dim msg As String, Style As String, Title As String
msg = " Item not listed on Data Base."
Style = vbInformation + vbOKOnly
Title = "Listing not found"
MsgBox msg, Style, Title
End If
it does not fire, but when I place the msgbox in the module, then it works great.
What am I doing wrong?
This is the whole code for the Keypress:
vb Code:
Private Sub txtLookup_KeyPress(KeyAscii As Integer)
Dim o2 As Object, o1 As Object, NotOnList As Boolean
Set o1 = lstRadioSerials
Set o2 = txtLookUp
If KeyAscii = 8 Then 'Backspace key
ChkListBox o1, o2, KeyAscii, NotOnList
Exit Sub
End If
ChkListBox o1, o2, KeyAscii, NotOnList: KeyAscii = 0
If NotOnList = True Then
Dim msg As String, Style As String, Title As String
msg = " Item not listed on Data Base."
Style = vbInformation + vbOKOnly
Title = "Listing not found"
MsgBox msg, Style, Title
End If
End Sub
Last edited by cfd33; Apr 21st, 2008 at 07:57 PM.
-
Apr 21st, 2008, 08:11 PM
#13
Member
Re: Autocomplete textbox question
I have just did a copy and past straight from this site onto a new VB6
project, added a textbox named Text1 and a ListBox name List1
Left code as is and works fine. So ??? suggest you do the same on a new
project. Do you have VB6 ??? By doing this you can prove at least this part
of the coding.
Cheers
-
Apr 21st, 2008, 08:50 PM
#14
Thread Starter
Hyperactive Member
Re: Autocomplete textbox question
dasjoe,
I will give what you said a try. Yes, I have vb6.
I really appreciate all of your help!!
I am still a newbie at this vb6, so please excuse me.
-
Apr 21st, 2008, 11:49 PM
#15
Member
Re: Autocomplete textbox question
No problem, we all start somewhere
Last edited by dasjoe; Apr 22nd, 2008 at 12:15 AM.
-
Apr 22nd, 2008, 02:45 AM
#16
Addicted Member
Re: Autocomplete textbox question
 Originally Posted by anhn
You can set MatchRequire to True, it will do the job for you and it also do autocomplete.
MatchRequire is not available in VB6.0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|