将 SQL 选择查询更改为 VBA [英] Changing SQL select query to VBA

查看:68
本文介绍了将 SQL 选择查询更改为 VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的查询转换为使用 VBA 为邮件合并选择数据的能力.现在我有一个使用两个参数的内置访问查询:

I am trying to transition my queries into the ability to select data for a mail merge using VBA. Right now I have a query built in access that uses two parameters:

PARAMETERS startdate DateTime, enddate DateTime;

我基本上想在运行时重新创建该查询以填充字母.

and i basically want to recreate that query at run time to populate letters.

date1 = InputBox("Please enter the start date, e.g. 5/28", "Please enter the start date")
date2 = InputBox("Please enter the end date, e.g. 6/28", "Please enter the end date")   

date1 = date1 & "/" & thisYear
date2 = date2 & "/" & thisYear


sqlstatement:="SELECT * FROM [Customer Data] WHERE [Customer Data].[Status]='Complete' AND [Customer Data].[CompletedBy] = '" & userID & "' AND [Customer Data].[Date Completed] Between " & date1 & " And " & date2 & ";"

我的date1 和 date2 之间"部分存在固有的错误,如果我删除语句的那部分,查询工作正常,但我需要能够指定日期范围.现在它告诉我没有找到任何数据.那部分我做错了什么?

There is something inherently wrong with my "between date1 and date2" part, if I remove that part of the statement, the query works fine, but i will require the ability to specify a date range. Right now it is telling me that no data was found. What am I doing wrong with that part?

推荐答案

"' AND [Customer Data].[Date Completed] 介于 #" &日期 1 &# 和 #" &日期 2 &"#;"

正如您使用撇号包围字符串数据一样,日期值应该用 # 括起来.如果这没有帮助,请发表您的评论.

Just as you are using apostrophe to surround the string data, the date values should be enclosed with #. Post your comments, if this doesn't help.

这篇关于将 SQL 选择查询更改为 VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆