access数据库前后端分离技术:前端修改更新后端数据

360影视 2025-02-01 22:53 2

摘要:update_rs.Open update_sql, update_conn, adOpenKeyset, adLockOptimistic

获得要查询的员工信息

修改该员工数据,点击更新按钮

更新完成

后端该员工指定数据已更新成功

Private Sub Command更新_Click

' 错误处理,跳转到“更新失败错误”标签处

On Error GoTo 更新失败错误

' 检查“员工号”字段是否为空或为Null

If 员工号 = "" Or IsNull(员工号) = True Then

MsgBox "员工号值为空!" ' 提示用户输入员工号

Exit Sub ' 退出子程序

End If

' 检查“姓名”字段是否为空或为Null

If 姓名 = "" Or IsNull(姓名) = True Then

MsgBox "姓名值为空!" ' 提示用户输入姓名

Exit Sub ' 退出子程序

End If

' 检查“性别”字段是否为空或为Null

If 性别 = "" Or IsNull(性别) = True Then

MsgBox "性别值为空!" ' 提示用户输入性别

Exit Sub ' 退出子程序

End If

' 检查“部门”字段是否为空或为Null

If 部门 = "" Or IsNull(部门) = True Then

MsgBox "部门值为空!" ' 提示用户输入部门

Exit Sub ' 退出子程序

End If

' 检查“职位”字段是否为空或为Null

If 职位 = "" Or IsNull(职位) = True Then

MsgBox "职位值为空!" ' 提示用户输入职位

Exit Sub ' 退出子程序

End If

' 检查“联系方式”字段是否为空或为Null

If 联系方式 = "" Or IsNull(联系方式) = True Then

MsgBox "联系方式值为空!" ' 提示用户输入联系方式

Exit Sub ' 退出子程序

End If

' 检查“工资”字段是否为空或为Null

If 工资 = "" Or IsNull(工资) = True Then

MsgBox "工资值为空!" ' 提示用户输入工资

Exit Sub ' 退出子程序

End If

' 定义数据库文件路径

Dim db_pathname As String

db_pathname = "C:\Users\liang\Desktop\前后端示例\后端.accdb"

' 创建并打开数据库连接对象

Dim update_conn As New ADODB.Connection

Dim update_rs As New ADODB.Recordset

' 配置并打开数据库连接

With update_conn

.Provider = "microsoft.ace.oledb.12.0;Jet OLEDB:Database Password='aaa123'" ' 指定数据库提供程序以及密码

.ConnectionString = db_pathname ' 设置数据库路径

.Open ' 打开数据库连接

End With

' 定义SQL查询语句,用于查找匹配的记录

Dim update_sql As String

update_sql = "Select * From 员工表 Where " & "员工号='" & Me.员工号 & "'"

' 打开记录集,用于编辑

update_rs.Open update_sql, update_conn, adOpenKeyset, adLockOptimistic

' 修改记录集中的字段值

With update_rs

!员工号.Value = 员工号.Value ' 更新员工号

!姓名.Value = 姓名.Value ' 更新姓名

!性别.Value = 性别.Value ' 更新性别

!部门.Value = 部门.Value ' 更新部门

!职位.Value = 职位.Value ' 更新职位

!联系方式.Value = 联系方式.Value ' 更新联系方式

!工资.Value = 工资.Value ' 更新工资

End With

' 保存对记录集的更改

update_rs.Update

' 关闭记录集

update_rs.Close

' 释放记录集对象

Set update_rs = Nothing

' 关闭数据库连接

update_conn.Close

' 释放数据库连接对象

Set update_conn = Nothing

' 提示用户更新完成

MsgBox "更新完成!"

Exit Sub ' 退出子程序

更新失败错误:

' 错误处理,显示错误信息

MsgBox Err.Description

End Sub

来源:八哥科技坊

相关推荐