[Giúp] VBA delete data từ Excel sang Access (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

robinhsoon

Thành viên hoạt động
Tham gia
19/1/16
Bài viết
153
Được thích
11
Chào cả nhà GPEX!

Em có một thắc mắc mong mọi người giúp đỡ em nhé..!
Hiện tại mình có thể Import data từ Excel sang Access, vậy mình có thể dùng VBA can thiệp để delete data từ Excel sang Access không ạ...
Chuyện là: Em có một Macro Import Data từ Excel vào Access nhưng trước đó trong Access đã có Data sẵng nên nhiều lúc Import vào bị trùng và bị báo lỗi, nên Em phải dùng một bước vào Access để xóa hết dữ liệu trong đó rồi mới Import vào...
Vậy em muốn viết một cái Macro xóa data trong Access trước rồi mới Inport vào được không ạ... Em mò trên hoài nên không ra...
Mong các Thầy giúp đỡ ...Em cảm ơn ạ!

'Lệnh Import
Function ImportDataToP1(FlagCheck As Boolean)
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long

Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection

Set sht = ThisWorkbook.Worksheets("Data")


strDBName = "" & sht.Range("TP5000").Value & ".accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName

connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB

Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Part 1")

Set adoRecSet = New ADODB.Recordset

strTable = "P1"

adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic

lFieldCount = adoRecSet.Fields.Count

lastRow = ws.Cells(Rows.Count, "A").End(xlUp).row

On Error GoTo CheckError
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1

If ws.Cells(i, n + 1) = "" Then
adoRecSet.Fields(n).Value = Null
Else
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1)
End If
Next n
adoRecSet.Update
Next i

adoRecSet.Close
connDB.Close

Set adoRecSet = Nothing
Set connDB = Nothing

ImportDataToP1 = FlagCheck
Exit Function

CheckError:
MsgBox "Loi sai kieu du lieu. Part 1: Row: " & i & ", Column: " & (n + 1) & " , Value: " & ws.Cells(i, n + 1).Value
FlagCheck = True
ImportDataToP1 = FlagCheck
Exit Function
End Function
 

File đính kèm

Chào cả nhà GPEX!

Em có một thắc mắc mong mọi người giúp đỡ em nhé..!
Hiện tại mình có thể Import data từ Excel sang Access, vậy mình có thể dùng VBA can thiệp để delete data từ Excel sang Access không ạ...
Chuyện là: Em có một Macro Import Data từ Excel vào Access nhưng trước đó trong Access đã có Data sẵng nên nhiều lúc Import vào bị trùng và bị báo lỗi, nên Em phải dùng một bước vào Access để xóa hết dữ liệu trong đó rồi mới Import vào...
Vậy em muốn viết một cái Macro xóa data trong Access trước rồi mới Inport vào được không ạ... Em mò trên hoài nên không ra...
Mong các Thầy giúp đỡ ...Em cảm ơn ạ!

'Lệnh Import
Function ImportDataToP1(FlagCheck As Boolean)
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long

Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection

Set sht = ThisWorkbook.Worksheets("Data")


strDBName = "" & sht.Range("TP5000").Value & ".accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName

connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB

Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Part 1")

Set adoRecSet = New ADODB.Recordset

strTable = "P1"

adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic

lFieldCount = adoRecSet.Fields.Count

lastRow = ws.Cells(Rows.Count, "A").End(xlUp).row

On Error GoTo CheckError
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1

If ws.Cells(i, n + 1) = "" Then
adoRecSet.Fields(n).Value = Null
Else
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1)
End If
Next n
adoRecSet.Update
Next i

adoRecSet.Close
connDB.Close

Set adoRecSet = Nothing
Set connDB = Nothing

ImportDataToP1 = FlagCheck
Exit Function

CheckError:
MsgBox "Loi sai kieu du lieu. Part 1: Row: " & i & ", Column: " & (n + 1) & " , Value: " & ws.Cells(i, n + 1).Value
FlagCheck = True
ImportDataToP1 = FlagCheck
Exit Function
End Function
Bạn có thể vào http://www.giaiphapexcel.com/dienda...vấn-csdl-từ-file-excel-đến-file-access.69450/ để đọc thêm nhé
 
Upvote 0
Chào cả nhà GPEX!

Em có một thắc mắc mong mọi người giúp đỡ em nhé..!
Hiện tại mình có thể Import data từ Excel sang Access, vậy mình có thể dùng VBA can thiệp để delete data từ Excel sang Access không ạ...
Chuyện là: Em có một Macro Import Data từ Excel vào Access nhưng trước đó trong Access đã có Data sẵng nên nhiều lúc Import vào bị trùng và bị báo lỗi, nên Em phải dùng một bước vào Access để xóa hết dữ liệu trong đó rồi mới Import vào...
Vậy em muốn viết một cái Macro xóa data trong Access trước rồi mới Inport vào được không ạ... Em mò trên hoài nên không ra...
Mong các Thầy giúp đỡ ...Em cảm ơn ạ!

'Lệnh Import
Function ImportDataToP1(FlagCheck As Boolean)
Dim strMyPath As String, strDBName As String, strDB As String, strSQL As String
Dim i As Long, n As Long, lastRow As Long, lFieldCount As Long

Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection

Set sht = ThisWorkbook.Worksheets("Data")


strDBName = "" & sht.Range("TP5000").Value & ".accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName

connDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB

Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Part 1")

Set adoRecSet = New ADODB.Recordset

strTable = "P1"

adoRecSet.Open Source:=strTable, ActiveConnection:=connDB, CursorType:=adOpenStatic, LockType:=adLockOptimistic

lFieldCount = adoRecSet.Fields.Count

lastRow = ws.Cells(Rows.Count, "A").End(xlUp).row

On Error GoTo CheckError
For i = 2 To lastRow
adoRecSet.AddNew
For n = 0 To lFieldCount - 1

If ws.Cells(i, n + 1) = "" Then
adoRecSet.Fields(n).Value = Null
Else
adoRecSet.Fields(n).Value = ws.Cells(i, n + 1)
End If
Next n
adoRecSet.Update
Next i

adoRecSet.Close
connDB.Close

Set adoRecSet = Nothing
Set connDB = Nothing

ImportDataToP1 = FlagCheck
Exit Function

CheckError:
MsgBox "Loi sai kieu du lieu. Part 1: Row: " & i & ", Column: " & (n + 1) & " , Value: " & ws.Cells(i, n + 1).Value
FlagCheck = True
ImportDataToP1 = FlagCheck
Exit Function
End Function
Tôi biết có 2 cách để xóa dữ liệu Table trong Access
1/ Sử Dụng ADO
2/ Sử dụng DAO
3/ .....Chưa Biết ...
4/ Tham khảo DAO Link sau bài 26
http://www.giaiphapexcel.com/diendan/threads/ghi-và-lấy-dữ-liệu-giữa-access-và-excel.122982/page-2
 
Upvote 0
Cảm ơn cả nhà nhiều nhé! Em làm được rồi ạ
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom