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 & ""
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
            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")
            Do Until Tabela.EOF
                'listNasc.AddItem Tabela("Local_nasc")
                listEndereço.AddItem Tabela![Endereço]
            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
        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)
        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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.