我正在运行几个宏,这些宏在Excel中使用DAO
执行对MS Access的读写命令。我当前的文件类型是.MDB
,在来回读写MS Access 2周后遇到数据库损坏。我以前引用过Microsoft DAO 3.6 Object Library
,但切换到Microsoft Office 16.0 Access database engine Object Library
。我的数据库损坏是由于文件类型和连接类型引起的,还是仅引用了错误的库?我提供的代码用于从MS Access上传和检索数据。
从MS Access向Excel加载表
Sub loadData()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Set db = OpenDatabase("Z:\UBPB CRM Project\pbsbackup.mdb")
Set qdf = db.QueryDefs("pbsload")
qdf!pbsbranch = initialLogin.ComboBox1
Set rst = qdf.OpenRecordset
If rst.EOF Then
rst.Close
End If
Do
Sheet3.Range("A8").CopyFromRecordset rst
Loop Until rst.EOF
rst.Close
Set rst = Nothing
End Sub
将记录从Excel用户窗体添加到MS Access
Sub AddnewRecord()
Dim db As Database
Dim qdf As QueryDef
Application.StatusBar = "Connecting to PBS Database......"
Set db = OpenDatabase("Z:\UBPB CRM Project\pbsbackup.mdb")
Set qdf = db.QueryDefs("addclient")
Application.CalculateUntilAsyncQueriesDone
Application.StatusBar = "Uploading Client Data to PBS server...."
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
qdf!pbsbranch = Sheet4.Range("A2")
qdf!pbsclient = addnewClient.client
qdf!pbspriority = addnewClient.priority_
qdf!pbscontact = addnewClient.contact
qdf!pbsresult = addnewClient.result
qdf!pbsnextsteps = addnewClient.segmentType
qdf!pbsattempts = addnewClient.Label11
qdf!pbsnotes = addnewClient.notes
qdf.Execute dbFailOnError
qdf.Close
Application.StatusBar = "Upload Successful!"
Set qdf = Nothing
Set cdb = Nothing
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
将记录从Excel用户窗体更新为MS Access
Sub pbsupdate()
Dim db As Database
Dim qdf As QueryDef
Application.StatusBar = "Connecting to PBS Database......"
Set db = OpenDatabase("Z:\UBPB CRM Project\pbsbackup.mdb")
Set qdf = db.QueryDefs("pbsupdate")
Application.StatusBar = "Uploading Client Data to PBS server...."
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
qdf!pbskey = dialer.key
qdf!pbsclient = dialer.client
qdf!pbspriority = dialer.priority_
qdf!pbssource = dialer.priority
qdf!pbslastcontact = dialer.contact
qdf!pbsresult = dialer.result
qdf!pbsnextsteps = dialer.segmentType
qdf!pbsattempts = dialer.Label11 + dialer.Label11 + 1
qdf!pbsnotes = dialer.notes
Application.CalculateUntilAsyncQueriesDone
qdf.Execute dbFailOnError
qdf.Close
db.Close
Application.StatusBar = "Upload Successful!"
Set qdf = Nothing
Set cdb = Nothing
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub