从DAO转换到ADO(2)

2008-02-23 06:56:12来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折



Option Explicit

注释: Private references to the ADO 2.1 Object Library
Private mCN As Connection
Private mRS As New Recordset

注释: Internal reference to the current records ID value
Private mintRcdID As Integer

Private Sub cmdAbout_Click()
frmAbout.Show vbModal
End Sub

Private Sub cmdAdd_Click()
AddRecord
End Sub

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub OpenConnection(strPath As String)

注释: Close an open connection
If Not (mCN Is Nothing) Then
mCN.Close
Set mCN = Nothing
End If


注释: Create a new connection
Set mCN = New Connection

With mCN
注释: To connect to a SQL Server, use the following line:

注释: .ConnectionString="driver=[SQL Server];uid=admin;server=mysrv;database=site"

注释: For this example, we will be connecting to a local database
.ConnectionString = "Provider=Microsoft.JET.OLEDB.3.51;Data Source=" & strPath

.CursorLocation = adUseClient
.Open

End With

End Sub

Private Sub AddRecord()


注释: Add a new record using the recordset object
注释: Could be done using the connection object
mRS.Open "SELECT * FROM People", mCN, adOpenKeyset, adLockOptimistic

With mRS

.AddNew
.Fields("Name").Value = txtName.Text
.Fields("URL").Value = txtURL.Text
.Fields("Notes").Value = txtNotes.Text

注释: After updating the recordset, we need to refresh it, and then move to the
注释: end to get the newest record. We can then retrieve the new record注释:s id
.Update
.Requery
.MoveLast

mintRcdID = .Fields("ID").Value

.Close

End With

End Sub

Private Sub DeleteRecord()

注释: Delete a record and clear the textboxes

mRS.Open "SELECT * FROM People WHERE ID =" & mintRcdID, mCN, adOpenKeyset, adLockOptimistic

mRS.Delete
mRS.Close

txtName.Text = ""
txtURL.Text = ""
txtNotes.Text = ""

End Sub

Private Sub GetInfo()

注释: Get the data for a record based on its ID value
mRS.Open "SELECT * FROM People WHERE ID =" &
mintRcdID, mCN, adOpenKeyset, adLockOptimistic

With mRS

txtName.Text = .Fields("Name").Value
txtURL.Text = .Fields("URL").Value
txtNotes.Text = .Fields("Notes").Value
.Close

End With

End Sub

Private Sub UpdateRecord()

注释: Update a record注释:s values
mRS.Open "SELECT * FROM People WHERE ID =" & mintRcdID, mCN, adOpenKeyset, adLockOptimistic

With mRS

.Fields("Name").Value = txtName.Text
.Fields("URL").Value = txtURL.Text
.Fields("Notes").Value = txtNotes.Text

.Update
.Close

End With

End Sub

Private Sub cmdDelete_Click()
DeleteRecord
End Sub

Private Sub cmdGet_Click()

注释: Ask the user which record should be retrieved and get the data
注释: for that record
mintRcdID = Val(InputBox$("Enter ID of record:", App.Title, "1"))

GetInfo

End Sub

Private Sub cmdSave_Click()
UpdateRecord
End Sub

Private Sub Form_Load()

OpenConnection App.Path & "\people.mdb"

End Sub

Private Sub Form_Unload(Cancel As Integer)

If Not (mRS Is Nothing) Then
Set mRS = Nothing
End If

If Not (mCN Is Nothing) Then
mCN.Close
Set mCN = Nothing
End If

End Sub

上一篇: Access97的报表解决方案
下一篇: 在VB中存取数据库中的图片

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:字符串中包含双引号

下一篇:VB 如何将DBgrid印出来