【问题标题】:How to pass a string from one vba form to another如何将字符串从一个 vba 表单传递到另一个
【发布时间】:2019-10-03 20:59:22
【问题描述】:

我正在尝试将字符串值从一种形式传递到另一种形式。我的第一个表单是“frmLogin”,其中包含用户 ID“strId”和密码“strPassword”(它只是一个常规登录表单)。我试图打开并将字符串传递给的表单是“frmProfile1”。从 frmProfile1,Profile1 用户将文档分配给在 Access 中存储为记录的 Profile2 用户。这些记录将包含分配文档的 Profile1 用户的 ID,因此将 strId 的值从 frmLogin 传递到 frmProfile1 非常重要。

'Project: 
'Form: Login Form (frmLogin)
'Author: Nathan Willett
'Created: 9/10/19
'Current Version: 1.0 - 9/10/19

Option Compare Database
Option Explicit

Public strProfile, strId, strPassword As String

'On 'Button Ok' click, perform the following
Public Sub btnLogin_Click()

    'If ID Login Box is left blank, display "Please enter Login ID"
    If IsNull(Me.txtId) Then
        MsgBox "Please enter Login ID", vbInformation, "Login Error"
        Me.txtId.SetFocus
        Exit Sub
    'If Password Box is left blank, display "Please enter Password"
    ElseIf IsNull(Me.txtPassword) Then
        MsgBox "Please enter Password", vbInformation, "Login Error"
        Me.txtPassword.SetFocus
        Exit Sub
    End If

    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("tblUserList", dbOpenSnapshot, dbReadOnly)
    rs.FindFirst "fldId='" & Me.txtId & "'"

    'If txtID value does not equal fldId, prompt the user
    If rs.NoMatch = True Then
        MsgBox "Incorrect Login ID. Please try again.", vbInformation, "Login Error"
        Me.txtId.SetFocus
        Exit Sub
    End If

    'If txtPassword value does not equal fldPassword, prompt the user
    If rs!fldPassword <> Me.txtPassword Then
        MsgBox "Incorrect Password. Please try again.", vbInformation, "Login Error"
        Me.txtId.SetFocus
        Exit Sub
    End If

    'Look up the users profile version from tblUserList
    strProfile = DLookup("fldPosId", "tblUserList", "fldId='" & Me.txtId.Value & "'")
    strId = rs!fldId
    strPassword = rs!fldPassword

    'Open the corresponding profile form
    Select Case strProfile
        Case "Profile1"
            DoCmd.OpenForm "frmProfile1"
        Case "Profile2"
            DoCmd.OpenForm "frmProfile2"
        Case "Profile3"
            DoCmd.OpenForm "frmProfile3"
        Case "Profile5"
            DoCmd.OpenForm "frmProfile4"
    End Select
    DoCmd.Close acForm, "frmLogin"
End Sub
'On 'Button Cancel' click, close the program
Private Sub btnCancel_Click()
    DoCmd.Close acForm, "frmLogin"
End Sub
'Project:
'Form: frmProfile1
'Author: Nathan Willett
'Created: 9/21/19
'Current Version: 1.0 9/21/19

Option Compare Database
Option Explicit

Dim db As Database
Dim rs As Recordset

Private Sub Form_Open(Cancel As Integer)
    Text468.Text = frmLogin!strId
End Sub

【问题讨论】:

  • 密码不属于数据库中的纯文本。 Hash+salt 密码,存储加盐的哈希。然后哈希+盐(相同的盐值)输入,并与存储的值进行比较。如果它们匹配,则密码很好。比较哈希,而不是纯文本密码。
  • 我在字段上使用密码输入掩码。可以吗?
  • 我说的是数据库中的内容。如果您以纯文本形式存储密码,则任何可以访问数据库表的人都可以伪造任何凭据,即可能没有任何密码,您不能 100% 确定记录的凭据是凭据的人宣称。输入掩码很棒,但解决了在您输入密码时有人从背后偷看您的安全问题 - 它不会使系统真正安全。
  • 普通用户无法访问这些数据库,但如果这是我需要做的事情,我可能会在稍后添加。我会做一些研究。
  • 专业提示:安全性是一项功能,而不是附加组件。您的“不正常”用户 /devs 可以很容易地访问任何人的凭据。根据此应用程序正在做什么以及需要保护它的原因,您不希望允许这样做。这就是每一次安全漏洞的发生方式:开发人员认为他们可以在以后保护事情。

标签: vba ms-access


【解决方案1】:

通常,要引用表单上的控件,您会:

 forms!yourFormName.ControlName.Value

或参考子窗体上的控件:

forms!yourFormName.yourSubFormName.Form.ControlName.Value

要打开一个表单并在其上设置一个值,您可以:

DoCmd.OpenForm "yourFormName"
Forms!YourFormName.ControlName.Value = Forms!FormOpeningOtherFormName.Control.Value

这有帮助吗?

对于您的场景,您实际上需要这样做:

Select Case strProfile
    Case "Profile1"
        DoCmd.OpenForm "frmProfile1"
        Forms!frmProfile1.Text468.Text = strId
    Case "frmProfile2"
        DoCmd.OpenForm "frmProfile2"
        Forms!frmProfile2.Text468.Text = strId
    Case "Profile3"
        DoCmd.OpenForm "frmProfile3"
        Forms!frmProfile3.Text468.Text = strId
    Case "Profile4"
        DoCmd.OpenForm "frmProfile4"
        Forms!frmProfile4.Text468.Text = strId
End Select

【讨论】:

  • 我也尝试通过将其作为参数传递给 openargs 来执行此操作,但这也不起作用......
  • @NathanWillett 您似乎错误地引用了您的公共变量
  • @NathanWillett 查看更新的答案,这当然是假设每个表单在另一个选项卡的属性窗口中具有相同名称的相同文本框
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 2011-10-19
  • 1970-01-01
  • 2015-03-13
  • 2013-05-19
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多