【VB开源代码栏目提醒】:网学会员为广大网友收集整理了,Ex12_2.frm,希望对大家有所帮助!
VERSION 5.00
Begin VB.Form Ex12_2
Caption = "DAO对象的使用"
ClientHeight = 2565
ClientLeft = 60
ClientTop = 450
ClientWidth = 4350
LinkTopic = "Form1"
ScaleHeight = 2565
ScaleWidth = 4350
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command8
Caption = "尾记录"
Height = 375
Left = 3000
TabIndex = 13
Top = 2040
Width = 855
End
Begin VB.CommandButton Command7
Caption = "后一条"
Height = 375
Left = 2040
TabIndex = 12
Top = 2040
Width = 855
End
Begin VB.CommandButton Command6
Caption = "前一条"
Height = 375
Left = 1080
TabIndex = 11
Top = 2040
Width = 855
End
Begin VB.CommandButton Command5
Caption = "首记录"
Height = 375
Left = 120
TabIndex = 10
Top = 2040
Width = 855
End
Begin VB.CommandButton Command1
Caption = "添加"
Height = 375
Left = 3000
TabIndex = 9
Top = 120
Width = 855
End
Begin VB.CommandButton Command2
Caption = "删除"
Height = 375
Left = 3000
TabIndex = 8
Top = 600
Width = 855
End
Begin VB.CommandButton Command3
Caption = "修改"
Height = 375
Left = 3000
TabIndex = 7
Top = 1080
Width = 855
End
Begin VB.CommandButton Command4
Caption = "保存"
Height = 375
Left = 3000
TabIndex = 6
Top = 1560
Width = 855
End
Begin
VB.TextBox Text1
Height = 375
Left = 1200
TabIndex = 2
Top = 240
Width = 1575
End
Begin VB.TextBox Text2
Height = 375
Left = 1200
TabIndex = 1
Top = 840
Width = 1575
End
Begin VB.TextBox Text3
Height = 375
Left = 1200
TabIndex = 0
Top = 1440
Width = 1575
End
Begin VB.Label Label1
Caption = "定单号:"
Height = 255
Left = 120
TabIndex = 5
Top = 360
Width = 855
End
Begin VB.Label Label2
Caption = "客户号:"
Height = 255
Left = 120
TabIndex = 4
Top = 960
Width = 855
End
Begin VB.Label Label3
Caption = "订购日期:"
Height = 255
Left = 120
TabIndex = 3
Top = 1560
Width = 975
End
End
Attribute VB_Name = "Ex12_2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private myWorkspace As DAO.Workspace
Private myDatabase As DAO.Database
Private myRecord As DAO.Recordset
'显示数据
Private Sub ShowData()
On Error Resume Next
'定单号
Text1.Text = myRecord.Fields("orderID").Value
'客户号
Text2.Text = myRecord.Fields("clientID").Value
'定单日期
Text3.Text = myRecord.Fields("data").Value
End Sub
Private Sub Form_Load()
'新建
工作空间
Set myWorkspace = CreateWorkspace("myWorkspace", "Admin", "", dbUseJet)
'连接数据库
Set myDatabase = myWorkspace.OpenDatabase(App.Path & "\dbbook.mdb", , ";")
'构造SQL语句
Dim strSQL As String
strSQL = "Oders"
'获取recordset
Set myRecord = myDatabase.OpenRecordset(strSQL)
'显示数据
ShowData
End Sub
'添加按钮单击事件,表示增加记录
Private Sub Command1_Click()
'调用RecordSet对象的AddNew方法
myRecord.AddNew
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
'删除按钮单击事件
Private Sub Command2_Click()
'调用RecordSet对象的Delete方法删除数据
myRecord.Delete
'显示数据
ShowData
End Sub
'修改按钮单击事件
Private Sub Command3_Click()
'调用Recordset对象的Edit方法修改数据
'注意,DAO需要调用Recordset的Edit方法表示开始修改数据,与ADO不同
myRecord.Edit
myRecord.Update
End Sub
'保存按钮单击事件
Private Sub Command4_Click()
'保存数据,这里需要通过编程来实现
'保存订单号
myRecord.Fields("orderID").Value = Val(Text1.Text)
'保存客户号
myRecord.Fields("clientID").Value = Text2.Text
'保存定单日期
myRecord.Fields("data").Value = Val(Text3.Text)
'调用Recordset对象Update方法保存数据
myRecord.Update
'显示数据
ShowData
End Sub
'首记录按钮单击事件
Private Sub Command5_Click()
'调用MoveFirst移动
myRecord.MoveFirst
'显示记录
ShowData
End Sub
'前一条按钮单击事件
Private Sub Command6_Click()
'判断是否超出头记录
'若超出头记录,再执行MovePrevious会出错
If Not myRecord.BOF Then
myRecord.MovePrevious
Else
'超出头记录
'使用MoveFirst恢复到指向第一条记录
myRecord.MoveFirst
End If
'显示数据
ShowData
End Sub
'后一条按钮单击事件
Private Sub Command7_Click()
'移动记录到下一条的方法是MoveNext
'在调用MoveNext方法之前,首先需要判断,当前是不是已经超出尾纪录
'若超出尾记录,再执行MoveNext会出错
If Not myRecord.EOF Then
myRecord.MoveNext
Else
'超出尾记录,使用MoveLast恢复指向尾记录
myRecord.MoveLast
End If
'显示数据
ShowData
End Sub
'尾记录按钮单击事件
Private Sub Command8_Click()
'调用MoveLast
myRecord.MoveLast
'显示记录
ShowData
End Sub