【问题标题】:Microsoft Access VBA - IF statement always evaluates Else expressionMicrosoft Access VBA - IF 语句总是计算 Else 表达式
【发布时间】:2019-07-10 07:17:43
【问题描述】:

我想使用一些文本作为条件构造一个if 语句。

在我的字段Status_anggota 中有一个组合框,其值为"Active""Not active"

我的问题是它不会得到"Active" 的值,所以它只显示错误的声明:

Set db = CurrentDb()
Set rs = db.OpenRecordset("Select Status_anggota from Tbl_anggota Where Kode_anggota='" & Text2 & "'")
If rs.RecordCount = "Active" Then
    MsgBox "Status is active", vbInformation
Else
    MsgBox "Status is not active", vbInformation
End If

【问题讨论】:

  • RecordCount 是一个数字

标签: database ms-access if-statement vba


【解决方案1】:

在没有迭代的情况下检查单个记录中单个字段保存的值时,使用DLookup 函数通常更简单,例如:

If DLookup("Status_anggota", "Tbl_anggota", "Kode_anggota='" & Text2 & "'") = "Active" Then
    MsgBox "Status is active", vbInformation
Else
    MsgBox "Status is not active", vbInformation
End If

【讨论】:

    【解决方案2】:

    试试这个:

    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset("Select Status_anggota from Tbl_anggota Where Kode_anggota='" & Text2 & "'")
    
    If rs!Status_anggota.Value = "Active" Then
        MsgBox "Status is active", vbInformation
    Else
        MsgBox "Status is not active", vbInformation
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2017-09-02
      相关资源
      最近更新 更多