2015-02-17 43 views
0

任何人都可以幫助我設置一個Access 2007 VBA查詢,其中視覺受損的人可以在表單上輸入日期(Pdate)並導致查詢我的表[ Donation_Data]查找[拾取日期] = Pdate的所有記錄,然後自動將記錄導出爲EXCEL文件。簡單的點擊,查詢和導出 - 並非如此簡單

這裏是我到目前爲止,但

qd.SQL停止= 「SELECT * FROM」 & [Donation_Data] & 「WHERE [皮卡日期] = PDATE」

Option Compare Database 
 
Option Explicit 
 

 
Private Sub Command1_Click() 
 

 
Dim db As DAO.Database 
 
Set db = CurrentDb ' use current database 
 

 
'On Error GoTo Pickup_OnClick_Err 
 

 
Dim Pdate As Date 
 

 
Pdate = InputBox("Please Enter the Desired Pickup Date", "") 
 

 

 
'develop a query of all Donation_Data records where Pickup Date in Donation_Data table = Pdate 
 

 
Dim qd As DAO.QueryDef 
 

 
Set qd = CurrentDb.CreateQueryDef("DonationDataQuery") 
 

 
qd.SQL = "SELECT * FROM " & [Donation_Data] & " WHERE [Pickup Date] = Pdate" 
 

 
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "DonationDataQuery", "C:\Users\dads\Downloads\Donation Data.xlsx", True 
 

 
Debug.Print qd.SQL 
 

 
MsgBox "transfered" 
 

 

 
End Sub

根據下面的評論推薦,代碼現在看起來像這樣。查詢開發精美,但沒有數據傳輸

Dim db As DAO.Database 
 
Set db = CurrentDb ' use current database 
 

 
'On Error GoTo Pickup_OnClick_Err 
 

 
Dim Pdate As Date 
 

 
'Pdate = InputBox("Please Enter the Desired Pickup Date", "") 
 

 

 
'develop a query of all Donation_Data records where Pickup Date in Donation_Data table = Pdate 
 

 
Dim qd As DAO.QueryDef 
 

 
Set qd = CurrentDb.CreateQueryDef("DonationDataQuery") 
 

 
qd.SQL = "SELECT * FROM [Donation_Data] WHERE [Donation_Data].[Pickup Date] = Pdate" 
 

 
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "DonationDataQuery", "C:\Users\dads\Downloads\Donation Data.xlsm", True 
 

 
Debug.Print qd.SQL 
 

 
MsgBox "transfered" 
 

 

 
End Sub

回答

1

試試這個,替換(未經測試),這個行:

qd.SQL = "SELECT * FROM [Donation_Data] WHERE [Donation_Data].[Pickup Date] = Pdate" 
+0

謝謝....越來越近。它在Access 2007中創建查詢結果,但不會將任何數據傳輸到空白文件「Donation Data.xlsx」。所以我將文件更改爲Donation Data.xlsm並聲明: – Michael 2015-02-18 02:13:39

+0

DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel12Xml,「DonationDataQuery」,「C:\ Users \ dads \ Downloads \ Donation Data.xlsm」,True傳輸消息表示傳輸,但是文件中沒有數據 – Michael 2015-02-18 02:14:25

+0

您的輸入框行中有一個撇號,您的SQL對於Pdate沒有任何價值,因此您的代碼返回0條記錄 – 2015-02-18 02:39:00