Em cần giúp đỡ về ado

  • Thread starter bigbigworld
  • Ngày gửi
B

bigbigworld

Guest
10/9/05
21
0
1
Quan 9
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

Em sử dụng đoạn mã trên để chuyển data từ excel vào access. Trường hợp file DataBaseName.mdb có password thì không mở được. Bây giờ em phải làm sao.
----------------
Trường hợp em muốn lấy dữ liệu từ file access thì lập trình như thế nào? (Bình thường em dùng External để lấy ra). Nếu dùng External và lập trình thì cái nào có lợi hơn ?
----------------
 
Khóa học Quản trị dòng tiền
Đào Việt Cường

Đào Việt Cường

Moderator
22/11/05
400
4
18
Khánh Hòa
Dear bigbigword,
---------------
Bạn muốn mở được cánh cửa thì bạn phải có chìa khoá đã chứ, đúng không! Nếu *.mdb có mật khẩu thì bạn phải có pass của nó. Bạn tìm hiểu thêm về đối tượng connection. Khi mở kết nối, ngoài tham số connectstring còn có các tham số quan trọng khác, trong đó có UserID và Password. Bạn truyền thêm hai tham số này có thể (có thể thôi nhé) sẽ kết nối được với database.
Chúc bạn thành công!
 

Xem nhiều

Webketoan Zalo OA