- Tham gia
- 30/5/06
- Bài viết
- 1,798
- Được thích
- 4,706
- Giới tính
- Nam
Trong một số ứng dụng, việc khởi tạo và import dữ liệu bên ngoài vào sau khi bạn hoàn thành là một công việc không dễ dàng chút nào. Làm sao để kiểm tra việc cập nhật có thành công hay không? Nếu không thành công thì hủy bỏ tất cả...
Việc cập nhật theo lô sẽ giúp bạn điều này.
Xin giới thiệu một thủ tục để các bạn tham khảo:
Tôi đã test thủ tục trên với hơn 70,000 records và có vài nhận xét như sau:
1. Bằng việc dùng mảng từ đối tượng Range code của các bạn sẽ nhanh hơn.
2. Đối với dữ liệu quá lớn, các thao tác thêm vào trước khi gọi phương thức UpdateBatch thực hiện rất nhanh. Nhưng khi các bạn gọi phương thức này thì chương trình của bạn sẽ "bị treo" một thời gian, mới thực hiện xong. Vậy nên, cách tốt nhất là chúng ta chia dữ liệu thành nhiều phần nhỏ (giả sử chia thành nhiều sheets chẳng hạn), rồi cập nhật theo từng phần là tốt nhất.
Lê Văn Duyệt
Việc cập nhật theo lô sẽ giúp bạn điều này.
Xin giới thiệu một thủ tục để các bạn tham khảo:
Mã:
Option Explicit
Const DBTable As String = "TB_Bom"
Const DBPath As String = "\\Sun-Server\Production\QuanLyKho.mdb"
Sub BatchUpdate()
Dim iLastrow As Long, i As Long, j As Long
Dim conn As ADODB.Connection
Dim ADOrst As ADODB.Recordset
Dim arrFieldnames As Variant
Dim arrValues As Variant
Dim arrRecordvals As Variant
On Error GoTo ErrorHandler
arrFieldnames = Array("sBomHeader", "sBomDes", _
"sMaNo", "sMaDes", "sMaUoM", "nMaQty") 'change as needed
'Speed up execution by disabling screen updating
Application.ScreenUpdating = False
'Make a connection to your database file
Set conn = New ADODB.Connection
With conn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "data source=" & DBPath
.Open
End With
'Create a *new* recordset here because we overwrite the ones in the existing table
Set ADOrst = New ADODB.Recordset
'Use a client cursor and adLockBatchOptimistic to do batch updates
ADOrst.CursorLocation = adUseClient
ADOrst.Open DBTable, conn, adOpenStatic, adLockBatchOptimistic
'Find the last row(number) with data in Sheet1
With ThisWorkbook.Worksheets("BOM_09092008")
iLastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
'Clear the table
ClearTable (DBTable)
'Assign your worksheet values in one statement to the variable arrValues (type Variant)
arrValues = ThisWorkbook.Worksheets("BOM_09092008").Range("A2:M" & iLastrow).Value
'Stuff the worksheet values into the recordset
For i = 1 To UBound(arrValues, 1)
If Len(arrValues(i, 9)) > 0 Then
arrRecordvals = Array(arrValues(i, 1), arrValues(i, 2), _
arrValues(i, 9), arrValues(i, 10), _
arrValues(i, 13), arrValues(i, 12))
ADOrst.AddNew arrFieldnames, arrRecordvals
Application.StatusBar = "Update to record " & i & "/" & iLastrow - 1
End If
Next i
Application.StatusBar = "Batch updating...Please wait."
'(Batch)Update the table with the just created recordset
[B]ADOrst.UpdateBatch
[/B]
'Close the recordset
ADOrst.Close
'Close database connection
conn.Close
'Inform the user
MsgBox "Updating is successful.", vbOKOnly + vbInformation, "Inf"
ErrorExit:
'Clean up
Set ADOrst = Nothing
Set arrValues = Nothing
Set arrRecordvals = Nothing
Set arrFieldnames = Nothing
'Re-enable screen updating
Application.ScreenUpdating = True
Application.StatusBar = False
Exit Sub
ErrorHandler:
MsgBox "Error is " & Err.Number & "; Error description: " & Err.Description
Resume ErrorExit
End Sub
Sub ClearTable(sTable As String)
'Thủ tục này nhằm xóa dữ liệu trong bảng
With New ADODB.Connection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open DBPath
.Execute "DELETE FROM " & sTable
.Close
End With
End Sub
Tôi đã test thủ tục trên với hơn 70,000 records và có vài nhận xét như sau:
1. Bằng việc dùng mảng từ đối tượng Range code của các bạn sẽ nhanh hơn.
Mã:
arrValues = ThisWorkbook.Worksheets("BOM_09092008").Range("A2:M" & iLastrow).Value
2. Đối với dữ liệu quá lớn, các thao tác thêm vào trước khi gọi phương thức UpdateBatch thực hiện rất nhanh. Nhưng khi các bạn gọi phương thức này thì chương trình của bạn sẽ "bị treo" một thời gian, mới thực hiện xong. Vậy nên, cách tốt nhất là chúng ta chia dữ liệu thành nhiều phần nhỏ (giả sử chia thành nhiều sheets chẳng hạn), rồi cập nhật theo từng phần là tốt nhất.
Lê Văn Duyệt
Lần chỉnh sửa cuối: