在 MS ACCESS 中,是否可以执行:DoCmd.OpenForm "Dlookup...."?

in MS ACCESS, is it possible to do: DoCmd.OpenForm "Dlookup...."?

微软访问 2013 我有一个 tblUser table,其中包含以下数据(用户名/密码/StartForm) 我有一个登录系统,用户可以在其中输入名为 txtLogin 的表单字段他的用户名。

用户名和密码匹配后,我需要为每个用户(取决于他在公司的职能)打开一个特定的表格。 我有这段代码,但无法找出问题所在。

DoCmd.OpenForm DLookup("StartForm", "tblUser","[UserName]='" & txtLogin & "';")

我才刚开始编程,我想学习,而不是copy/paste代码,所以如果你能给我一个简单的解释,我非常感谢。

谢谢

DoCmd.OpenForm 的第一个参数是表单名称。要打开特定参数的表单,您需要使用 第 4 个参数是 WhereCondition.

这里不需要Dlookup函数。它用于 return 来自单个记录的单个列,其中源列是第一个参数,源 table 是第二个参数。它通过搜索条件,即第三个参数,知道要抓取什么记录。

您进行此设置的方式是要求 DoCmd.OpenForm 打开名为 [the result of your DLookup call] 的表单,且未应用过滤器。

你想要的更像这样

DoCmd.OpenForm NameOfYourUserForm, acNormal, , "[UserName]='" & txtLogin & "'"

我能够解决以下问题。 我为要调用的表单 (nomeForm) 创建了一个变量,并使用 Dlookup 为每个用户找到合适的表单。

谢谢

Private Sub cmdLogin_Click()

     Dim rst As Recordset
     Dim nomeUsuario As String

    If IsNull(txtLogin) Or IsNull(txtSenha) Then
    MsgBox "Preencha o login e senha"
    Exit Sub
    End If

    nomeUsuario = txtLogin

    Set rst = CurrentDb.OpenRecordset("SELECT * FROM tblUser WHERE UserName = '" & txtLogin & "' AND Password = '" & txtSenha & "';")

    If rst.RecordCount = 1 Then

    bcansafelyclose = True
    DoCmd.Close

    Dim nomeForm As String
    nomeForm = DLookup("Start", "tblUser", "UserName = '" & nomeUsuario & "'")

    DoCmd.OpenForm nomeForm

    Else

    MsgBox "Login ou senha incorretos"
    bcansafelyclose = False

    End If

    rst.Close

    End Sub