Important alert: (current site time 7/16/2013 3:57:59 AM EDT)
 

VB icon

AutoCompleter - Class Module

Email
Submitted on: 10/17/1999
By: dmbbob 
Level: Not Given
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 27828
 
     This code allows you to have an autocomplete function on any text boxes by creating an instance of the class module below and setting a text control on a form to is CompleteTextbox property. Ideal for those situations when you have multiple autocompletes. (Visual Basic 6 Only - Can easily be modified for 5.0 users)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: AutoCompleter - Class Module
' Description:This code allows you to have an autocomplete function on any text boxes by creating an instance of the class module below and setting a text control on a form to is CompleteTextbox property. Ideal for those situations when you have multiple autocompletes. (Visual Basic 6 Only - Can easily be modified for 5.0 users)
' By: dmbbob
'
' Inputs:
Dim m_objAutoCompleteUser as clsAutoComplete
Set m_objAutoCompleteUser = New clsAutoComplete
With m_objAutoCompleteUser
 .SearchList = m_strUserList
 Set .CompleteTextbox = txtUser
 .Delimeter = ","
End With
'
' Assumes:Create a new class module.
Paste all the code below into it.
Rename the module to clsAutoComplete.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4073&lngWId=1'for details.'**************************************

Option Explicit
Private WithEvents m_txtComplete As TextBox
Private m_strDelimeter As String
Private m_strList As String
Private Sub m_txtComplete_KeyUp(KeyCode As Integer, Shift As Integer)
 
 Dim i As Integer
 Dim strSearchText As String
 Dim intDelimented As Integer
 Dim intLength As Integer
 Dim varArray As Variant
 
 With m_txtComplete
If KeyCode <> vbKeyBack And KeyCode > 48 Then 
 If InStr(1, m_strList, .Text, vbTextCompare) <> 0 Then
varArray = Split(m_strList, m_strDelimeter)
 
For i = 0 To UBound(varArray)
 strSearchText = Trim(varArray(i))
 
 If InStr(1, strSearchText, .Text, vbTextCompare) And
(Left$(.Text, 1) = Left$(strSearchText, 1)) And 
.Text <> "" Then
.SelText = ""
.SelLength = 0
intLength = Len(.Text)
.Text = .Text & Right$(strSearchText, Len(strSearchText) - Len(.Text))
.SelStart = intLength
.SelLength = Len(.Text)
Exit Sub
 End If
 
Next i
 End If
End If
 End With
 
End Sub
Public Property Get CompleteTextbox() As TextBox
 Set CompleteTextbox = m_txtComplete
End Property
Public Property Set CompleteTextbox(ByRef txt As TextBox)
 Set m_txtComplete = txt
End Property
Public Property Get SearchList() As String
 SearchList = m_strList
End Property
Public Property Let SearchList(ByVal str As String)
 m_strList = str
End Property
Public Property Get Delimeter() As String
 Delimeter = m_strDelimeter
End Property
Public Property Let Delimeter(ByVal str As String)
 m_strDelimeter = str
End Property


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
11/11/1999 8:18:00 PMSTEPPA

Kewl code but I can't get it to work...Any ideas?
(If this comment was disrespectful, please report it.)

 
11/11/1999 8:34:00 PMdmbbob

Well are you using VB 6.0? If so, could you tell me where you're having problems...Maybe I can help. You should be able to pop it right in....


(If this comment was disrespectful, please report it.)

 
11/21/1999 11:43:00 AMFredrik Stai

If someone could just take some minutes making a VB5-version of this code and post it to PlanetSourceCode.. (c;
(If this comment was disrespectful, please report it.)

 
11/22/1999 9:08:00 AMdmbbob

The only thing this code needs to work with VB5 is something that resembled the split function in VB6. Search for Jean-Philippe Leconte's split function listed on Planet Source Code. Paste the function into this class module.

Suggestion: Change the function from Public to Private to hide it from the calling objects.
(If this comment was disrespectful, please report it.)

 
11/30/1999 2:23:00 AMAtique

I could not find it work. I am using VB6. I want to know what to write in form and what controls to put on the form.
(If this comment was disrespectful, please report it.)

 
11/30/1999 12:35:00 PMdmbbob

On a form you should have a textbox that you'd like to have autocomplete.

In the form you should do the following in the Form_Load() event.

Set m_objAutoCompleteUser = New clsAutoComplete


With m_objAutoCompleteUser
.SearchList = m_strUserList
Set .CompleteTextbox = Text1
.Delimeter = ","
End With

As a global declarartion within the form
Dim m_objAutoCompleteUser as clsAutoComplete

Should work fine...

(If this comment was disrespectful, please report it.)

 
2/29/2000 4:15:17 PMtodd

dmbbob: i know that you pasted in a feedback, but just a sugestion, change the code in the submission so that when users copy it, they will under stand that
"Dim m_objAutoCompleteUser As clsAutoComplete" must be in the (General) Declarations section.

BTW: this is the BEST BEST Autocomplete i have seen. the way you have it just parsing a string is perfect for performance...

im impressed =)
thanks for the code
(If this comment was disrespectful, please report it.)

 
3/30/2000 11:30:35 AMDarkDraco

I'm probably going to sound Thick but oh well,

i can't get this thing to work, whatever i type in the box doesnt get auto competed if i retype it in the same session...

after running the code line by line, i noticed that "m_strList" is always empty, meaning that "If InStr(1, m_strList, .Text, vbTextCompare) <> 0" is never valid and therefore never enters in the if.

i was wondering, do i need to add code to fill an array or to set a value to "m_strList" whenever i type something?

or should this code work by itself with nothing to be added?

also, is it possible that to put this code into an activeX control would require a complete rewrite of the code?

im thinking that it probably does since it doesnt let you define "Public Property Get CompleteTextbox() As TextBox" because TextBox is a private object type

but im not shure
(If this comment was disrespectful, please report it.)

 
4/16/2000 5:39:43 PMProgrammer411

I don't know what I'm doing wrong!

I copied the code into the class module (clsAutoComplete) and I added

Dim m_objAutoCompleteUser As clsAutoComplete

to form load and the general declarations sections. I also placed a txtbox (txtAuto) on the form.

What's keeping this from working?

>>Programmer411
>>Marztek Software
>>Marztek.itgo.com
>>webmaster@marztek.itgo.com
(If this comment was disrespectful, please report it.)

 
4/17/2000 9:11:05 AMdmbbob

The object should be declared as a global object within the Form Module.
(If this comment was disrespectful, please report it.)

 
4/17/2000 9:12:13 AMdmbbob

Darn...this is cutting off my text...lemme try again....
The object should be declared as a global object within the Form Module.
Option Explict
Dim m_objAutoCompleteUser as clsAutoComplete

Within the Form_Load() event you'll set up the object like this:

Private Sub Form_Load()
Set m_objAutoCompleteUser = New clsAutoComplete

With m_objAutoCompleteUser
.SearchList = m_strUserList
Set .CompleteTextbox = txtUser
.Delimeter = ","
End With
End Sub

Give it a shot...
(If this comment was disrespectful, please report it.)

 
8/19/2000 9:00:13 PMliliafan

I am getting an error here

Error "459" object does not support events

Public Property Set CompleteTextbox(ByRef txt As TextBox)
Set m_txtComplete = txt
End Property

Any ideas what I am doing wrong?
(If this comment was disrespectful, please report it.)

 
8/28/2000 2:00:53 PMDarrin

This code looks awesome, and I would love to use it, but I can't figure out where it gets the key to match up to the list of items??? Does there need to be someting in the keypress event for that particular txtbox?? Please help. Thank you in advance!!!
(If this comment was disrespectful, please report it.)

 
8/29/2000 11:04:54 AMdmbbob

I trap the events of the textbox using the
(If this comment was disrespectful, please report it.)

 
10/19/2000 9:50:41 AMCape

The Code is excelente, but if you change this part:
-----------
If InStr(1, strSearchText, .Text, vbTextCompare) And (Left$(.Text, 1) = Left$(strSearchText, 1)) And .Text <> "" Then
-----------------
by this:
-----------
If InStr(1, strSearchText, .Text, vbTextCompare) And (.Text = Left$(strSearchText, len(.text))) And .Text <> "" Then
-----------------
will be better.
Thanx for the code.!

(If this comment was disrespectful, please report it.)

 
5/13/2001 12:48:36 AMTerrell Davis

In order for your routine to be case insensitive you need to modify this line:
If InStr(1, strSearchText, .Text, vbTextCompare) And
(Left$(.Text, 1) = Left$(strSearchText, 1)) And
.Text <>
(If this comment was disrespectful, please report it.)

 
9/3/2002 9:34:40 PM

I get an error with m_strUserList not defined. How can I fix this?
(If this comment was disrespectful, please report it.)

 
4/20/2003 11:49:15 AM§e7eN

m_strUserList is suppose to contain the list of users you want to use.
example, put "m_strUserlist = "John,Dick,Harry" in the form_load() event. Dont forget to define it too, dim m_strUserlist as string.
Anyways, Awsome code! now i just goto find something to use it in :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.