AUTOCOMPLETAR DE COMBOBOX COM ERRO APOS DAO 3.6
eu uso um class modulo para fazer aquele serviço de autocompletar de combobox... ou seja, quando eu coloco as iniciais de um nome que contenha na lista do mesmo combo, ele completa, sendo assim dispensa eu ter que digitar o nome completo.
Usava DAO 3.51 e rodava bem, só foi eu colocar para 3.60 que dar erro...
No form eu uso assim:
[ô]no general_declaration
[ô]no form_load
[ô]no form_unload
[ô]no gotfocusdo combo eu coloco assim:
[ô]esse aqui é o class modulo:
ai quando executo e dou o foco ao combo, ele dar o seguinte erro:
Run-time erro [ô]91[ô]
Object variable or with block variable not set
e mando debugar e ele seleciona a linha:
moCombo.AttachTo cboCliente
como eu faço para resolver ou uma outra forma de autocompletar a digitação em combobox.
Usava DAO 3.51 e rodava bem, só foi eu colocar para 3.60 que dar erro...
No form eu uso assim:
[ô]no general_declaration
Private moCombo As cComboHelper
[ô]no form_load
Set moCombo = New cComboHelper
[ô]no form_unload
Set moCombo = Nothing
[ô]no gotfocusdo combo eu coloco assim:
moCombo.AttachTo cboCliente
[ô]esse aqui é o class modulo:
Option Explicit
Private WithEvents moCombo As ComboBox
Private Declare Function SendMessageByString Lib [Ô]user32[Ô] Alias _
[Ô]SendMessageA[Ô] (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As String) As Long
Private Const CB_SETCURSEL = &H14E
Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158
Private Function MatchingRow(ByVal sText$, ByVal mStart&, ByVal iMatchType%) As Long
Dim mFoundRow As Long [ô]the row we find
On Error GoTo Err_MatchingRow
mFoundRow = SendMessageByString(moCombo.hwnd, iMatchType, mStart, sText)
Bye_MatchingRow:
MatchingRow = mFoundRow
Exit Function
Err_MatchingRow:
mFoundRow = -1
Resume Bye_MatchingRow
End Function
Public Sub AttachTo(ByVal oCombo As ComboBox)
On Error Resume Next
Set moCombo = oCombo
End Sub
Private Sub Class_Terminate()
On Error Resume Next
Set moCombo = Nothing
End Sub
Public Function Contains(ByVal sText$) As Boolean
On Error Resume Next
Contains = (SendMessageByString(moCombo.hwnd, CB_FINDSTRINGEXACT, 0, sText) <> -1)
End Function
Private Function ResultingText(iKeyAscii%) As String
Dim sLeft As String [ô]string element
Dim sSel As String [ô]selected string element
Dim sRight As String [ô]string element
Dim sResult As String [ô]what we[ô]ll return
On Error Resume Next
With moCombo
sLeft = Left$(.Text, .SelStart) [ô]SelStart is 0-based
sSel = Mid$(.Text, .SelStart + 1, .SelLength)
sRight = Mid$(.Text, .SelStart + .SelLength + 1)
End With
Select Case iKeyAscii
Case vbKeyBack [ô]Backspace key
If Len(sSel) = 0 Then [ô]nothing selected
sResult = MinusRightChar(sLeft) & sRight [ô]delete first char on the left
Else [ô]selection exists
sResult = sLeft & sRight [ô]delete selected text only
End If
Case vbKeyDelete [ô]Delete key
If Len(sSel) = 0 Then [ô]nothing selected
sResult = sLeft & MinusLeftChar(sRight) [ô]delete first char on the right
Else [ô]selection exists
sResult = sLeft & sRight [ô]delete selected text only
End If
Case Else [ô]an ordinary character
sResult = sLeft & Chr$(iKeyAscii) & sRight
End Select
Bye_ResultingText:
ResultingText = sResult
End Function
Private Function MinusLeftChar(ByVal sGiven As String) As String
On Error Resume Next
If Len(sGiven) = 0 Then
MinusLeftChar = [Ô][Ô]
Else
MinusLeftChar = Mid$(sGiven, 2)
End If
End Function
Private Function MinusRightChar(ByVal sGiven As String) As String
On Error Resume Next
If Len(sGiven) = 0 Then
MinusRightChar = [Ô][Ô]
Else
MinusRightChar = Left$(sGiven, Len(sGiven) - 1)
End If
End Function
Private Sub moCombo_KeyDown(KeyCode As Integer, Shift As Integer)
Dim sSearchOn As String [ô]current string to search on
On Error Resume Next
If KeyCode <> vbKeyDelete Then GoTo Bye_moCombo_KeyDown
sSearchOn = ResultingText(KeyCode)
If SearchOn(sSearchOn, CB_FINDSTRINGEXACT) = True Then KeyCode = 0
Bye_moCombo_KeyDown:
Exit Sub
End Sub
Private Sub moCombo_KeyPress(KeyAscii As Integer)
Dim sSearchOn As String [ô]current string to search on
On Error Resume Next
If KeyAscii = vbKeyReturn Or KeyAscii = vbKeyTab Then
GoTo Bye_moCombo_KeyPress [ô]we don[ô]t want to know
End If
sSearchOn = ResultingText(KeyAscii)
If KeyAscii = vbKeyBack Then [ô]prevent re-finding original !
If SearchOn(sSearchOn, CB_FINDSTRINGEXACT) = True Then KeyAscii = 0
Else
If SearchOn(sSearchOn, CB_FINDSTRING) = True Then KeyAscii = 0
End If
Bye_moCombo_KeyPress:
Exit Sub
End Sub
Private Function SearchOn(ByVal sStartText$, ByVal iMatchType%) As Boolean
Dim mOriginalIndex As Long
Dim mNewIndex As Long
Dim yMoveOccurred As Boolean
On Error Resume Next
mOriginalIndex = moCombo.ListIndex
yMoveOccurred = False
mOriginalIndex = mOriginalIndex - 1
If mOriginalIndex < -1 Then mOriginalIndex = -1
With moCombo
If Len(sStartText) > 0 Then [ô]if it contains any text
mNewIndex = MatchingRow(sStartText, mOriginalIndex, iMatchType)
If mNewIndex <> -1 Then [ô]match found
SendMessageByString .hwnd, CB_SETCURSEL, mNewIndex, 0 [ô]set list index
.SelStart = Len(sStartText)
.SelLength = Len(.Text) - Len(sStartText) [ô]select the bit AFTER the match
yMoveOccurred = True
End If
Else [ô]contains no text
SendMessageByString .hwnd, CB_SETCURSEL, -1, 0 [ô]set list index to -1
If mOriginalIndex <> -1 Then yMoveOccurred = True
End If
End With
SearchOn = yMoveOccurred
End Function
ai quando executo e dou o foco ao combo, ele dar o seguinte erro:
Run-time erro [ô]91[ô]
Object variable or with block variable not set
e mando debugar e ele seleciona a linha:
moCombo.AttachTo cboCliente
como eu faço para resolver ou uma outra forma de autocompletar a digitação em combobox.
tenta isto
Public Sub AttachTo(ByVal oCombo As ComboBox)
[ô] On Error Resume Next
Set moCombo = New oCombo
End Sub
Eu troquei esse:
por esse:
continuou o mesmo erro:
Public Sub AttachTo(ByVal oCombo As ComboBox)
On Error Resume Next
Set moCombo = oCombo
End Sub
por esse:
Public Sub AttachTo(ByVal oCombo As ComboBox)
[ô] On Error Resume Next
Set moCombo = New oCombo
End Sub
continuou o mesmo erro:
teste aqui efetuado com sucesso
no load do form não usei o set, pois se vc declarou moCombo como cComboHelper, não precisa seta-lo
[ô] no general
Private moCombo As New cComboHelper
no load do form não usei o set, pois se vc declarou moCombo como cComboHelper, não precisa seta-lo
[ô] gotfocus do combo
Private Sub Combo1_GotFocus()
moCombo.AttachTo Combo1
End Sub
ps: não tem nada a ver com DAO 3.6
com essas ultimas alterações funcionou direito
Obrigado Marcelo_Treze e um Prospero ano novo para ti e sua familia
Obrigado Marcelo_Treze e um Prospero ano novo para ti e sua familia
Tópico encerrado , respostas não são mais permitidas