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

问题描述 投票:0回答:2

我想用一些文本作为条件构造一个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
database ms-access if-statement access-vba
2个回答
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

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
© www.soinside.com 2019 - 2024. All rights reserved.