can someone help me how to convert this code to public function... this code works but the problem is i have to copy it in every combo box command. here's the code
VB Code:
Option Explicit
Dim InChangeMode As Boolean
Dim BackSpacePressed As Boolean
Private Sub Combo1_Change()
On Error Resume Next
Dim idx As Integer
Dim SelFrom As Integer
If InChangeMode Then Exit Sub
If BackSpacePressed Then
BackSpacePressed = False
Exit Sub
End If
With Combo1
If Trim(.Text) = "" Then Exit Sub
If .ListCount < 0 Then Exit Sub
InChangeMode = True
For idx = 0 To .ListCount
If UCase(.Text) = UCase(Left(.list(idx), Len(.Text))) Then Exit For
'' If your data in Combox is sorted than add the next line
' If UCase(.Text) < UCase(Left(.List(idx), Len(.Text))) Then GoTo EndOfProcess
I'm not sure what you mean by "every combo box command". If you want to use the same code for many combo boxes on the same form then you can create a control array. The structure will look like this:
VB Code:
Private Sub Combo1_Change(Index As Integer)
With Combo1(Index)
' your code in here...
End With
End Sub
Private Sub Combo1_KeyPress(Index As Integer, KeyAscii As Integer)
hi, thanx for d reply but wat i mean for every combo box command is i will not put dat code in every combo1_change() , combo2_change() etc... i can access it publicly so dat every combo box in my form has autocomplete.
That's not possible with a self-contained function. You would need to make an AutoComplete class and then declare a separate class for each combo box. If you want to do it that way I can try an example for ya
Sorry, but there aren't many people who'll just create a class for you. Besides, wouldn't it be more fun to learn how to do it rather than get away with it being done for you? It's really not that hard at all. Try searching google, or the forums. Once you get the hang of them, they'll become part of nearly every program you write.
@The Rest of us:
I think it's time we covered some of the stuff like API's, Classes, Etc. Unless there are some pretty easy to understand ones already on the forums?
That's not possible with a self-contained function. You would need to make an AutoComplete class and then declare a separate class for each combo box. If you want to do it that way I can try an example for ya
Here it is, might have a few bugs but it seems to work.
BTW, I don't usually just write code for people, but in this case it was an example that I already had (part of it anyway) I just wrapped it in a class to show you how it is done. Hopefully you learn something from it for the future