
Estoy realizando un proyecto en Visual Basic (es mi 1º vez) y he tenido algunos inconvenientes como este que les contaré. Resulta que en un Form coloqué un código que sirve para 'autocompletar' un textbox mientras se escribe, buscando datos desde una base de datos Acces. El codigo para el autocompletado es el sig.:
Código:
Más abajo, tengo un código para cargar un valor autonumérico en un textbox.Option Explicit Dim CnN As New ADODB.Connection Dim Rst As New ADODB.Recordset Public Sub CnX() On Local Error GoTo er With CnN .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & (CurDir(App.Path) & "\kiosco.mdb") & ";" .Open End With Exit Sub er: MsgBox "Error Numero " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, Err.Source End End Sub Private Sub Form_Load() Call abrir lst1.Height = 0: lst2.Height = 0 CnX End Sub Private Sub lst1_Click() lst2.Selected(lst1.ListIndex) = True End Sub Private Sub lst1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then lst1.Clear: lst1.Visible = False lst2.Clear: lst2.Visible = False End If End Sub Private Sub lst1_KeyPress(KeyAscii As Integer) On Local Error Resume Next If KeyAscii = 13 Then Dim RstCod As New ADODB.Recordset With RstCod .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic .Find "CODIGO='" & lst1.Text & "'" Text6.Text = .Fields(0) Text4.Text = .Fields(1) Text3.Text = .Fields(2) Combo1.Text = .Fields(5) .Close lst1.Visible = False lst2.Visible = False End With End If End Sub Private Sub lst2_Click() lst1.Selected(lst2.ListIndex) = True End Sub Private Sub lst2_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then lst1.Clear: lst1.Visible = False lst2.Clear: lst2.Visible = False End If End Sub Private Sub Text6_Change() On Local Error Resume Next Text6.Text = UCase(Text6.Text) Text6.SelStart = Len(Trim(Text6.Text)) lst1.Clear lst2.Clear If Len(Trim(Text6.Text)) <= 0 Then lst1.Clear: lst2.Clear lst1.Visible = False: lst2.Visible = False Exit Sub Else With Rst .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic Do While Not .EOF If Mid(.Fields(0), 1, Len(Text6)) = Mid(Text6, 1, Len(Text6)) Then lst1.AddItem .Fields(0): lst2.AddItem .Fields(1) End If .MoveNext Loop .Close End With If lst1.ListCount > 0 Then If lst1.ListCount > 3 Then lst1.Height = lst1.ListCount * 200 lst2.Height = lst2.ListCount * 200 lst1.Visible = True: lst2.Visible = True Else lst1.Height = lst1.ListCount * 300 lst2.Height = lst2.ListCount * 300 lst1.Visible = True: lst2.Visible = True End If Else lst1.Visible = False lst2.Visible = False End If End If End Sub Private Sub Text6_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then lst1.Clear: lst1.Visible = False lst2.Clear: lst2.Visible = False End If End Sub Private Sub Text6_KeyPress(KeyAscii As Integer) On Local Error Resume Next If KeyAscii = 13 Then Dim RstCod As New ADODB.Recordset With RstCod .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic .Find "CODIGO='" & Text6.Text & "'" Text6.Text = .Fields(0) Text4.Text = .Fields(1) Text3.Text = .Fields(2) Combo1.Text = .Fields(5) .Close lst1.Visible = False lst2.Visible = False End With End If End Sub Private Sub Text6_KeyUp(KeyCode As Integer, Shift As Integer) On Local Error Resume Next Select Case KeyCode Case vbKeyUp lst1.SetFocus lst1.ListIndex = lst1.ListIndex + 1 Case vbKeyDown lst1.SetFocus lst1.ListIndex = lst1.ListIndex + 1 End Select End Sub Private Sub lst2_KeyPress(KeyAscii As Integer) On Local Error Resume Next If KeyAscii = 13 Then Dim RstPro As New ADODB.Recordset With RstPro .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic .Find "detalle='" & lst2.Text & "'" Text6.Text = .Fields(0) Text4.Text = .Fields(1) Text3.Text = .Fields(2) Combo1.Text = .Fields(5) .Close lst1.Visible = False lst2.Visible = False End With End If End Sub Private Sub Text4_Change() On Local Error Resume Next Text4.Text = UCase(Trim(Text4.Text)) Text4.SelStart = Len(Trim(Text4.Text)) lst1.Clear lst2.Clear If Len(Trim(Text4.Text)) <= 0 Then lst1.Clear: lst2.Clear lst1.Visible = False: lst2.Visible = False Exit Sub Else With Rst .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic Do While Not .EOF If Mid(UCase(.Fields(1)), 1, Len(Text4)) = Mid(UCase(Text4), 1, Len(Text4)) Then lst1.AddItem .Fields(0): lst2.AddItem .Fields(1) End If .MoveNext Loop .Close End With If lst2.ListCount > 0 Then If lst2.ListCount > 3 Then lst1.Height = lst1.ListCount * 200 lst2.Height = lst2.ListCount * 200 lst1.Visible = True: lst2.Visible = True Else lst1.Height = lst1.ListCount * 300 lst2.Height = lst2.ListCount * 300 lst1.Visible = True: lst2.Visible = True End If Else lst1.Visible = False lst2.Visible = False End If End If End Sub Private Sub Text4_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then lst1.Clear: lst1.Visible = False lst2.Clear: lst2.Visible = False End If End Sub Private Sub Text4_KeyPress(KeyAscii As Integer) On Local Error Resume Next If KeyAscii = 13 Then Dim RstPro As New ADODB.Recordset With RstPro .Open "Select * From stock", CnN, adOpenDynamic, adLockOptimistic .Find "detalle='" & Text4.Text & "'" Text6.Text = .Fields(0) Text4.Text = .Fields(1) Text3.Text = .Fields(2) Combo1.Text = .Fields(5) .Close lst1.Visible = False lst2.Visible = False End With End If End Sub Private Sub Text4_KeyUp(KeyCode As Integer, Shift As Integer) On Local Error Resume Next Select Case KeyCode Case vbKeyUp lst2.SetFocus lst2.ListIndex = lst2.ListIndex + 1 Case vbKeyDown lst2.SetFocus lst2.ListIndex = lst2.ListIndex + 1 End Select End Sub
Código:
Cuando pruebo la aplicación, me marca el sig. error: Error de compilacion. Variable no definida.Private Sub Image3_Click() Text2.SetFocus x = "Select max(NumeroFactura) from ventas" conexion_tablas.Open x, conexion_basedatos Text1 = conexion_tablas(0) + 1 conexion_tablas.Close End Sub
img696.imageshack.us/img696/6812/capturao.png
Cabe destacar que si quito el codigo del autocompletado, la aplicacion funciona sin problemas

Ojala puedan ayudarme. Les estaría muy agradecido.