Microsoft Access VBA-IF语句始终评估其他表达式

时间:2019-02-16 08:36:44

标签: database ms-access if-statement access-vba

我想使用一些文本作为条件构造一个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

2 个答案:

答案 0 :(得分:0)

尝试一下:

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

答案 1 :(得分:0)

检查没有重复的单个记录中单个字段的值时,通常更容易使用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
相关问题