唉,发了几个论坛,都没有答案,今天还是自己七拼八凑做出来了。 '首先引用:microsoft activeX data objects 2.X library 和 microsoft ado ext.2.x for ddl and security" '代码: Sub excelba() Dim BakCat As New ADOX.catalog '定义ADOX的Catalog对象变量 Dim BakCmd As New ADODB.Command '定义Command对象变量 Dim BakData As String '备份数据库名称 Dim OldData As String '原来数据库名称 Dim BakTable As String '数据表名称 Dim sSql As String 'SQL语句 Dim Conn As New ADODB.Connection '定义connection对象变量
'设置要备份的数据库名称(包括完整路径) BakData = ThisWorkbook.Path & "\学生成绩管理备份.mdb"
'如果已经存在,则删除原来的数据库文件 On Error Resume Next Kill BakData On Error GoTo 0
'创建数据库文件 BakCat.create "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & BakData Set BakCat = Nothing
'弹出信息 MsgBox "创建数据库成功!" & vbCrLf & "数据库文件名为:" & BakData & _ vbCrLf & "保存位置:" & ThisWorkbook.Path, , "提示" '设置数据库名称(包括完整路径) OldData = ThisWorkbook.Path & "\学生成绩管理.mdb" '设置要创建的数据表名称 BakTable = "期末成绩"
'建立与建数据库的连接 With Conn .Provider = "Microsoft.Jet.OLEDB.4.0" .Open OldData End With
'设置创建数据表的SQL语句 sSql = "Select * Into [;DataBase=" & BakData & "]." & BakTable & _ " From ( Select * From " & BakTable & " Where " & BakTable & ".[班级] = '一班')"
'利用Execute方法执行SQL语句 Conn.Execute sSql
'弹出信息 MsgBox "数据库备份成功!", , "提示"
'设置删除数据的SQL语句 sSql = "Delete From " & BakTable & " Where [班级] = '一班'"
'利用Execute方法执行SQL语句 Conn.Execute sSql
'弹出信息 MsgBox "数据删除成功!", , "提示"
'关闭数据库连接,并释放变量 Conn.Close Set Conn = Nothing End Sub |