Lỗi xảy ra khi thực thi DAO Connection?
shevchenki > 08-12-17, 12:32 AM
Chuyện là em xuất dữ liệu từ một Query qua một File Excel bên ngoài và em dùng ADO Connection.
Em hay dùng DAO là chính nên lỗi về ADO em cũng ko rõ nên hỏi.
Với Query mà em có gắn Parameter là dạng Form![FormName]![BoxName] thì khi em chạy thì em bị lỗi như dưới:
RunTime Error - 2147217900 Invalid SQL Statement; expected 'DELETE' 'INSERT'...
Với Query mà em gắn Parameter với điều kiệu trực tiếp như kiểu Is Null hay "Một giá trị bất kỳ" thì vẫn chạy được, nhưng cứ gắn dạng biến theo Form thì lại bị lỗi này. Em có định dạng cho Form![FormName]![BoxName] trong Parameter của Query nhưng vẫn kết quả như vậy.
'Trigger
Private Sub BntOuputQuery_Click()
Const stPath As String = "C:\FolderName"
Const stXLName As String = "\FileName.xlsm"
Const stQryName As String = "QueryName" 'Query có sự dụng Parameter dạng Form![FormName]![BoxName]
Const stSheet As String = "SheetNameOfExcel"
Const stRng As String = "A4" 'Paste To Cell
Call OpenExcel(stPath, stXLName, stQryName, stSheet, stRng, "B1")
End Sub
'To Open and Paste On Excel
Public Sub OpenExcel(ByVal stPath As String, ByVal stXLName As String, ByVal stQryName As String, ByVal stSheet As String, ByVal stRng As String, ByVal timeRng As String)
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim xls As Excel.Application
Dim wkb As Excel.Workbook
Set cnt = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open stQryName, cnt 'Lỗi xảy ra ở đây
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Add template:=stPath & stXLName
Set wkb = xls.Workbooks(1)
With wkb.Worksheets(stSheet)
.Range(stRng).CopyFromRecordset Data:=rst
.Range(timeRng).Value = Now
End With
xls.Visible = True
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub
Mong mọi người cho e lời khuyên
EM CÁM ƠN!