Link controls with SQL database fields using a class and dataBindingCollection with ADO.
API Declarations
' Microsoft ActiveX DataObject 2.0
' Microsoft DataBinding Collection
' ===== put this code in a classModule =====
' --- with class' properties ---
' className = Estudiante; dataBlindingBehavior = 0-vbNone; dataSourceBehavior = 1-vbDataSource
Option Explicit
' connexiOn y tabla activas
Private cnnMyConexion As ADODB.Connection
Private rstStudent As ADODB.Recordset
' propiedades de la clase
Private mvarEndOfFile As Boolean
Private mvarBeginOfFile As Boolean
' ---- comienzo de las propiedades ----
Public Property Let BeginOfFile(ByVal vData As Boolean)
mvarBeginOfFile = vData
End Property
Public Property Get BeginOfFile() As Boolean
BeginOfFile = mvarBeginOfFile
End Property
Public Property Let EndOfFile(ByVal vData As Boolean)
mvarEndOfFile = vData
End Property
Public Property Get EndOfFile() As Boolean
EndOfFile = mvarEndOfFile
End Property
' ---- fin de las propiedades ----
Private Sub Class_GetDataMember(DataMember As String, Data As Object)
Set Data = rstStudent
End Sub
Private Sub Class_Initialize()
Dim strCadenaConexion As String
Dim strSQL As String
DataMembers.Add "dmbEstudiante"
Set cnnMyConexion = New ADODB.Connection
' definir la cadena de conexiOn
strCadenaConexion = ""
strCadenaConexion = strCadenaConexion & "Provider=SQLOLEDB.1;"
strCadenaConexion = strCadenaConexion & "Persist Security Info=False;"
strCadenaConexion = strCadenaConexion & "User ID=sa;"
strCadenaConexion = strCadenaConexion & "Initial Catalog=student_db;"
strCadenaConexion = strCadenaConexion & "Data Source=MLAZARO"
cnnMyConexion.ConnectionString = strCadenaConexion
cnnMyConexion.Open
Set rstStudent = New ADODB.Recordset
strSQL = ""
strSQL = strSQL & "SELECT fldIdMatricula, fldNombre, fldApePaterno, fldApeMaterno, fldEdad, fldSem, fldMail "
strSQL = strSQL & "FROM tblStudent"
Set rstStudent.ActiveConnection = cnnMyConexion
With rstStudent
.LockType = adLockOptimistic
.CursorType = adOpenDynamic
.Source = strSQL
.Open , , , , adCmdText
Me.BeginOfFile = .BOF
Me.EndOfFile = .EOF
End With
End Sub
Public Sub MostrarDatos()
MsgBox rstStudent!fldIdMatricula & " - " & rstStudent!fldNombre, vbInformation
End Sub
Public Sub MoverPrimero()
rstStudent.MoveFirst
Me.BeginOfFile = True
End Sub
Public Sub MoverAnterior()
rstStudent.MovePrevious
If rstStudent.BOF Then
rstStudent.MoveFirst
Exit Sub
End If
Me.BeginOfFile = rstStudent.BOF
End Sub
Public Sub MoverSiguiente()
rstStudent.MoveNext
If rstStudent.EOF Then
rstStudent.MoveLast
Exit Sub
End If
Me.EndOfFile = rstStudent.EOF
End Sub
Public Sub MoverUltimo()
rstStudent.MoveLast
Me.EndOfFile = True
End Sub