Thầy xem giúp em cách khắc phục lỗi này nhé
Chào bạn bạn kết nối Earlybinding xem nhé
hoặc thử đoạn code sau
Option Explicit
Sub KetnoiLateBinding()
Dim mysql As String, ConnectionString As String
Dim cnn As Object, lrs As Object
Set cnn = CreateObject("ADODB.Connection")
Set lrs = CreateObject("ADODB.Recordset")
If Val(Application.Version) < 12 Then
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"";"
Else
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=0"";"
End If
cnn.Open
''mysql = "SELECT * FROM [Customers1$]"
mysql = "SELECT * FROM Customers"
lrs.Open mysql, cnn
sheetsname.Range("A1").CopyFromRecordset lrs
End Sub
lrs.Close: Set lrs = Nothing
cnn.Close: Set cnn = Nothing
End Sub
may em khong khai bao duoc bien ADODB.Connection. khac phuc nhu sao a
Chào bạn máy bạn thử code mẫu xem thế nào nhé.
Option Explicit
Sub KetnoiLateBinding()
Dim mysql As String, ConnectionString As String
Dim cnn As Object, lrs As Object
Set cnn = CreateObject("ADODB.Connection")
Set lrs = CreateObject("ADODB.Recordset")
If Val(Application.Version) < 12 Then
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"";"
Else
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=0"";"
End If
cnn.Open
''mysql = "SELECT * FROM [Customers1$]"
mysql = "SELECT * FROM Customers"
lrs.Open mysql, cnn
sheetsname.Range("A1").CopyFromRecordset lrs
End Sub
lrs.Close: Set lrs = Nothing
cnn.Close: Set cnn = Nothing
End Sub