MS Access VBA 中的 SQL 查询

发布于 2024-10-01 14:07:52 字数 1378 浏览 4 评论 0原文

数据记录源表单中的此查询工作正常,返回我想要的精确值

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 技术交流群。

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

发布评论

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

评论(5

聚集的泪 2024-10-08 14:07:52

我怀疑你的变量 tempTrackingNumber 不好

I would suspect that your variable tempTrackingNumber is bad

耳钉梦 2024-10-08 14:07:52

如果 RevRelTrackingNumber 是数字,则无需将其括在引号中。

If RevRelTrackingNumber is numeric you don't need to enclose it in quotes.

明月松间行 2024-10-08 14:07:52

tempTrackingNumber 的数据类型是什么?您的代码(前面截断)显示了这一点:

...tblRevRelLog_Detail.RevRelTrackingNumber = """ & tempTrackingNumber & """);"

这意味着它是一个字符串,并且将被视为字符串。

如果它实际上是数字,你会需要这个:

...tblRevRelLog_Detail.RevRelTrackingNumber = " & tempTrackingNumber & ");"

What's the data type of tempTrackingNumber? Your code (front-truncated) shows this:

...tblRevRelLog_Detail.RevRelTrackingNumber = """ & tempTrackingNumber & """);"

This implies that it's a String, and it will be treated as such.

If it's actually numeric, you'll want this instead:

...tblRevRelLog_Detail.RevRelTrackingNumber = " & tempTrackingNumber & ");"
维持三分热 2024-10-08 14:07:52

首先,查询的内容不一样。例如,纯文本 SQL 在 SELECT 子句中有八列,而 VBA 版本只有三列。

其次,您在同一范围内两次使用相同的表名称 tblEventLog。因此,您将需要至少使用一个表关联名称。我知道 tbl- 前缀是 Access 世界中的一个“骄傲点”,但它确实使您的表名变得更长且更难以阅读(IMO)(并且前缀被 ISO 11179 标准 用于数据元素命名:))...那么为什么不始终使用表相关名称呢?

第三,对于 Access(ACE/Jet/其他),IIRC EXISTS 的性能优于 IN,并且 IMO 更易于理解(DISTINCT..INNER JOIN可能会表现得更好,但又更难阅读,IMO)。

这是建议的重写:

SELECT D1.PartNumber, D1.ChangeLevel, 
       D1.ID 
  FROM tblRevRelLog_Detail AS D1
       LEFT OUTER JOIN tblEventLog AS E1
          ON D1.PartNumber = E1.PartNumber 
 WHERE NOT EXISTS (
                   SELECT *
                     FROM tblEventLog AS E2 
                    WHERE E2.EventTypeSelected = 'pn REMOVED From Wrapper' 
                          AND E2.TrackingNumber = D1.RevRelTrackingNumber
                          AND E2.PartNumber = E1.PartNumber
                  ); 

更新:似乎我对 EXIST 给予更好的看法是错误的,所以这里有更多重写可供选择:

SELECT D1.PartNumber, D1.ChangeLevel, 
       D1.ID 
  FROM tblRevRelLog_Detail AS D1
       LEFT OUTER JOIN tblEventLog AS E1
          ON D1.PartNumber = E1.PartNumber 
 WHERE E1.PartNumber NOT IN 
       (
        SELECT E2.PartNumber
          FROM tblEventLog AS E2 
         WHERE E2.EventTypeSelected = 'pn REMOVED From Wrapper' 
               AND E2.TrackingNumber = D1.RevRelTrackingNumber
       ); 

实际上,我正在努力使用此重写Access 的专有连接。我不断收到“灾难性故障”错误。这是重现代码,我哪里出错了?:

Sub grjieopgj()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")
  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"
    With .ActiveConnection

      Dim Sql As String
      Sql = _
      "CREATE TABLE tblRevRelLog_Detail " & vbCr & "(" & vbCr & _
      " PartNumber VARCHAR(20), " & vbCr & " EventTypeSelected VARCHAR(20), " & vbCr & _
      " TrackingNumber VARCHAR(20), " & vbCr & " RevRelTrackingNumber VARCHAR(20), " & vbCr & _
      " ChangeLevel VARCHAR(20), ID VARCHAR(20)" & vbCr & ");"
      .Execute Sql

      Sql = _
      "CREATE TABLE tblEventLog " & vbCr & "(" & vbCr & _
      " PartNumber VARCHAR(20), " & vbCr & " EventTypeSelected VARCHAR(20), " & vbCr & _
      " TrackingNumber VARCHAR(20), " & vbCr & " RevRelTrackingNumber VARCHAR(20), " & vbCr & _
      " ChangeLevel VARCHAR(20), ID VARCHAR(20)" & vbCr & ");"

      Sql = _
      "SELECT DISTINCT D1.PartNumber, D1.ChangeLevel," & _
      " " & vbCr & "       D1.ID " & vbCr & "  FROM (" & vbCr & "        tblRevRelLog_Detail" & _
      " AS D1" & vbCr & "        LEFT OUTER JOIN tblEventLog" & _
      " AS E1" & vbCr & "          ON D1.PartNumber = E1.PartNumber" & vbCr & "" & _
      "       )" & vbCr & "       LEFT OUTER JOIN tblEventLog" & _
      " AS E2" & vbCr & "          ON AND E2.TrackingNumber" & _
      " <> D1.RevRelTrackingNumber" & vbCr & "           " & _
      "  AND E2.PartNumber <> E1.PartNumber" & vbCr & " WHERE" & _
      " E2.EventTypeSelected = 'pn REMOVED From" & _
      " Wrapper';"

      Dim rs
      Set rs = .Execute(Sql)
      MsgBox rs.GetString
    End With
    Set .ActiveConnection = Nothing
  End With
End Sub

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 than IN 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:

SELECT D1.PartNumber, D1.ChangeLevel, 
       D1.ID 
  FROM tblRevRelLog_Detail AS D1
       LEFT OUTER JOIN tblEventLog AS E1
          ON D1.PartNumber = E1.PartNumber 
 WHERE NOT EXISTS (
                   SELECT *
                     FROM tblEventLog AS E2 
                    WHERE E2.EventTypeSelected = 'pn REMOVED From Wrapper' 
                          AND E2.TrackingNumber = D1.RevRelTrackingNumber
                          AND E2.PartNumber = E1.PartNumber
                  ); 

UPDATE: Seems I was wrong about the EXIST giving being better, so here's a couple more rewrite to choose from:

SELECT D1.PartNumber, D1.ChangeLevel, 
       D1.ID 
  FROM tblRevRelLog_Detail AS D1
       LEFT OUTER JOIN tblEventLog AS E1
          ON D1.PartNumber = E1.PartNumber 
 WHERE E1.PartNumber NOT IN 
       (
        SELECT E2.PartNumber
          FROM tblEventLog AS E2 
         WHERE E2.EventTypeSelected = 'pn REMOVED From Wrapper' 
               AND E2.TrackingNumber = D1.RevRelTrackingNumber
       ); 

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?:

Sub grjieopgj()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")
  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"
    With .ActiveConnection

      Dim Sql As String
      Sql = _
      "CREATE TABLE tblRevRelLog_Detail " & vbCr & "(" & vbCr & _
      " PartNumber VARCHAR(20), " & vbCr & " EventTypeSelected VARCHAR(20), " & vbCr & _
      " TrackingNumber VARCHAR(20), " & vbCr & " RevRelTrackingNumber VARCHAR(20), " & vbCr & _
      " ChangeLevel VARCHAR(20), ID VARCHAR(20)" & vbCr & ");"
      .Execute Sql

      Sql = _
      "CREATE TABLE tblEventLog " & vbCr & "(" & vbCr & _
      " PartNumber VARCHAR(20), " & vbCr & " EventTypeSelected VARCHAR(20), " & vbCr & _
      " TrackingNumber VARCHAR(20), " & vbCr & " RevRelTrackingNumber VARCHAR(20), " & vbCr & _
      " ChangeLevel VARCHAR(20), ID VARCHAR(20)" & vbCr & ");"

      Sql = _
      "SELECT DISTINCT D1.PartNumber, D1.ChangeLevel," & _
      " " & vbCr & "       D1.ID " & vbCr & "  FROM (" & vbCr & "        tblRevRelLog_Detail" & _
      " AS D1" & vbCr & "        LEFT OUTER JOIN tblEventLog" & _
      " AS E1" & vbCr & "          ON D1.PartNumber = E1.PartNumber" & vbCr & "" & _
      "       )" & vbCr & "       LEFT OUTER JOIN tblEventLog" & _
      " AS E2" & vbCr & "          ON AND E2.TrackingNumber" & _
      " <> D1.RevRelTrackingNumber" & vbCr & "           " & _
      "  AND E2.PartNumber <> E1.PartNumber" & vbCr & " WHERE" & _
      " E2.EventTypeSelected = 'pn REMOVED From" & _
      " Wrapper';"

      Dim rs
      Set rs = .Execute(Sql)
      MsgBox rs.GetString
    End With
    Set .ActiveConnection = Nothing
  End With
End Sub
伊面 2024-10-08 14:07:52

好像你缺少一个右括号。在 VBA 中的查询末尾添加“)”并尝试。

Seems like you missing a closing bracket. Add ")" at the end of the query in VBA and try.

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