如何将此功能从InputBox更改为UserForm?

时间:2019-04-03 22:51:33

标签: excel vba

我正在尝试更改Testing()函数以利用UserForm代替应用程序输入框。我已经用用户名和密码输入以及一个“登录”按钮制作了用户窗体,但是我不确定表单上“登录”按钮的代码是什么样子。

我已经包含了它引用的Login函数和相关的声明。

Public Sub Testing()
    Dim User As String
    Dim Pass As String
    User = Application.InputBox("Enter Your Username")
    Pass = Application.InputBox("Enter Your Password")
    Const AccountID = "Cerberus"
    Dim Cerberus As New Class_Cerberus
    Call Cerberus.Login(AccountID, User, Pass)

    Range("A1") = Cerberus.GUID

    Set Cerberus = Nothing

End Sub
Private Online_ As Boolean
Private Username_ As String
Private Password_ As String
Private AccountID_ As String
Private GUID_ As String
Private GUID_Timestamp As Date

Public Function Login(Optional ByRef AccountID As String, Optional ByRef Username As String, Optional ByRef Password As String) As Boolean
    If Not Online_ Then 'Verify connectivity before proceeding
        If Not CheckOnline Then Exit Function
    End If
    'If any credentials are given at this time, store them for later use and proceed with login
    If Not IsMissing(AccountID) Then AccountID_ = AccountID
    If Not IsMissing(Username) Then Username_ = Username
    If Not IsMissing(Password) Then Password_ = Password

    If Not (Len(GUID_) = 0 Or GUID_Timestamp < (Now() - TimeSerial(8, 0, 0))) Then Login = True
    Dim RawJSON As String: RawJSON = GetHTTP(BASE_URL & "?fct=login&accountid=" & AccountID_ & "&username=" & Username_ & "&password=" & Password_ & "&browserinfo=NULL&format=JSON")
    Dim JSON As Object: Set JSON = JsonConverter.ParseJson(RawJSON)

    Login = JSON("status") = "ok"
    GUID_ = JSON("results")(1)("GUID")
    GUID_Timestamp = GetDateTime(JSON("results")(1)("DATE_ALIVE"))
    Set JSON = Nothing
End Function

1 个答案:

答案 0 :(得分:0)

因此,您的登录按钮将是一个CommandButton,您可以将其从Toolbox上拖动。

enter image description here

您说您已经制作了“用户/密码输入”框,因此CommandButton代码将是这样。

Public Sub Testing()
    Dim User As String
    Dim Pass As String
    User = TextBox1.Value 'Changed from Application.InputBox("Enter Your Username")
    Pass = TextBox2.Value
    Const AccountID = "Cerberus"
    Dim Cerberus As New Class_Cerberus
    Call Cerberus.Login(AccountID, User, Pass)

    Range("A1") = Cerberus.GUID

    Set Cerberus = Nothing

    Unload Me 'This closes the Userform, perhaps add a check If Login = True then Unload Me

End Sub

注意:您可以通过PasswordChar = *通过输入字段的属性隐藏PWD。

enter image description here