1
我有以下Excel VBA代碼,直到我將Access(2007)數據庫轉換爲Access(2010)數據庫後才能正常工作。我已經使用Access(2010)中的一些新功能來改進我的數據庫,但現在我的Excel宏將無法運行。我收到以下消息。在Excel 2010中從Access 2010中的特定記錄讀取特定字段
運行時錯誤3343 Unreconized數據庫格式
我想修復可能是一個簡單的辦法,但我不是一個程序員,所以我在迷失....在此先感謝您的幫助 。
Sub Get_Hardware() 'Retrieves hardware weights from Access database
'********Note: If program fails to run, in the visual basic editor, under tools, references, you may
' need to have "Microsoft DAO 3.6 Object Library" downloaded**********
' ******* May also need to register DAO 3.6 if it does not apear in the selectable list by doing the following.
' 1. Open Window's start menu and select "Run"
' 2. Paste the following into the run windo and tell it to run it....
' regsvr32 "c:\program files\common files\microsoft shared\dao\dao360.dll"
Do While Not IsEmpty(ActiveCell.Offset(0, 0))
DAOCopyFromRecordSet "\\fil-ict-07\s0052491$\Engineering\Mass Properties Database (Access 2010)\Mass Properties Database (2010).accdb", "Hardware", "Part Number", ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
End Sub
___________________________________________________________________________________________
Public Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
FieldName As String, TargetRange As Range)
Dim db As Database, UW, MC, Des, WQ As Recordset
Dim intColIndex As Integer
Dim MatCode As String
Set TargetRange = TargetRange.Cells(1, 3)
Set db = OpenDatabase(DBFullName)
'SQL Query
Set UW = db.OpenRecordset("SELECT Std_Parts.UnitWeight FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & _
ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
Set MC = db.OpenRecordset("SELECT Std_Parts.Material_Code FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & _
ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
Set Des = db.OpenRecordset("SELECT Std_Parts.Description FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & _
ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
Set WQ = db.OpenRecordset("SELECT Std_Parts.Qual FROM Std_Parts WHERE (((Std_Parts.[Part Number]) = '" & _
ActiveCell.Offset(0, 0).Value & "'))", dbReadOnly)
' write recordset
TargetRange.CopyFromRecordset UW
Set TargetRange = TargetRange.Cells(1, 0)
TargetRange.CopyFromRecordset Des
Set TargetRange = TargetRange.Cells(1, 3)
TargetRange.CopyFromRecordset MC
Set TargetRange = TargetRange.Cells(1, 22)
TargetRange.CopyFromRecordset WQ
ActiveCell.Offset(0, 6).Select
ActiveCell.Formula = "=RC[-4]*RC[-1]"
GoOn:
ActiveCell.Offset(0, -6).Select
Set UW = Nothing
Set MC = Nothing
Set Des = Nothing
Set WQ = Nothing
db.Close
End Sub
如果我保留Microsoft Office 12.0 Access數據庫引擎對象庫,DAO命令是否可以工作? – 2012-08-07 20:55:02
他們應該這樣做,Microsoft Office 12.0 Access數據庫引擎對象是DAO庫的最新版本。我添加此參考時,您的代碼適用於我。 – Fionnuala 2012-08-07 21:09:40