简单的点击,查询和导出 - 不是那么简单

时间:2015-02-17 21:36:59

标签: access-vba

任何人都可以帮我设置一个Access 2007 VBA查询,其中视障人士可以在表单上输入日期(Pdate)并导致查询我的表[Donation_Data]以查找[Pickup Date]的所有记录= Pdate然后自动将记录导出为EXCEL文件。

这是我到目前为止所做的,但是停在

qd.SQL =“SELECT * FROM”& [Donation_Data]& “哪里[提货日期] = 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 个答案:

答案 0 :(得分:1)

试试这个,用(UNTESTED)替换有问题的行:

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