MS Access VBA 中的 SQL 查询
数据记录源表单中的此查询工作正常,返回我想要的精确值
SELECT tblRevRelLog_Detail.RevRelTrackingNumber, tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.Version, tblRevRelLog_Detail.JobPnType, tblRevRelLog_Detail.EdsName, tblRevRelLog_Detail.DetailerNamePerPartNumber, tblRevRelLog_Detail.DetailerCompanyPerPartNumber
FROM tblRevRelLog_Detail
LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber
WHERE (((tblEventLog.PartNumber) Not In
(SELECT tblEventLog.PartNumber
FROM tblEventLog
WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber)))
ORDER BY tblRevRelLog_Detail.PartNumber;
但是如果我在 VBA 中编写相同的查询。它没有返回任何内容
strNewSql = "SELECT tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.ID FROM tblRevRelLog_Detail LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber"
strNewSql = strNewSql & " WHERE ((tblEventLog.PartNumber) Not In (SELECT tblEventLog.PartNumber FROM tblEventLog WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper' AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber);"
查询有问题?有人可以帮助我吗!
This query in the form-Data-Record Source is working fine, returning exact values that i want
SELECT tblRevRelLog_Detail.RevRelTrackingNumber, tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.Version, tblRevRelLog_Detail.JobPnType, tblRevRelLog_Detail.EdsName, tblRevRelLog_Detail.DetailerNamePerPartNumber, tblRevRelLog_Detail.DetailerCompanyPerPartNumber
FROM tblRevRelLog_Detail
LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber
WHERE (((tblEventLog.PartNumber) Not In
(SELECT tblEventLog.PartNumber
FROM tblEventLog
WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber)))
ORDER BY tblRevRelLog_Detail.PartNumber;
But if i write the same query in the VBA. It is not returning anything
strNewSql = "SELECT tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel, tblRevRelLog_Detail.ID FROM tblRevRelLog_Detail LEFT JOIN tblEventLog ON tblRevRelLog_Detail.PartNumber = tblEventLog.PartNumber"
strNewSql = strNewSql & " WHERE ((tblEventLog.PartNumber) Not In (SELECT tblEventLog.PartNumber FROM tblEventLog WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper' AND tblEventLog.TrackingNumber = tblRevRelLog_Detail.RevRelTrackingNumber);"
Something wrong with the query?? can someone help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑你的变量 tempTrackingNumber 不好
I would suspect that your variable tempTrackingNumber is bad
如果
RevRelTrackingNumber
是数字,则无需将其括在引号中。If
RevRelTrackingNumber
is numeric you don't need to enclose it in quotes.tempTrackingNumber 的数据类型是什么?您的代码(前面截断)显示了这一点:
这意味着它是一个字符串,并且将被视为字符串。
如果它实际上是数字,你会需要这个:
What's the data type of tempTrackingNumber? Your code (front-truncated) shows this:
This implies that it's a String, and it will be treated as such.
If it's actually numeric, you'll want this instead:
首先,查询的内容不一样。例如,纯文本 SQL 在 SELECT 子句中有八列,而 VBA 版本只有三列。
其次,您在同一范围内两次使用相同的表名称
tblEventLog
。因此,您将需要至少使用一个表关联名称。我知道 tbl- 前缀是 Access 世界中的一个“骄傲点”,但它确实使您的表名变得更长且更难以阅读(IMO)(并且前缀被 ISO 11179 标准 用于数据元素命名:))...那么为什么不始终使用表相关名称呢?第三,对于 Access(ACE/Jet/其他),IIRC
EXISTS
的性能优于IN
,并且 IMO 更易于理解(DISTINCT..INNER JOIN
可能会表现得更好,但又更难阅读,IMO)。这是建议的重写:
更新:似乎我对
EXIST
给予更好的看法是错误的,所以这里有更多重写可供选择:实际上,我正在努力使用此重写Access 的专有连接。我不断收到“灾难性故障”错误。这是重现代码,我哪里出错了?:
First, the queries are not the same. For example, your plaintext SQL has eight columns in the
SELECT
clause where the VBA version has just three.Second, you are using the same table name,
tblEventLog
, twice in the same scope. So you will need to use at least one table correlation name. I know the tbl- prefix is a 'point of pride' in the Access world but it does make you table names longer and harder to read IMO (and prefixes are specifically outlawed by the ISO 11179 Standard for data element naming :))... so why not use table correlation names throughout?Third, IIRC
EXISTS
performs better thanIN
for Access (ACE/Jet/whatever) and IMO is easier to understand (DISTINCT..INNER JOIN
may perform even better but is again harder to read, IMO).Here's a suggested re-write:
UPDATE: Seems I was wrong about the
EXIST
giving being better, so here's a couple more rewrite to choose from:Actually, I'm struggling to get this re-writes using Access's proprietary joins. I keep getting a "Catastrophic failure" error. Here's the repro code, where am I going wrong?:
好像你缺少一个右括号。在 VBA 中的查询末尾添加“)”并尝试。
Seems like you missing a closing bracket. Add ")" at the end of the query in VBA and try.