BICS 546
Intrinsic and ActiveX Controls
• VB supports two types of controls:
– Intrinsic controls (command button, listbox,
etc): When compile a program the code to call
the intrinsic controls becomes embedded in the
executable file.
– ActiveX controls: the code containing the
methods and events for an ActiveX control is
stored in a .OCX file and not stored in the
executable.
Intrinsic and ActiveX Controls
VB Program
Executable .EXE
Intrinsic Windows
Controls Library
BOF
Record # 1
Record # 2
Record #3
EOF
Loop through a Recordset
DO Until Recordset.EOF
‘Perform action on data
Recordset.MoveNext
Loop
ADODC1.Recordset.MoveNext
IF ADODC1.Recordset.EOF THEN
ADODC1.Recordset.MoveLast
End IF
ADODC1.Recordset.MovePrevious
IF ADODC1.Recordset.BOF THEN
ADODC1.Recordset.MoveFirst
End IF
Referring to Database Fields
• DataControName.Recordset.Fields(“fieldName”)
• DataControName.Recordset(“fieldName”)
• DataControName.Recordset!fieldName
ADO Control Recordset Update Methods
Adding records: ADODC1.Recordset.AddNew
(AddNew clears bound controls. The new record is added when
move to the next record or added by:
ADODC1.Recordset.Update (with a SaveNew button)
Deleting records (after deletion, move to the next)
ADODC1.Recordset.Delete
ADODC1.Recordset.MoveNext
IF ADODC1.Recordset.EOF THEN
ADODC1.Recordset.MoveLast
END IF
Updating record: The database is automatically updated if the
user moves to a new record. Otherwise it can be updated by:
ADODC1.Recordset.Update (with a SaveChange button)
Record set Find Method
• Syntax: RecordsetName.Find criteria
• Only accept simple comparison criteria.
• Example:
– Adodc1.recordset.Find “CID = ‘” & txtCID & “’”
• Finding employees hired between 2/15/2000 and 5/15/2000:
– Adodc1.recordset.Find “Hdate > #2/15/2000#”
Then use IF statement in program to filter out those hired after 5/15/2000
Find Code Example
Private Sub Form_Load()
Adodc1.Refresh
List1.Clear
Do Until Adodc1.Recordset.EOF
List1.AddItem Adodc1.Recordset.Fields("cname")
Adodc1.Recordset.MoveNext
Loop
End Sub
Demo
Accessing Recordset in ADO Data
Control and Data Environment
• Data Control
– Adodc1.Recordset.MoveNext
• Data Environment’s command object
– DataEnvironment1.rsCommand1.MoveNext
Events of ADO Data Control
• Error
• WillChangeField, WillChangeRecord,
WillChangeRecordSet, WillMove.
• FieldChangeComplete,
RecordChangeComplete,
RecordsetChangeComplete
• EndOfRecordSet
• Other mouse events
Parameters Passed to the Event
Handlers
• Error: ErrorNumber, Description, Scode, Source,
etc.
• WillChange: (Illustrate ADODB Enum)
– adReason: adRsnAddNew, adRsnDelete, adRsnUpdate
– adStatus: adStatusCancel,
adStatusCantDeny,adStatusOK
– etc.
• ChangeComplete: adReason, pError, adStatus
Data Validation with the ADO
Data Control
• WillChangeField, WillMove,
WillChangeRecord, WillChangeRecordset
• You can cancel the pending change if the
adStatus is not set to adStatusCantDeny by
changing the adStatus to adStatusCancel.
• The ADO control must be refreshed after an
event handler is fired using the Refresh
method.
WillChangeField Validation
Example
IF adStatus <> adStatusCantDeny THEN
IF MsgBox(“DO you want to update” & Fields(0), vbYesNO) = vbNo THEN
adStatus = adSTatusCancel
END IF
END IF
ADODC1.Refresh
WillChangeRecord Validation
Example
Errors Error
Option Explicit
Private cn As Connection
Private rs As Recordset
Private Sub Form_Load()
Set cn = New Connection
cn.Open ("provider=msdasql;dsn=Sales")
Set rs = cn.Execute("select * from customers", adCmdText)
Set Text1.DataSource = rs
Text1.DataField = "CustomerID"
Set Text2.DataSource = rs
Text2.DataField = "CompanyName“