我有一些Excel VBA的經驗,但沒有一個使用Access。我在excel中創建了一個工具,它執行很多操作,並以帶有表格中數據的選項卡結束。多人將使用excel作爲數據輸入工具。我希望最終選項卡中的數據可以導出到Access,以便在輸入的人員之間彙總數據。爲確保不會造成混亂,我需要在提交到Access時檢查重複的ID。 ID變量稱爲ADP_Number,位於Excel和Access的第2列中。vba:導出Excel以訪問,刪除重複的ID
下面的代碼用於將數據提交給Access,但對ID沒有做任何處理。有人可以幫助我嗎?
Sub ExportToAccess_DAO()
Dim oSelect As Range, i As Long, j As Integer ', sPath As String
Dim ws As DAO.Workspace
Sheets("Program_List").Activate
Set oSelect = Application.InputBox("Range", , Range("A1").CurrentRegion.Address, , , , , 8)
Dim oDAO As DAO.DBEngine
Dim oDB As DAO.Database
Dim oRS As DAO.Recordset
ChDir ActiveWorkbook.Path
Set oDAO = New DAO.DBEngine
Set oDB = oDAO.OpenDatabase("I:\ALLSHARE\MAD_14.accdb", _
False, False, "MS Access;PWD=MAD_14")
Set oRS = oDB.OpenRecordset("Program") ' change name?
For i = 2 To oSelect.Rows.Count 'skips labels
oRS.AddNew 'addnew record to recordset
For j = 1 To oSelect.Columns.Count ' number of columns 'Field 0 is an auto# - like an ID, so we start at 1
oRS.Fields(j) = oSelect.Cells(i, j)
Next j
oRS.Update
Next i
oDB.Close
End Sub