by Ing Milton Lazaro (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 4th March 2002
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
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
' must be 4 commandButton (cmdPrimero, cmdAnterior, cmdSiguiente, cmdUltimo)
' 6 textBox (txtfldIdMatricula, txtfldNombre, txtfldApePaterno, txtfldApeMaterno, txtfldEdad, txtfldSem, txtfldMail)
Option Explicit
Dim clsEstudiante As Estudiante
Dim bndEnlace As BindingCollection
Private Sub cmdAnterior_Click()
clsEstudiante.MoverAnterior
End Sub
Private Sub cmdPrimero_Click()
clsEstudiante.MoverPrimero
End Sub
Private Sub cmdSiguiente_Click()
clsEstudiante.MoverSiguiente
End Sub
Private Sub cmdUltimo_Click()
clsEstudiante.MoverUltimo
End Sub
Private Sub Form_Load()
cmdPrimero.Caption = "| <"
cmdAnterior.Caption = "<"
cmdSiguiente.Caption = ">"
cmdUltimo.Caption = "> |"
Set clsEstudiante = New Estudiante
Set bndEnlace = New BindingCollection
Set bndEnlace.DataSource = clsEstudiante
With bndEnlace
.Add Me.txtfldIdMatricula, "Text", "fldIdMatricula"
.Add Me.txtfldNombre, "Text", "fldNombre"
.Add Me.txtfldApePaterno, "Text", "fldApePaterno"
.Add Me.txtfldApeMaterno, "Text", "fldApeMaterno"
.Add Me.txtfldEdad, "Text", "fldEdad"
.Add Me.txtfldSem, "Text", "fldSem"
.Add Me.txtfldMail, "Text", "fldMail"
End With
End Sub
No comments have been posted about Link controls with SQL database fields using a class and dataBindingCollection with ADO.. Why not be the first to post a comment about Link controls with SQL database fields using a class and dataBindingCollection with ADO..