Public ssql As String
Public mysql As String
Public myrcs As Recordset
Public cnn As Connection
Public rcs As Recordset
Public datos As String
Dim i As Integer
Dim j As Integer
Dim tablatype() As String
Dim tablaname() As String
Private Sub Command1_Click()
CommonDialog1.CancelError = True
On Local Error GoTo errhandler
CommonDialog1.Flags = cdOFNHideReadOnly
CommonDialog1.DialogTitle = "Escoge la base de datos que quieres utilizar"
CommonDialog1.Filter = "Archivos de Microsoft Access" & "(*.mdb)|*.mdb|"
CommonDialog1.FilterIndex = 2
CommonDialog1.ShowOpen
datos = CommonDialog1.FileName
Text1 = Empty
abrir
i = Empty
Grid.Clear
cbotabla.Clear
cbocampo.Clear
cbotabla.SetFocus
Set vertabla = cnn.OpenSchema(adSchemaTables)
ReDim Preserve tablaname(0)
ReDim Preserve tablatype(0)
Do While Not vertabla.EOF
If vertabla!TABLE_TYPE = "TABLE" Then
tablaname(i) = vertabla!TABLE_NAME
i = i + 1
ReDim Preserve tablaname(i)
End If
vertabla.MoveNext
Loop
For j = 0 To i - 1
cbotabla.AddItem tablaname(j)
Next j
cnn.Close
Exit Sub
errhandler:
MsgBox Err.Number & Err.Description
Exit Sub
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
busca
End Sub
Sub busca()
On Local Error GoTo tu
Grid.Clear
Grid.Cols = 1
Grid.Rows = 1
abrir
ssql = "select * from " & cbotabla.Text
If Trim(cbocampo.Text) <> "" And Trim(Text1.Text) <> "" Then
ssql = ssql & " where " & cbocampo.Text & " = '" & Text1.Text & "'"
End If
Set rcs = New Recordset
rcs.Open ssql, cnn
If rcs.BOF Then Exit Sub
rcs.MoveFirst
With Grid
.Clear
.Cols = cbocampo.ListCount + 1
For p = 0 To cbocampo.ListCount - 1
.Row = 0
.Col = p + 1
.Text = rcs.Fields(p).Name
Next p
r = 1
Do While Not rcs.EOF
.Rows = r + 1
.Row = r
For p = 0 To cbocampo.ListCount - 1
.Col = p
If IsNull(rcs(rcs.Fields(p).Name)) Then
.Text = "nulo"
Else
.Text = rcs(rcs.Fields(p).Name)
End If
Next p
r = r + 1
rcs.MoveNext
Loop
End With
Set rcs = Nothing
Exit Sub
tu:
MsgBox Err.Number & Err.Description
Exit Sub
End Sub
Private Sub cbocampo_GotFocus()
On Local Error GoTo gg
cbocampo.Clear
mysql = "select * from " & cbotabla.Text
abrir
Set myrcs = New Recordset
myrcs.Open mysql, cnn
For m = 0 To myrcs.Fields.Count - 1
cbocampo.AddItem myrcs.Fields(m).Name
Next m
myrcs.Close
Exit Sub
gg:
MsgBox Err.Number & Err.Description
myrcs.Close
Exit Sub
End Sub
Public Function abrir()
On Local Error Resume Next
cnn.Close: Set cnn = Nothing
Set cnn = New Connection
On Local Error GoTo licen
cnn.Open ("provider = microsoft.jet.oledb.4.0;data source=" & datos)
Exit Function
licen:
MsgBox Err.Number & Err.Description
Exit Function
End Function
Private Sub Form_Unload(Cancel As Integer)
On Local Error Resume Next
cnn.Close: Set cnn = Nothing
rcs.Close: Set rcs = Nothing
myrcs.Close: Set myrcs = Nothing
End Sub