如何在私有模块中创建公共变量

时间:2018-05-02 19:49:25

标签: ms-access access-vba

基本上它是一个登录表单,我试图保存ID,即Me.CBOUsername.Column(0),但是当我尝试以另一种形式调用它时出现错误。

Option Compare Database
Option Explicit
Public ID_ As String
Private Sub Command4_Click()
    Dim strCBOPass As String
    Dim strPassword As String

    strCBOPass = Me.CBOUsername.Column(1)
    strPassword = Me.txtpassword

    If strCBOPass = strPassword Then
        MsgBox "Login Successful!"
        DoCmd.OpenForm "Form1"
        DoCmd.Close acForm, Me.Name
        ID_ = Me.CBOUsername.Column(0)

    Else
        MsgBox "login Unsuccessful!"
    End If
End Sub

1 个答案:

答案 0 :(得分:3)

用这种方式解释可能会容易一些。

作为全局变量的一个例子:

Public Global_Variable1 as String

Public Sub Procedure1()
    Global_Variable1 = "Test"
End Sub

Public Sub Procedure2()
    Call Procedure1
    MsgBox (Global_Variable1)  'return Test
End Sub

enter image description here

右键单击添加模块,然后使用Insert>模块