点【关于本公众号】了解一下,欢迎关注,谢谢!
快速浏览
实用案例
|日期控件||简单的收发存||收费管理系(Access改进版)|
|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|
收费使用项目
|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|
内容提要
VBA操作ACCESS数据库「扩展篇」|完整代码
Private Sub CmdCreate_Click()
'//新建一个表:tb销售
' On Error Resume Next
If IsTableExists("tb销售") Then
MsgBox "已存在【tb销售】!"
Exit Sub
End If
sql = "Create table tb销售 " _
& "(ID AUTOINCREMENT primary key,日期 Date," _
& "销售单号 text(255),产品名称 text(255)," _
& "数量 double,单价 double,金额 double," _
& "业务员 text(255),备注 text(255))"
Call ExecuteSQL(sql)
MsgBox "Done!"
End Sub
Private Sub CmdCreate2_Click()
'//根据tb员工表建立另一个表
tbl = "tb员工bak"
If IsTableExists(tbl) Then
MsgBox "已存在【" & tbl & "】!"
Exit Sub
End If
sql = "select * into tb员工bak from tb员工"
'sql = "select * into tb员工bak from tb员工 where false"
'sql = "select 姓名,年龄 into tb员工bak from tb员工"
Call ExecuteSQL(sql)
MsgBox "创建成功!"
End Sub
Private Sub CmdDeleteTable_Click()
'//删除表
tbl = "tb员工bak"
If Not IsTableExists(tbl) Then
MsgBox "不存在【" & tbl & "】!"
Exit Sub
End If
sql = "DROP TABLE " & tbl
Call ExecuteSQL(sql)
MsgBox "删除成功!"
End Sub
Private Sub CmdAllTables_Click()
'//显示所有表
Dim arr(), i As Integer
Dim strMsg As String
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
arr = getAllTables(dbs)
For i = LBound(arr) To UBound(arr)
strMsg = strMsg & arr(i) & Chr(10)
Next
MsgBox "所有表名如下:" & Chr(10) & strMsg
End Sub
Private Sub CmdAlterTable1_Click()
'//把tb销售表中的“日期"类型修改为文本
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
tbl = "tb销售"
'//先检查一下“日期”是否存在
If Not IsFieldExists(dbs, tbl, "日期") Then
MsgBox "不存在字段【日期】!"
Exit Sub
End If
'// 日期修改为文本
sql = " ALTER TABLE tb销售 ALTER COLUMN 日期 TEXT(50) "
Call ExecuteSQL(sql)
MsgBox "成功修改!"
End Sub
Private Sub CmdAlterTable2_Click()
'//把tb销售表中的“日期"类型修改为日期
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
tbl = "tb销售"
'//先检查一下“日期”是否存在
If Not IsFieldExists(dbs, tbl, "日期") Then
MsgBox "不存在字段【日期】!"
Exit Sub
End If
'// 日期修改为日期类型
sql = " ALTER TABLE tb销售 ALTER COLUMN 日期 Date "
Call ExecuteSQL(sql)
MsgBox "成功修改!"
End Sub
Private Sub CmdAlterTable3_Click()
'//在tb销售表中添加“销售类型”字段
Dim fieldName As String
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
tbl = "tb销售"
fieldName = "销售类型"
'//先检查一下“销售类型”是否存在
If IsFieldExists(dbs, tbl, fieldName) Then
MsgBox "已存在字段【" & fieldName & "】!"
Exit Sub
End If
'// 添加字段
sql = " ALTER TABLE tb销售 ADD " & fieldName & " TEXT(10) "
Call ExecuteSQL(sql)
MsgBox "添加成功!"
End Sub
Private Sub CmdAlterTable4_Click()
'//在tb销售表中删除“销售类型”字段
Dim fieldName As String
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
tbl = "tb销售"
fieldName = "销售类型"
'//先检查一下“销售类型”是否存在
If Not IsFieldExists(dbs, tbl, fieldName) Then
MsgBox "不存在字段【" & fieldName & "】!"
Exit Sub
End If
'// 删除字段
sql = " ALTER TABLE tb销售 DROP " & fieldName
Call ExecuteSQL(sql)
MsgBox "删除成功!"
End Sub
Dim sql As String
Function IsTableExists(tableName As String) As Boolean
Dim blnFound As Boolean
dbs = ThisWorkbook.Path & "\DataBase1101.accdb"
'//打开数据库连接
Call OpenConnection(dbs)
'//查询表信息
Set rs = conn.OpenSchema(20)
blnFound = False
'//遍历表信息,检查是否存在指定的表名
Do Until rs.EOF
If rs!TABLE_NAME = tableName Then
blnFound = True
Exit Do
End If
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
IsTableExists = blnFound
End Function
Function getAllTables(dbs As String)
Dim arr(), i As Integer
'//打开数据库连接
Call OpenConnection(dbs)
'//查询表信息
Set rs = conn.OpenSchema(20)
Do Until rs.EOF
If rs("table_type") = "TABLE" Then
If Left(rs("table_name"), 1) <> "~" Then
ReDim Preserve arr(i)
arr(i) = rs("table_name")
i = i + 1
End If
End If
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
getAllTables = arr
End Function
Function IsFieldExists(dbs As String, tbl As String, fieldName As String) As Boolean
Dim blnFound As Boolean
Dim i As Integer
'//打开数据库连接
Call OpenConnection(dbs)
Set rs = CreateObject("ADODB.Recordset")
blnFound = False
sql = "select * from " & tbl & " where 1=0"
Set rs = conn.Execute(sql)
For i = 0 To rs.Fields.Count - 1
If rs.Fields(i).Name = fieldName Then
blnFound = True
Exit For
End If
Next
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
IsFieldExists = blnFound
End Function
安利小店 安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精、洗衣液也是日常必备,用过都说好! | |
合谷医疗 合谷医疗专攻各种疑难杂症,尤其擅长抑郁症、焦虑失眠、儿童神经发育异常、多动症、自闭孤独症、腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了! |
喜欢就点个赞、点在看、留言评论、分享一下呗!感谢支持!
Excel问题,请在文章下面留言讨论!或者加入我的付费交流群提问!
如需案例文件,请按当天另一篇文章末尾案例文件分享说明操作!