Jump to content
Agnaldo André

Ocultar dados repetidos em um combobox

Recommended Posts

Agnaldo André

Olá, Boa noite.

Projeto em VB, uma combobox carrega dados (Tabela endereço) de um banco de dados Access, porém existem vários endereços que se repetem.

Preciso de um código que restrinja a exibição de dados repetidos e conforme o usuário digita vá preenchendo a combobox (txt_endereco)

Private Sub txt_endereco_Change()
On Error GoTo erro
Set db = OpenDatabase(dirdb & "bdcrim.mdb")
Set Tabela = db.OpenRecordset("SELECT distinct Endereco FROM tblCadastro ")
                              
    While Not Tabela.EOF
        txt_endereco.AddItem Tabela!Endereco & ""
        Tabela.MoveNext
    Wend
Exit Sub



Private Sub txt_endereco_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Resume Next
    If KeyCode = 8 Then 'Backspace
        txt_endereco.SelStart = txt_endereco.SelStart - 1
        txt_endereco.SelLength = Len(txt_endereco)
    ElseIf KeyCode = 27 Then 'Esc
        txt_endereco.Text = Empty
    ElseIf KeyCode = 40 Then 'Seta para Baixo
        If listEndereço.Visible = True Then
            listEndereço.Text = txt_endereco.Text
            listEndereço.Visible = True
        Else
            Set Tabela = db.OpenRecordset("SELECT Endereço FROM tblCadastro WHERE Endereço Like '" & Mid(txt_endereco.Text, 1, txt_endereco.SelStart) & "*' ORDER BY Endereço Asc")
                                         
            listEndereço.Clear
            Do Until Tabela.EOF
                DoEvents
                'listNasc.AddItem Tabela("Local_nasc")
                listEndereço.AddItem Tabela![Endereço]
                Tabela.MoveNext
            Loop
            If Tabela.RecordCount > 8 Then listEndereço.Height = 225 * 8 Else listEndereço.Height = 225 * Tabela.RecordCount
            listEndereço.Visible = True
            listEndereço.Text = txt_endereco.Text
            listEndereço.SetFocus
        End If
    End If
End Sub



Private Sub txt_endereco_KeyPress(KeyAscii As Integer)
Dim cb As Long
    Dim FindString As String
    Const CB_ERR = (-1)
    Const CB_FINDSTRING = &H14C
    
    If KeyAscii < 32 Or KeyAscii > 127 Then Exit Sub
    
    If txt_endereco.SelLength = 0 Then
        FindString = txt_endereco.Text & Chr$(KeyAscii)
    Else
        FindString = Left$(txt_endereco.Text, txt_endereco.SelStart) & Chr$(KeyAscii)
    End If
    
    cb = SendMessage(txt_endereco.hwnd, CB_FINDSTRING, -1, ByVal FindString)
    
    If cb <> CB_ERR Then
        txt_endereco.ListIndex = cb
        txt_endereco.SelStart = Len(FindString)
        txt_endereco.SelLength = Len(txt_endereco.Text) - txt_endereco.SelStart
    End If
    KeyAscii = 0
End Sub

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.