Sự khách biệt giữa hai cách import dữ liệu từ excel vào access
Các anh chị giúp em. Em đã tìm ra đoạn code để import từ excel vào access như sau:
http://www.utteraccess.com/forums/printthread.php?Cat=&Board=88&main=497117&type=thread
So sánh với đoạn code import trong module của em, không biết lỗi ở đâu:
Tuy nhiên cách 1 em đã import thành công vào access.
Còn với cách 2, em chỉ impor được vào table GLINVC, nhưng không import được vào table GLDATA, thiệt tình không hiểu? (vì cấu trúc lệnh INSERT INTO GLDATA (field1,field2,...,field n) em làm đúng mà!.....
Các anh chị giúp em với.
Các anh chị giúp em. Em đã tìm ra đoạn code để import từ excel vào access như sau:
http://www.utteraccess.com/forums/printthread.php?Cat=&Board=88&main=497117&type=thread
PHP:
'Cách 1:
Function InsertInto_GLDATA()
'Macro purpose: To add record to Access database using ADO and SQL
'NOTE: Reference to Microsoft ActiveX Data Objects Libary required
'-------------------------------------------------------------------------------
Application.ScreenUpdating = False
Dim gcnAccess as New ADOB.connection
Dim gcnRecords as New ADOB.Recordset
Dim rngDetail As Range
Dim tblName As String
tblName = "GLDATA"
'-------------------------------------------------------------------------------
'Open connection to the database
Call ConnectToDatabase
'Chuan bi tien hanh import du lieu vao access
On Error GoTo EndUpdate
'-------------------------------------------------------------------------------
'Insert records into database from worksheet table
gcnAccess.BeginTrans
Set gcnRecords = New ADODB.recordset
For Each rngDetail In Range(Sheets("GLDATA").[a2], Sheets("GLDATA").[a65536].End(xlUp))
gcnRecords.Open "Select Period,DocNbr,DocDate,DocDesc,DocAttached,DrAcct,CrAcct,CustVendID,InvtID,LineQty," & _
"LineUP,LineAmt,Serial,InvcNbr,InvcDate,TaxRate,InvcDesc,ContactName,WorkCode,TaxID," & _
"Division,CostCenter,CflwID,User,Date From " & tblName, gcnAccess, adOpenStatic, adLockPessimistic, adCmdText
With gcnRecords
.AddNew ' create a new record
' Add values to each field in the record
.Fields("Period") = rngDetail.Value
.Fields("DocNbr") = rngDetail(, 2).Value
.Fields("DocDate") = rngDetail(, 3).Value
.Fields("DocDesc") = rngDetail(, 4).Value
.Fields("DocAttached") = rngDetail(, 5).Value
.Fields("DrAcct") = rngDetail(, 6).Value
.Fields("CrAcct") = rngDetail(, 7).Value
.Fields("CustVendID") = rngDetail(, 8).Value
.Fields("InvtID") = rngDetail(, 9).Value
.Fields("LineQty") = rngDetail(, 10).Value
.Fields("LineUP") = rngDetail(, 11).Value
.Fields("LineAmt") = rngDetail(, 12).Value
.Fields("Serial") = rngDetail(, 13).Value
.Fields("InvcNbr") = rngDetail(, 14).Value
.Fields("InvcDate") = rngDetail(, 15).Value
.Fields("TaxRate") = rngDetail(, 16).Value
.Fields("InvcDesc") = rngDetail(, 17).Value
.Fields("ContactName") = rngDetail(, 18).Value
.Fields("WorkCode") = rngDetail(, 19).Value
.Fields("TaxID") = rngDetail(, 20).Value
.Fields("Division") = rngDetail(, 21).Value
.Fields("CostCenter") = rngDetail(, 22).Value
.Fields("CflwID") = rngDetail(, 23).Value
.Fields("User") = rngDetail(, 24).Value
.Fields("Date") = rngDetail(, 25).Value
.Update ' stores the new record
.Close
End With
Next
a = MsgBox("Da cap nhat du lieu vao he thong thanh cong!", vbInformation, "Thong bao")
'-------------------------------------------------------------------------------------------------
EndUpdate:
'Check if error was encounted
If Err.Number <> 0 Then
'Error encountered. Rollback transaction and inform user
On Error Resume Next
gcnAccess.RollbackTrans
MsgBox vbObjectError & "-" & Err.Number & "-" & Err.Description, vbCritical, "Error!"
Else
On Error Resume Next
gcnAccess.CommitTrans
End If
'Close the ADO objects
gcnAccess.Close
Set gcnRecords = Nothing
Set gcnAccess = Nothing
On Error GoTo 0
Application.ScreenUpdating = True
End Function
So sánh với đoạn code import trong module của em, không biết lỗi ở đâu:
PHP:
'Cách 2:
Function InsertInto_GLDATA()
'Macro purpose: To add record to Access database using ADO and SQL
'NOTE: Reference to Microsoft ActiveX Data Objects Libary required
'-------------------------------------------------------------------------------
Dim colHead, rcdDetail As String
Dim rngHeader, rngDetail As Range
Dim NColumn As Integer
Dim NRow As Integer
Dim NotNull As Boolean
Dim tblName As String
Dim TargetDetail As New ADODB.recordset
Set rngHeader = Range("A1:Y1")
Set rngDetail = Range("A2:Y2")
tblName = "GLDATA"
'MsgBox rngDetail.Address & Chr(13) & rngHeader.Address, , "Thong bao"
'Truyen tham so cot can insert vao access:
'Ke noi chuoi cac cot trong cu pham Insert into tblTemp(column1, column2,column3,....)
colHead = " ("
For NColumn = 1 To rngHeader.Count
colHead = colHead & rngHeader.Columns(NColumn).Value
Select Case NColumn
Case Is = rngHeader.Count
colHead = colHead & ")"
Case Else
colHead = colHead & ","
End Select
Next NColumn
MsgBox "Insert into " & colHead & " From " & tblName, , "Thong bao" 'kietvt: Bay loi tai day
'-------------------------------------------------------------------------------
'Open connection to the database
Call ConnectToDatabase
'Chuan bi tien hanh import du lieu vao access
On Error GoTo EndUpdate
'-------------------------------------------------------------------------------
'Insert records into database from worksheet table
gcnAccess.BeginTrans
For NRow = 1 To rngDetail.Rows.Count
'Assume record is completely Null, and open record string for concatenation
NotNull = False
rcdDetail = "('"
'Evaluate field in the record
For NColumn = 1 To rngHeader.Count
Select Case rngDetail.Rows(NRow).Columns(NColumn).Value
'if empty, append value of null to string
Case Is = Empty
Select Case NColumn
Case Is = rngHeader.Count
rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "'')" 'Neu gia tri null thi bo qua va cach nhau boi dau ")" trong truong hop cot cuoi cung
Case Else
rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & ",'" 'Neu gia tri null thi bo qua va cach nhau boi dau ")" trong truong hop cot cuoi cung
End Select
'if not empty, set notNull to true, and append value to string
Case Else
NotNull = True
Select Case NColumn
Case Is = rngHeader.Count
rcdDetail = rcdDetail & rngDetail.Rows(NRow).Columns(NColumn).Value & "')"
Case Else
rcdDetail = rcdDetail & rngDetail.Rows(NRow).Columns(NColumn).Value & "','"
End Select
End Select
Next NColumn
MsgBox rcdDetail, , "Thongbao" 'kiet
'If record consists of only Null values, do not insert it to table, otherwise
'insert the record
Select Case NotNull
Case Is = True
TargetDetail.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, gcnAccess
Case Is = False
'do not insert record
End Select
Next NRow
a = MsgBox("Da cap nhat du lieu vao he thong thanh cong!", vbInformation, "Thong bao")
EndUpdate:
'Check if error was encounted
If Err.Number <> 0 Then
'Error encountered. Rollback transaction and inform user
On Error Resume Next
gcnAccess.RollbackTrans
MsgBox vbObjectError & "-" & Err.Number & "-" & Err.Description, vbCritical, "Error!"
Else
On Error Resume Next
gcnAccess.CommitTrans
End If
'Close the ADO objects
gcnAccess.Close
Set TargetData = Nothing
Set gcnAccess = Nothing
On Error GoTo 0
End Function
Tuy nhiên cách 1 em đã import thành công vào access.
Còn với cách 2, em chỉ impor được vào table GLINVC, nhưng không import được vào table GLDATA, thiệt tình không hiểu? (vì cấu trúc lệnh INSERT INTO GLDATA (field1,field2,...,field n) em làm đúng mà!.....
Các anh chị giúp em với.