Option Explicit
Private goConexion As ADODB.Connection
Public Property Get goBD() As Connection
Dim lsCadenaConexion As String
lsCadenaConexion = "Driver={Microsoft Access Driver (*.mdb)};"
lsCadenaConexion = lsCadenaConexion & "Dbq=" & App.Path & "\BaseDeDatos.mdb;"
lsCadenaConexion = lsCadenaConexion & "Uid=Admin; Pwd="
If goConexion Is Nothing Then
Set goConexion = New ADODB.Connection
goConexion.Open lsCadenaConexion
goConexion.CommandTimeout = 30
Else
If goConexion.State = 0 Then goConexion.Open lsCadenaConexion
End If
Set goBD = goConexion
End Property
Private Sub Command1_Click()
If Trim(lblEtiqueta.Caption) <> "" Then
msGenerarConsulta 'Aquí es dónde llamaríamos a que consulte en la BD
End If
End Sub
Private Sub msGenerarConsulta()
Dim lsSQL As String
Dim loRS As ADODB.Recordset
text1.Text = ""
text2.Text = ""
text3.Text = ""
text4.Text = ""
text5.Text = ""
lsSQL = "SELECT * FROM Tabla "
lsSQL = lsSQL & " WHERE Campo = '" & lblEtiqueta.Caption & "'"
Set loRS = New ADODB.Recordset
loRS.Open lsSQL, goBD, adOpenKeyset, adLockOptimistic
If Not loRS.EOF Then
text1.Text = IIf(Not IsNull(loRS!Campo1), loRS!Campo1, "")
text2.Text = IIf(Not IsNull(loRS!Campo2), loRS!Campo2, "")
text3.Text = IIf(Not IsNull(loRS!Campo3), loRS!Campo3, "")
text4.Text = IIf(Not IsNull(loRS!Campo4), loRS!Campo4, "")
text5.Text = IIf(Not IsNull(loRS!Campo5), loRS!Campo5, "")
End If
If loRS.State = 1 Then loRS.Close
Set loRS = Nothing
End Sub