Microsoft Access VBA:使用表达式组合文本值

时间:2018-05-02 10:13:01

标签: vba ms-access

您好我已经为培训师和成员构建了一个登录表单,他们都可以登录到一个表单。但是,我想确保当培训师或会员登录时,弹出消息框并应该说。 “欢迎= [FirstName]&”“& [LastName]”。所以基本上在欢迎之后会显示成员或培训师全名。

有人可以帮帮我,真的很感激,谢谢。我的代码如下所示。

Option Compare Database

Private Sub Command9_Click()

    Dim isTrainer As Boolean, isMember As Boolean

    isTrainer = DCount("TrainerFirstName", "tbl4_Trainers", _
                 "TrainerEmail = [txtEmail] And TrainerPassword = [txtPassword] ") > 0
    isMember = DCount("MemberFirstName", "tbl1_Members", _
                 "MemberEmail = [txtEmail] And MemberPassword = [txtPassword] ") > 0

    If isTrainer Or isMember Then
        MsgBox ("Welcome & = [TrainerFirstName] & " " & [TrainerLastName] or = [MemberFirstName] & " " & [MemberLastName] ")
        DoCmd.Close acForm, "frm1_Member & Trainer Login"
        If isTrainer Then
            DoCmd.OpenForm "frm3_Main Menu"
        Else
            DoCmd.OpenForm "frm2_Member Class Registration"
        End If

    Else
        MsgBox ("Login Failed")
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

连接变量。如果字段是文本类型,则参数需要撇号分隔符。

isTrainer = DCount("TrainerFirstName", "tbl4_Trainers", _
             "TrainerEmail = '" & Me.txtEmail & "' And TrainerPassword = '" & Me.txtPassword & "'") > 0
isMember = DCount("MemberFirstName", "tbl1_Members", _
             "MemberEmail = '" & Me.txtEmail & "' And MemberPassword = '" & Me.txtPassword & "'") > 0

需要条件来构造欢迎MsgBox文本。不要使用MsgBox函数,只需使用MsgBox OkOnly。

MsgBox "Welcome " & IIf(isTrainer, [TrainerFirstName] & " " & [TrainerLastName], [MemberFirstName] & " " & [MemberLastName])

MsgBox "Login Failed"

建议不要在命名约定中使用空格或标点符号/特殊字符(仅下划线异常)。更好的表格名称是:
frm1_MemberTrainerLogin
frm2_MemberClassRegistration
frm3_MainMenu

答案 1 :(得分:0)

好像你错过了全名。使用 DLookup

Private Sub Command9_Click()

    Dim Trainer As Variant
    Dim Member As Variant

    Trainer = DLookup("[TrainerFirstName] & ' ' & [TrainerLastName]", "tbl4_Trainers", _
                 "TrainerEmail = '" & [txtEmail] & "' And TrainerPassword = '" &[txtPassword] & "'")
    Member = DLookup("[MemberFirstName] & ' ' & [MemberLastName]", "tbl1_Members", _
                 "MemberEmail = '" & [txtEmail] & "' And MemberPassword = '" &[txtPassword] & "'")

    ' To debug:
    MsgBox "Trainer: '" & Trainer & "'" & vbCrLf & "Member: '" & Member & "'"
    ' Note the revised logic: + -> &
    If Not IsNull(Trainer & Member) Then
        MsgBox "Welcome " & Nz(Trainer, Member)
        DoCmd.Close acForm, "frm1_Member & Trainer Login"

        If Not IsNull(Trainer) Then
            DoCmd.OpenForm "frm3_Main Menu"
        Else
            DoCmd.OpenForm "frm2_Member Class Registration"
        End If
    Else
        MsgBox "Login Failed"
    End If

End Sub