访问输入框重复用户提示

发布于 2025-01-09 10:16:55 字数 795 浏览 0 评论 0原文

我有一个很大的 Access 表。我有一个名为 qry_ExtractYear 的查询,我希望用户修改条件并导出结果。我这里的代码可以工作,但我遇到了在导出之前重复提示 sDate1 和 sDate2 的问题。请问我做错了什么?

Sub ExportAnnualQuoteActivity()

Dim db As dao.Database
    Set db = CurrentDb
    Dim qdf As dao.QueryDef
    Dim sDate1 As String
    Dim sDate2 As String
    
    sDate1 = InputBox(prompt:="Start Date YYYYMMDD")
    sDate2 = InputBox(prompt:="End Date YYYYMMDD")
        
    
    Set qdf = db.QueryDefs("Qry_ExtractYear")
    qdf.SQL = "Select * From [tbl_QuoteData] WHERE [Quote Date] BETWEEN sDate1 AND sDate2"
    
    DoCmd.Save acQuery, "Qry_ExtractYear"
    
    DoCmd.OutputTo acOutputQuery, "Qry_ExtractYear", acFormatXLSX, "T:\Actuary\Metrics\NB\Data\Yearly\tbl_QuoteData_" & Left(sDate2, 4) & ".xlsx", True
    
 
    
End Sub

I have a large Access table. I have a query entitled qry_ExtractYear which I would like to have the criteria modified by the user and the the results exported. I have the code here which works but I am having the issue of being prompted repeatedly for sDate1 and sDate2 before the export occurs. What am I doing wrong please?

Sub ExportAnnualQuoteActivity()

Dim db As dao.Database
    Set db = CurrentDb
    Dim qdf As dao.QueryDef
    Dim sDate1 As String
    Dim sDate2 As String
    
    sDate1 = InputBox(prompt:="Start Date YYYYMMDD")
    sDate2 = InputBox(prompt:="End Date YYYYMMDD")
        
    
    Set qdf = db.QueryDefs("Qry_ExtractYear")
    qdf.SQL = "Select * From [tbl_QuoteData] WHERE [Quote Date] BETWEEN sDate1 AND sDate2"
    
    DoCmd.Save acQuery, "Qry_ExtractYear"
    
    DoCmd.OutputTo acOutputQuery, "Qry_ExtractYear", acFormatXLSX, "T:\Actuary\Metrics\NB\Data\Yearly\tbl_QuoteData_" & Left(sDate2, 4) & ".xlsx", True
    
 
    
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-01-16 10:16:55

这是因为构造的 SQL 具有文字文本“sDate1 AND sDate2”,而不是这些变量的值。连接变量并使用日期/时间#分隔符。

WHERE [Quote Date] BETWEEN #" & sDate1 & "# AND #" & sDate2 & "#"

如果字段是文本类型,请使用撇号分隔符而不是#。数字字段不需要分隔符。

建议不要使用 InputBox,因为无法轻松验证用户输入。可变用户输入的表单上的参考控件。

It's because the constructed SQL has literal text "sDate1 AND sDate2" and not the values of those variables. Concatenate variables and use date/time # delimiter.

WHERE [Quote Date] BETWEEN #" & sDate1 & "# AND #" & sDate2 & "#"

If field is text type, use apostrophe delimiter instead of #. Number fields do not require delimiters.

Advise not to use InputBox as cannot easily validate user input. Reference controls on form for variable user input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文