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
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