简体   繁体   English

案例声明未评估我对MS Access VBA的期望

[英]Case Statement Not Evaluating How I expect it to MS Access VBA

I am trying to figure out why my case statement is not executing as I believe it should. 我试图弄清楚为什么我的案例陈述没有按照我认为的那样执行。 In the below sub, rs1.recordcount evaluates to 0, and the other 4 evaluate to 1. To be sure of this I have printed the results of each recordset.recordcount both before the case statement is executed and also after it has executed. 在下面的子代码中,rs1.recordcount的计算结果为0,其他4的计算结果为1。为确保这一点,我在执行case语句之前和执行之后都打印了每个recordset.recordcount的结果。 However when I step through the code, I get the complete opposite of what I expect. 但是,当我单步执行代码时,得到的结果与我所期望的完全相反。 When I step through it, the second case statement Case rs2.RecordCount = 0 is executed despite the fact that the statement evaluates to equal 1. Any help would be much appreciated. 当我逐步执行该语句时,将执行第二个case语句Case rs2.RecordCount = 0 ,尽管该语句的计算结果等于1。非常感谢您的帮助。 Thank you! 谢谢!

Option Compare Database

Sub CheckDataTypes()

    Set db = CurrentDb
    Set rs1 = db.OpenRecordset("SELECT [Dual Year Carrier Report].EE_ID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].EE_ID) Is Not Null));")
    Set rs2 = db.OpenRecordset("SELECT [Dual Year Carrier Report].PERSON_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].PERSON_OID) Is Not Null));")
    Set rs3 = db.OpenRecordset("SELECT [Dual Year Carrier Report].SPONSOR_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].SPONSOR_OID) Is Not Null));")
    Set rs4 = db.OpenRecordset("SELECT [Dual Year Carrier Report].ZIP_CD FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].ZIP_CD) Is Not Null));")
    Set rs5 = db.OpenRecordset("SELECT [Dual Year Carrier Report].EP_PERSON_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].EP_PERSON_OID) Is Not Null));")

Debug.Print rs1.RecordCount & ", " & rs2.RecordCount & ", " & rs3.RecordCount & ", " & rs4.RecordCount & ", " & rs5.RecordCount



Select Case intRecordCount

Case rs1.RecordCount = 0
    Debug.Print rs1.RecordCount
    MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")

Case rs2.RecordCount = 0
    MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")

Case rs3.RecordCount = 0
    MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")

Case rs4.RecordCount = 0
    MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")

Case rs5.RecordCount = 0
    MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")

End Select
    Debug.Print rs1.RecordCount & ", " & rs2.RecordCount & ", " & rs3.RecordCount & ", " & rs4.RecordCount & ", " & rs5.RecordCount

End Sub

Note that your intRecordCount variable is never assigned a value. 请注意,您的intRecordCount变量永远不会分配值。

Each Case should be followed by a value against which intRecordCount can be compared. 每个Case后面都应跟一个可以与intRecordCount进行比较的值。 In your code example's Case rs1.RecordCount = 0 , the rs1.RecordCount = 0 first evaluates to a Boolean that will be True if there are no records in rs1; 在您的代码示例的Case rs1.RecordCount = 0rs1.RecordCount = 0首先计算为布尔值,如果rs1中没有记录,则该布尔值为True;否则,结果为True。 comparing intRecordCount and a Boolean doesn't make sense. 比较intRecordCount和布尔值没有任何意义。

You could simplify your code as follows: 您可以按如下方式简化代码:

If (rs1.RecordCount = 0) Or (rs2.RecordCount = 0) Or (rs3.RecordCount = 0) Or (rs4.RecordCount = 0) Or (rs5.RecordCount = 0) Then
    MsgBox "Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type"
End If

1) use Option Explicit 1)使用Option Explicit

2) declare variables 2)声明变量

3) vba Case statement doesn't work like that. 3)vba Case语句不能那样工作。 Your statement looks a little bit like SQL Server syntax and a lot wrong. 您的语句看起来有点像SQL Server语法,并且有很多错误。

4) trick for young players: if you want to know how many records in your recordset then you need to access all the records. 4)给年轻玩家的技巧:如果您想知道记录集中有多少条记录,则需要访问所有记录。

5) [not shown] consider rewriting your SQL statements as "select count(*) as recCount from [Dual Year Carrier Report] where ..." and checking the value of rs1("recCount") , rs2("recCount"), etc. 5)[未显示]考虑将您的SQL语句重写为“从[Dual Year Carrier Report]其中...”中选择“ recCount(*)作为recCount”,并检查rs1(“ recCount”),rs2(“ recCount”)的值等

Option Compare Database
Option Explicit

Sub CheckDataTypes()

    Dim db As DAO.Database
    Dim rs1 As DAO.Recordset, rs2 As DAO.Recordset, rs3 As DAO.Recordset, rs4 As DAO.Recordset, rs5 As DAO.Recordset

    Set db = CurrentDb

    Set rs1 = db.OpenRecordset("SELECT [Dual Year Carrier Report].EE_ID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].EE_ID) Is Not Null));")
    If Not rs1.EOF Then rs1.MoveLast    ' required to force MS Access to load records to recordset NOW
    Set rs2 = db.OpenRecordset("SELECT [Dual Year Carrier Report].PERSON_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].PERSON_OID) Is Not Null));")
    If Not rs2.EOF Then rs2.MoveLast
    Set rs3 = db.OpenRecordset("SELECT [Dual Year Carrier Report].SPONSOR_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].SPONSOR_OID) Is Not Null));")
    If Not rs3.EOF Then rs3.MoveLast
    Set rs4 = db.OpenRecordset("SELECT [Dual Year Carrier Report].ZIP_CD FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].ZIP_CD) Is Not Null));")
    If Not rs4.EOF Then rs4.MoveLast
    Set rs5 = db.OpenRecordset("SELECT [Dual Year Carrier Report].EP_PERSON_OID FROM [Dual Year Carrier Report] WHERE ((([Dual Year Carrier Report].EP_PERSON_OID) Is Not Null));")
    If Not rs5.EOF Then rs5.MoveLast

    Debug.Print rs1.RecordCount & ", " & rs2.RecordCount & ", " & rs3.RecordCount & ", " & rs4.RecordCount & ", " & rs5.RecordCount

    If rs1.RecordCount = 0 Then
        Debug.Print rs1.RecordCount
        MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")
    ElseIf rs2.RecordCount = 0 Then
        MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")
    ElseIf rs3.RecordCount = 0 Then
        MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")
    ElseIf rs4.RecordCount = 0 Then
        MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")
    ElseIf rs5.RecordCount = 0 Then
        MsgBox ("Please import Dual Year Carrier Report again and make sure to change the proper fields to text data type")
    End If

    Debug.Print rs1.RecordCount & ", " & rs2.RecordCount & ", " & rs3.RecordCount & ", " & rs4.RecordCount & ", " & rs5.RecordCount

End Sub

Hope this helps, GraemeR 希望这会有所帮助,GraemeR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM