Option Explicit
Private goConexion As ADODB.Connection
Private Const gsCadenaConexion = "PROVIDER=SQLOLEDB;DATA SOURCE=(local);DATABASE=BaseDeDatos;UID=sa;PWD="
Private Sub Form_Load()
msCargarLista
End Sub
Public Property Get goBD() As Connection
Dim lsCadenaConexion As String
lsCadenaConexion = gsCadenaConexion
If goConexion Is Nothing Then
Set goConexion = New ADODB.Connection
goConexion.Open lsCadenaConexion
Else
If goConexion.State = 0 Then goConexion.Open lsCadenaConexion
End If
Set goBD = goConexion
End Property
Private Sub msCargarLista()
Dim loItem As ListBox
Dim lsSQL As String
Dim loRS As Recordset
Lista.Clear
lsSQL = "SELECT * FROM Empresas"
Set loRS = goBD.Execute(lsSQL)
While Not loRS.EOF
Lista.AddItem IIf(Not IsNull(loRS!sNombre), loRS!sNombre, "")
Lista.ItemData(Lista.NewIndex) = IIf(Not IsNull(loRS!nCodigo), loRS!nCodigo, 0)
loRS.MoveNext
Wend
loRS.Close
Set loRS = Nothing
End Sub
Private Sub Lista_Click()
Dim lidCodigo As Long
Dim lsSQL As String
Dim loRS As Recordset
Text1.Text = ""
If Lista.ListIndex <> -1 Then
lidCodigo = Lista.ItemData(Lista.ListIndex)
lsSQL = "SELECT * FROM Empresas WHERE nCodigo = " & lidCodigo
Set loRS = goBD.Execute(lsSQL)
If Not loRS.EOF Then
Text1.Text = IIf(Not IsNull(loRS!sDireccion), loRS!sDireccion, "")
End If
loRS.Close
Set loRS = Nothing
End If
End Sub