-1

我为我的数据库创建了一个登录屏幕,它可以工作。

我想将一个变量传递给一个文本框,一旦你被验证,它将出现在“主菜单”表单上。

当您单击“登录”时,这是我的代码。

'Login button submit, validate, welcome message & main menu navigation
Private Sub cmd_login___Click()

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim strSQL As String
 
    'query to check if login details are correct
    strSQL = "SELECT Name FROM LoginQuery WHERE Username = """ & Me.txt_username.Value & """ AND Password = """ & Me.txt_password.Value & """"
  
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSQL)
    If rst.EOF Then
        MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error"
        Me.txt_username.SetFocus
    Else
        MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful"
        DoCmd.Close acForm, "frm_login", acSaveYes
        DoCmd.Close
        DoCmd.OpenForm "MainMenu"
    End If
 
    Set db = Nothing
    Set rst = Nothing

此行在屏幕上显示一个提示,提示使用“rst.Fields(0).Value”变量登录成功,该变量是用户的全名,但我希望按下更少的窗口和按钮,而是将此变量传递给新的带有文本框的空白表单。

MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful"

我将MainMenu表单上的文本框命名为txt_welcome

我尝试在 IF 语句中附加以下内容:

Dim name As String
name = "SELECT Name FROM LoginQuery WHERE Username = """ & Me.txt_username.Value & """ AND Password = """ & Me.txt_password.Value & """"
[MainMenu]![txt_welcome].Value = name

[MainMenu]![txt_welcome].Value = "dsfadsf"

MainMenu!txt_welcome.value = "justdisplaysomethingplz"
4

1 回答 1

1

您可以通过方法的OpenArgs参数传递它DoCmd.OpenForm

DoCmd.OpenForm "MainMenu", OpenArgs:="Something"

然后,在 MainMenu 表单上,在表单加载时读取其值:

Private Sub Form_Load()
    If Not IsNull(OpenArgs) Then Debug.Print OpenArgs
End Sub
于 2021-11-09T17:24:59.380 回答