为了将 MyData 控件变成一个数据源,您需要添加一些代码来处理到数据的连接和在记录中移动。您也需要显露许多的属性,以允许开发者在设计时使用控件选择一个数据源。
注意 该主题是帮助您创建示例数据源部件系列主题的一部分。创建数据源是第一部分。
要添加数据处理代码到 MyData 控件,请按照以下步骤执行:
'
只读Public Property Get RecordSet() As ADODB.RecordSet
Set RecordSet = rs
End Property
Public Property Get RecordSource() As String
RecordSource = m_RecordSource
End Property
Public Property Let RecordSource(ByVal New_RecordSource As String)
m_RecordSource = New_RecordSource
End Property
Public Property Get BOFAction() As BOFActionType
BOFAction = m_BOFAction
End Property
Public Property Let BOFAction(ByVal New_BOFAction As BOFActionType)
m_BOFAction = New_BOFAction
End Property
Public Property Get EOFAction() As EOFActionType
EOFAction = m_EOFAction
End Property
Public Property Let EOFAction(ByVal New_EOFAction As EOFActionType)
m_EOFAction = New_EOFAction
End Property
Public Property Get ConnectionString() As String
ConnectionString = m_ConnectionString
End Property
Public Property Let ConnectionString(ByVal New_ConnectionString _
As String)
m_ConnectionString = New_ConnectionString
End Property
Private Sub cmdFirst_Click()
If rs Is Nothing Then Exit Sub
rs.MoveFirst
End Sub
Private Sub cmdLast_Click()
If rs Is Nothing Then Exit Sub
rs.MoveLast
End Sub
Private Sub cmdPrev_Click()
If rs Is Nothing Then Exit Sub
If rs.BOF Then
Select Case m_BOFAction
Case BOFActionType.adDoMoveFirst
rs.MoveFirst
Case BOFActionType.adStayBOF
Exit Sub
Case Else
Exit Sub
End Select
Else
rs.MovePrevious
End If
End Sub
Private Sub cmdNext_Click()
If rs Is Nothing Then Exit Sub
If rs.EOF Then
Select Case m_EOFAction
Case EOFActionType.adDoAddNew
rs.AddNew
Case EOFActionType.adDoMoveLast
rs.MoveLast
Case EOFActionType.adStayEOF
Exit Sub
Case Else
Exit Sub
End Select
Else
rs.MoveNext
End If
End Sub
Private Sub UserControl_Terminate()
On Error Resume Next
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
If Not cn Is Nothing Then
cn.Close
Set cn = Nothing
End If
Err.Clear
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag) '
将属性值写到存储中Call PropBag.WriteProperty("Caption", _
lblCaption.Caption, Ambient.DisplayName)
Call PropBag.WriteProperty("RecordSource", _
m_RecordSource, m_def_RecordSource)
Call PropBag.WriteProperty("BOFAction", _
m_BOFAction, m_def_BOFAction)
Call PropBag.WriteProperty("EOFAction", _
m_EOFAction, m_def_EOFAction)
Call PropBag.WriteProperty("ConnectionString", _
m_ConnectionString, m_def_ConnectionString)
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag) '
从存储中加载属性值lblCaption.Caption = PropBag.ReadProperty("Caption", _
Ambient.DisplayName)
m_RecordSource = PropBag.ReadProperty("RecordSource", _
m_def_RecordSource)
m_BOFAction = PropBag.ReadProperty("BOFAction", m_def_BOFAction)
m_EOFAction = PropBag.ReadProperty("EOFAction", m_def_EOFAction)
m_ConnectionString = PropBag.ReadProperty("ConnectionString", _
m_def_ConnectionString)
End Sub
Private Sub UserControl_GetDataMember(DataMember As String, _ Data As Object) Dim conn As String On Error GoTo GetDataMemberError If rs Is Nothing Or cn Is Nothing Then '
确保各种属性已被设置If Trim$(m_ConnectionString) = "" Then
MsgBox "No ConnectionString Specified!", _
vbInformation, Ambient.DisplayName
Exit Sub
End If
If Trim$(m_RecordSource) = "" Then
MsgBox "No RecordSource Specified!", _
vbInformation, Ambient.DisplayName
Exit Sub
End If
If Trim$(m_ConnectionString) <> "" Then
'
创建一个Connection
对象并建立'
一个连接。Set cn = New ADODB.Connection
cn.ConnectionString = m_ConnectionString
cn.Open
'
创建一个RecordSet
对象。Set rs = New ADODB.RecordSet
rs.Open m_RecordSource, cn, adOpenKeyset, adLockPessimistic
rs.MoveFirst
Else
Set cn = Nothing
Set rs = Nothing
End If
End If
Set Data = rs
Exit Sub
GetDataMemberError:
MsgBox "Error: " & CStr(Err.Number) & vbCrLf & vbCrLf & _
Err.Description, vbOKOnly, Ambient.DisplayName
Exit Sub
End Sub
在下一步中将运行我们的工程查看结果。
该主题是帮助您创建 ActiveX 数据源系列主题的一部分。
要 | 请参阅 |
到下一步骤 | 运行 MyDataControl 工程 |
从头开始 | 创建数据源 |