1

我对我的问题束手无策。我有一张有正数和负数的表。在将两者拆分为单独的表后,我想使用负数的表从使用 vba 的正数的表中删除记录的第一个实例。下面是我的代码:

Public Sub Suppress_Offset()
Dim MYSQL As String
Dim MYSQL2 As String
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim i As Long
Dim db As DAO.Database

MYSQL2 = "select * From Negative where [QUANTITY]<0"

Set db = CurrentDb()
Set rst = db.OpenRecordset(MYSQL2)

    If Not rst.EOF Then
        rst.MoveFirst
        Do Until rst.EOF
            MYSQL = ""
                MYSQL = MYSQL & "SELECT top 1 *" & vbCrLf
                MYSQL = MYSQL & "FROM Positive " & vbCrLf
                MYSQL = MYSQL & "Where [Unique ID] = "" & rst![Unique ID]) & "" [ACCOUNT #] = " & rst![ACCOUNT #] & " [MRN]= " & rst![MRN] & vbCrLf
                MYSQL = MYSQL & " [PATIENT NAME] = """ & CStr(rst![PATIENT NAME]) & vbCrLf
                MYSQL = MYSQL & " [CPT CODE]=""" & CStr(rst![CPT CODE]) & vbCrLf
                MYSQL = MYSQL & " [DATE OF SERVICE]=" & rst![DATE OF SERVICE] & vbCrLf
                MYSQL = MYSQL & " [ENTRY DATE] = " & rst![ENTRY DATE] & vbCrLf
                MYSQL = MYSQL & " [ATTENDING MD] =" & rst![ATTENDING MD] & vbCrLf
                MYSQL = MYSQL & " [ATTENDING MD NAME] = """ & CStr(rst![ATTENDING MD NAME]) & vbCrLf
                MYSQL = MYSQL & " [DISCHARGE FLOOR] = """ & CStr(rst![DISCHARGE FLOOR]) & vbCrLf
                MYSQL = MYSQL & " [DCFL DESC] = """ & CStr(rst![DCFL DESC]) & vbCrLf
                MYSQL = MYSQL & " [INOUT CODE] = """ & CStr(rst![INOUT CODE]) & vbCrLf
                MYSQL = MYSQL & " [FKEY] = """ & CStr(rst![FKEY]) & vbCrLf
                MYSQL = MYSQL & " [DISCHARGE DEPT] = """ & CStr(rst![DISCHARGE DEPT]) & vbCrLf
                MYSQL = MYSQL & " [DCDP DESC] = """ & CStr(rst![DCDP DESC]) & vbCrLf
                MYSQL = MYSQL & " [FAC/PRO] = """ & CStr(rst![FAC/PRO]) & vbCrLf
                MYSQL = MYSQL & " [QUANTITY]>0 and [QUANTITY]>= " & CStr(-rst![QUANTITY])

                Debug.Print TypeName(MYSQL), MYSQL

                Set rst2 = db.OpenRecordset(MYSQL)
                If Not rst2.EOF Then
                    rst2.Edit
                    rst2![QUANTITY] = rst2![QUANTITY] + rst![QUANTITY]
                    rst2.Update

                    rst.Edit
                    rst![QUANTITY] = 0
                    rst.Update
                End If
                rst2.Close
            rst.MoveNext
        Loop
    End If
        rst.Close
End Sub

谁能看到为什么我在以下位置收到运行时错误 13:Set rst2 = db.OpenRecordset(MYSQL)

请帮忙!谢谢

输出 SQL:

String        SELECT top 1 *
FROM Positive 
Where [Unique ID] = " & rst![Unique ID]) & " [ACCOUNT #] = ########## [MRN]= #######
[PATIENT NAME] = "DOE, BANKSY
[CPT CODE]="#####
[DATE OF SERVICE]=########
[ENTRY DATE] = ########
[ATTENDING MD] =XXXX
[ATTENDING MD NAME] = "BACON, KEVIN
[DISCHARGE FLOOR] = "XXXX
[DCFL DESC] = "RESEARCH/DEVELOPMENT
[INOUT CODE] = "X
[FKEY] = "X-XXXXXXXXXXX
[DISCHARGE DEPT] = "XXXX
[DCDP DESC] = "XXXXXXXX
[FAC/PRO] = "X
[QUANTITY]>0 and [QUANTITY]>= 1
String        SELECT top 1 *
FROM Positive 
Where [Unique ID] = " & rst![Unique ID]) & " [ACCOUNT #] = ########## [MRN]= #######
[PATIENT NAME] = "DOE, BANKSY
[CPT CODE]="#####
[DATE OF SERVICE]=########
[ENTRY DATE] = ########
[ATTENDING MD] =XXXX
[ATTENDING MD NAME] = "BACON, KEVIN
[DISCHARGE FLOOR] = "XXXX
[DCFL DESC] = "RESEARCH/DEVELOPMENT
[INOUT CODE] = "X
[FKEY] = "X-XXXXXXXXXXX
[DISCHARGE DEPT] = "XXXX
[DCDP DESC] = "XXXXXXXX
[FAC/PRO] = "X
[QUANTITY]>0 and [QUANTITY]>= 1  
4

2 回答 2

2

很难从您的代码中分辨出来,但这里有一些建议。

  • 不要使用 + 进行连接。正确的运算符是 &。+ 通常会起作用,但有时它喜欢添加。
  • 如果该字段不是字符串,请不要将变量转换为字符串并将其用引号引起来。保持原样。
  • 拥有超长的代码行使得以后编辑或调试变得更加困难。分开来。可能是这样的:

    var1 = ""
    var1 = var1 & "SELECT * " & vbCrLf
    var1 = var1 & "FROM orders " & vbCrLf
    var1 = var1 & "WHERE shipcity = "" & me!txtshipcity & "" "

字符串也可以用单引号引起来。这有时更容易。

按照这些步骤,我敢打赌你会发现你的错误。

于 2013-04-23T20:45:57.803 回答
1

在代码MYSQL尝试将其与OpenRecordset.

Debug.Print TypeName(MYSQL), MYSQL
Set rst2 = db.OpenRecordset(MYSQL)

您可以Debug.Print在立即窗口中看到输出;Ctrl和+一起去那里g

我的预感是它MYSQL不包含你所期望的。根据变量的构建方式,它甚至可以是 Null 而不是字符串。

它可能是 Null 的原因是因为MYSQL声明如下:

Dim MYSQL, MYSQL2 As String

您可能期望这两个变量都将被声明AS String。但是,只有MYSQL2字符串。 MYSQL是变体。因此该声明与此具有相同的效果:

Dim MYSQL As Variant, MYSQL2 As String

由于MYSQL打算保存一条 SQL 语句,因此声明它AS String不允许它包含 Null。

Dim MYSQL As String, MYSQL2 As String

请注意,当您声明记录集变量时,您会遇到类似的问题。

Dim rst, rst2 As DAO.Recordset ' rst will be Variant

但是,我认为该声明不会导致眼前的问题。不过,在其他情况下,类似的事情可能会导致不受欢迎的意外。

于 2013-04-23T21:34:33.603 回答