|
|||||||
|
|
|
|||||
|
|
|||||||
[Sample Mission]
This sample demonstrates the usage of the Data control. You
can build up your application including Data control expediently and quickly.
[Usage]
[Quick Use]
Get the current status by the public variable 'DataStatus'.Usually,the public variable 'DataStatus' is indicated as below:
1 - Browse Data
2 - AddNew Data
3 - No current record to Browse and no AddNew
[NOTES]
This sample doesn't support OLE field!
[Other Variations]
[Codes as following]
''''''''''''''''''''''''''''
''' Usage of This Sample '''
''' '''
''''''''''''''''''''''''''''
'(please put these comments at the entry module or form of the application!)
'[Comment Specification]
'
' A. Lines
' 'This is the comment of all below lines without space-line between each any:
' Unload Me
' End
'
' B. Block (means that lines with or without space-line between each any)
' ''''''''''''''''''''
' ''' <block name> '''
' ''' '''
' ''''''''''''''''''''
' <code lines/block>
'
' C. Considering More Flexiblities
' '// <code line>
'
' D. Replacable Code Block or Lines
' 'RP#0: <description of this replacement>
' '{ <descriptions>
' <current code lines/block>
' '{ <descriptions>
' '<replacement code lines/block>
' '} this replacement refers to #1 to #n. search "RP#n".
'
' E. End Of A Line
' <code line> 'comment for only this line
'
'[Sample Mission]
'This sample demonstrates the usage of the Data control. You can
' build up your application including Data control expediently and quickly.
'
'[Customize]
'1. Data Control
'After adding a Data control, set below propertities:
' 1 - Connect
' 2 - DatabaseName
' 3 - RecordSource
'
'2. Text controls banded with the Data control
'Add Text controls to band with Data control, then set their properties:
' 1 - DataSource set to "Data1" which is the name of the Data control
' 2 - DataField 'select a field to band with
'
'Edit codes where at "txtFields"
'
'[Quick Use]
'1. Get the current status by the public variable 'DataStatus'
'Usually,the public variable 'DataStatus' is indicated as below:
' 1 - Browse Data
' 2 - AddNew Data
' 3 - No current record to Browse and no AddNew
'
'[NOTES]
'This sample doesn't support OLE field!
'
'[Other Variations]
'1. Show where in the database when starting, refer to where at #SHOW
'2. User can add some event process codes where at #EVENT
'
'[See Also]
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Public DataStatus As Integer
Private Sub Form_Load()
On Error GoTo Err_FormLoad
Me.Show
'Open database banded with Data1 control
Data1.Refresh 'if no, Data1.Recordset will not be accessed!
'To force the last record to be accessed,thus will can
'use the RecordCount property to find out how many records
If Data1.Recordset.RecordCount > 0 Then
Data1.Recordset.MoveLast
End If
'#SHOW : Show the last record if comment both below two lines.
'\\ Data1.Recordset.MoveFirst 'Show the first record when enter
Data1.Recordset.AddNew 'Show AddNew when enter
Exit Sub
Err_FormLoad:
MsgBox Err.Description
Unload Me
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
'''''''''''''''''''''
''' Button Events '''
''' '''
'''''''''''''''''''''
Private Sub cmdAdd_Click()
On Error GoTo Err_AddNew
Data1.Recordset.AddNew
Exit Sub
Err_AddNew:
MsgBox Err.Description
End Sub
Private Sub cmdUpdate_Click()
On Error GoTo Err_Update
Dim old_status As Integer
old_status = DataStatus: DataStatus = 1 'Browse Data
Data1.UpdateRecord
'The record that was current before you used AddNew remains current!
'If you want to make the new record current, you can set the Bookmark
'property to the bookmark identified by the LastModified property setting
On Error GoTo Err_BookMark
Data1.Recordset.Bookmark = Data1.Recordset.LastModified
Exit Sub
Err_Update:
DataStatus = old_status
Err_BookMark:
MsgBox Err.Description
End Sub
Private Sub cmdCancel_Click()
On Error GoTo Err_Cancel
Data1.UpdateControls
'Set focus to the first field
On Error Resume Next 'error occurs when it is not Visible or enabled
txtFields(0).SetFocus
Exit Sub
Err_Cancel:
MsgBox Err.Description
End Sub
Private Sub cmdDelete_Click()
On Error Resume Next 'Error occurs if delete where only one record in table!
'Also when no record in table
Data1.Recordset.Delete
Data1.Recordset.MoveNext
If Data1.Recordset.EOF Then Data1.Recordset.MoveLast 'Be TRUE When delete the last record
End Sub
Private Sub cmdRefresh_Click()
On Error GoTo Err_Refresh
Data1.Refresh
'To force the last record to be accessed,thus will can
'use the RecordCount property to find out how many records
If Data1.Recordset.RecordCount > 0 Then
Data1.Recordset.MoveLast
End If
DataStatus = 1 'Browse Data
Exit Sub
Err_Refresh:
MsgBox Err.Description
End Sub
''''''''''''''''''''''''''''''''''''''
''' Data Control Events
''''''''''''''''''''''''''''''''''''''
Private Sub Data1_Error(DataErr As Integer, Response As Integer)
MsgBox "For Debug: Data Error?" & Error$(DataErr)
Call cmdRefresh_Click 'force to refresh!
Response = 0 'ignore error
End Sub
Private Sub Data1_Reposition()
Dim i As Integer
'In order to optimize below codes, avoid running
'the same thing more than once
Static OldStatus As Integer
'Update Data1 control's status
If Data1.Recordset.RecordCount = 0 And DataStatus = 1 Then DataStatus = 3
'Update Controls according Data Status
If OldStatus <> DataStatus Then
OldStatus = DataStatus
Select Case DataStatus
Case 1 'Browse Data
For i = 0 To 4
txtFields(i).Enabled = True
Next
cmdUpdate.Enabled = True
cmdCancel.Enabled = True
cmdRefresh.Enabled = True
cmdDelete.Visible = True
Data1.Enabled = True
Case 2 'AddNew Data
For i = 0 To 4
txtFields(i).Enabled = True
Next
cmdUpdate.Enabled = True
cmdCancel.Enabled = True
cmdRefresh.Enabled = True
cmdDelete.Visible = False
Data1.Enabled = False
Case 3 'No current record to browse and no AddNew
For i = 0 To 4
txtFields(i).Enabled = False
Next
cmdUpdate.Enabled = False
cmdCancel.Enabled = False
cmdRefresh.Enabled = False
cmdDelete.Visible = False
Data1.Enabled = False
End Select
End If
'Update Controls All the Time
If DataStatus = 1 Then 'Browse Data
txtFields(0).SetFocus
Data1.Caption = "Current Record / Total Records: " & _
(Data1.Recordset.AbsolutePosition + 1) & "/" & Data1.Recordset.RecordCount
ElseIf DataStatus = 2 Then 'AddNew Data
txtFields(0).SetFocus
Data1.Caption = "New Record/Total Records: " & _
(Data1.Recordset.RecordCount + 1) & "/" & Data1.Recordset.RecordCount
ElseIf DataStatus = 3 Then 'No current record to browse and no AddNew
Data1.Caption = "No record! You must Add."
End If
'another expression to show total records number
'//Data1.Caption = "Record?" & (Data1.Recordset.RecordCount * (Data1.Recordset.PercentPosition * 0.01)) + 1
End Sub
Private Sub Data1_Validate(Action As Integer, Save As Integer)
'EVENT : This sub will be called when following events occur:
Select Case Action
Case vbDataActionMoveFirst
DataStatus = 1 'Browse Data
Case vbDataActionMovePrevious
DataStatus = 1 'Browse Data
Case vbDataActionMoveNext
DataStatus = 1 'Browse Data
Case vbDataActionMoveLast
DataStatus = 1 'Browse Data
Case vbDataActionAddNew
DataStatus = 2 'AddNew Data
Case vbDataActionUpdate
Case vbDataActionDelete
Case vbDataActionFind
Case vbDataActionBookmark
Case vbDataActionClose
Case vbDataActionUnload
End Select
End Sub
Copyright (c) 1999 - 2001, robert han, all rigths are reserved.