非綁定記錄窗體查看管理數(shù)據(jù) Access數(shù)據(jù)庫功能模塊講解 VBA代碼編程實例
模塊
Public 成績IDnum As Long
學生成績管理
Private Sub Command更新_Click()
DoCmd.SetWarnings (False)
Dim update_sql As String
update_sql = "Update 學生成績表 Set 考試日期=#" & 考試日期 & "#,姓名='" & 姓名 & "',科目='" & 科目 & "',分數(shù)=" & 分數(shù) & "? Where? 成績ID=" & 成績ID
DoCmd.RunSQL update_sql
MsgBox "更新成功"
End Sub
Private Sub Command刪除_Click()
If MsgBox("是否刪除該記錄", vbOKCancel) <> vbOK Then
Exit Sub
End If
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 學生成績表 Where? 成績ID= " & Me.成績ID
DoCmd.RunSQL del_sql
MsgBox "刪除成功"
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Close()
On Error Resume Next
Forms("學生成績列表").Form.數(shù)據(jù)表子窗體.Requery
End Sub
Private Sub Form_Load()
Dim search_rs As DAO.Recordset
Dim search_sql As String
search_sql = "Select * From 學生成績表 Where 成績ID= " & 成績IDnum
Set search_rs = CurrentDb.OpenRecordset(search_sql, dbOpenDynaset)
If search_rs.EOF = False Then
成績ID.Value = search_rs!成績ID.Value
考試日期.Value = search_rs!考試日期.Value
姓名.Value = search_rs!姓名.Value
科目.Value = search_rs!科目.Value
分數(shù).Value = search_rs!分數(shù).Value
End If
search_rs.Close
Set search_rs = Nothing
End Sub
學生成績列表
Private Sub Command清除_Click()
考試日期.Value = ""
姓名.Value = ""
科目.Value = ""
分數(shù).Value = ""
End Sub
Private Sub Command添加_Click()
On Error GoTo 添加失敗錯誤
If 考試日期 = "" Or IsNull(考試日期) = True Then
MsgBox "考試日期值為空!"
Exit Sub
End If
If 姓名 = "" Or IsNull(姓名) = True Then
MsgBox "姓名值為空!"
Exit Sub
End If
If 科目 = "" Or IsNull(科目) = True Then
MsgBox "科目值為空!"
Exit Sub
End If
If 分數(shù) = "" Or IsNull(分數(shù)) = True Then
MsgBox "分數(shù)值為空!"
Exit Sub
End If
'================================================================
Dim add_rs As DAO.Recordset
Set add_rs = CurrentDb.OpenRecordset("學生成績表", dbOpenTable)
With add_rs
.AddNew
!考試日期.Value = 考試日期.Value
!姓名.Value = 姓名.Value
!科目.Value = 科目.Value
!分數(shù).Value = 分數(shù).Value
.Update
.Close
End With
Set add_rs = Nothing
?'================================================================
MsgBox "添加成功!"
Me.數(shù)據(jù)表子窗體.Requery
Exit Sub
添加失敗錯誤:
MsgBox Err.Description
End Sub
學生成績數(shù)據(jù)表
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo 數(shù)據(jù)更新前提醒_Err
? ? If (MsgBox("是否保存對記錄的修改", 1, "修改記錄提醒") = 1) Then
? ? Else
? ? ? ? DoCmd.RunCommand acCmdUndo
? ? End If
數(shù)據(jù)更新前提醒_Exit:
? ? Exit Sub
數(shù)據(jù)更新前提醒_Err:
? ? MsgBox Error$
? ? Resume 數(shù)據(jù)更新前提醒_Exit
End Sub
Private Sub 成績ID_DblClick(Cancel As Integer)
成績IDnum = Me.成績ID
DoCmd.OpenForm "學生成績管理", acNormal
End Sub