禁止访问vba中的已取消记录会生成错误

时间:2013-04-23 19:56:10

标签: ms-access-2007 access-vba

我的问题以我的问题告终。我有一张正负数量的表格。在将两者分成单独的表之后,我想使用具有负数量的表来使用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:     设置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  

2 个答案:

答案 0 :(得分:2)

很难从您的代码中了解到,但这里有一些建议。

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

    var1 =“”
    var1 = var1&amp; “SELECT *”&amp; vbCrLf
    var1 = var1&amp; “来自订单”&amp; vbCrLf
    var1 = var1&amp; “WHERE shipcity =”“&amp; me!txtshipcity&amp;”“”

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

请按照以下步骤操作,我打赌您会发现错误。

答案 1 :(得分: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是String。 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

但是,我不认为该声明有助于解决当前的问题。但是,类似的东西会在其他情况下引起不受欢迎的惊喜。