VBA 用户表单创建 - 密码屏蔽

VBA User Form creation- Password Masking

我开始使用 InputBox 作为 UI 从数据库中获取 运行 SQL 的密码。我发现 InputBoxes 没有屏蔽输入字符的能力(例如 *******)。然后我发现我需要使用用户表单来构建带有密码屏蔽字段的文本框。我以前从未这样做过。

我发现了这个 post (http://www.mrexcel.com/archive/VBA/19882a.html),它似乎可以帮助我完成大部分工作,我可以添加一些我知道如何做的事情。当我将它放入一个空白的电子表格中时,我得到了一个完整的错误列表,并且由于 post 太旧了,我想也许 VBA 有一些更新使这段代码过时了。是否有人能够对其提出批评以使其发挥作用?我将列出我在尝试修复它时遇到的一些错误以及代码。

Errors:
-Statement invalid Type block
-User-defined type not defined
-Method 'VBE' of object'_Application' failed
-Method 'VBProject' of object'_Workbook' failed
-Object required

代码:

Option Explicit
Public OK As Boolean
Public Const sMyPassWord As String = "test"

Function GetPassWord(Title As String)
'---------------------------------------------------------------------------    ------------
' Procedure : GetPassWord
' DateTime : 4/02/02 19:04
' Author : Ivan F Moala
' Purpose : Creates a Dynamic UF to Test for aPassword
' : so there is no need to create one.
'---------------------------------------------------------------------------    ------------
Dim TempForm
Dim NewTextBox As MSForms.TextBox
Dim NewCommandButton1 As MSForms.CommandButton
Dim NewCommandButton2 As MSForms.CommandButton
Dim x As Integer

' Hide VBE window to prevent screen flashing
Application.VBE.MainWindow.Visible = False

' Create a Temp UserForm
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)

' Add a TextBox
Set NewTextBox = TempForm.Designer.Controls.Add("forms.textbox.1")
With NewTextBox
 .PasswordChar = "*"
 .Width = 140
 .Height = 20
 .Left = 48
 .Top = 18
End With

' Add the OK button
Set NewCommandButton1 = TempForm.Designer.Controls.Add    ("forms.CommandButton.1")
With NewCommandButton1
 .Caption = "OK"
 .Height = 18
 .Width = 66
 .Left = 126
 .Top = 66
End With

' Add the Cancel button
Set NewCommandButton2 = TempForm.Designer.Controls.Add    ("forms.CommandButton.1")
With NewCommandButton2
 .Caption = "Cancel"
 .Height = 18
 .Width = 66
 .Left = 30
 .Top = 66
End With

' Add event-handler subs for the CommandButtons & Userform
With TempForm.CodeModule
 x = .CountOfLines
 .insertlines x + 0, "Sub CommandButton2_Click()"
 .insertlines x + 1, "OK = False: Unload Me"
 .insertlines x + 2, "End Sub"

.insertlines x + 3, "Sub CommandButton1_Click()"
 .insertlines x + 4, "If TextBox1 = sMyPassWord Then OK = True: Unload Me"
 .insertlines x + 5, "End Sub"

.insertlines x + 6, "Private Sub UserForm_Initialize()"
 .insertlines x + 7, "Application.EnableCancelKey = xlErrorHandler"
 .insertlines x + 8, "End Sub"
End With

' Adjust the form
With TempForm
 .Properties("Caption") = Title
 .Properties("Width") = 240
 .Properties("Height") = 120
 NewCommandButton1.Left = 46
 NewCommandButton2.Left = 126
End With

' Show the form
VBA.UserForms.Add(TempForm.Name).Show

' Delete the form
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm

' Pass the Variable back to the calling procedure
GetPassWord = OK

End Function

Sub ThisIsHowToUseIt()
'>>> This is the Main line <<<<br>Dim OKToProceed As Variant
OKToProceed = GetPassWord("Password Entry")
If OKToProceed = False Then End
'>>>-----------------------<<<<p>'>>> Your routine goes here     <<<<p>MsgBox "My routine is running now"

End Sub

当它到达第一个 insertlines 命令时,它似乎在本节中遇到 x = 0 的问题,在该命令中将 0 添加到 X,然后尝试在第 0 行插入行。如果您递增所有值将它们加到 x 上 1,以便它从第 1 行开始播放,效果很好。

' Add event-handler subs for the CommandButtons & Userform
With TempForm.CodeModule
 x = .CountOfLines
 .insertlines x + 1, "Sub CommandButton2_Click()"
 .insertlines x + 2, "OK = False: Unload Me"
 .insertlines x + 3, "End Sub"
 .insertlines x + 4, "Sub CommandButton1_Click()"
 .insertlines x + 5, "If TextBox1 = sMyPassWord Then OK = True: Unload Me"
 .insertlines x + 6, "End Sub"
 .insertlines x + 7, "Private Sub UserForm_Initialize()"
 .insertlines x + 8, "Application.EnableCancelKey = xlErrorHandler"
 .insertlines x + 9, "End Sub"
End With

此外,请确保在您的 Sub 中将 Dim 语句移至新行,这样它就不会像您在代码示例中那样成为注释的一部分。

Sub ThisIsHowToUseIt()
'>>> This is the Main line <<<<br>
Dim OKToProceed As Variant
OKToProceed = GetPassWord("Password Entry")
If OKToProceed = False Then End
'>>>-----------------------<<<<p>'>>> Your routine goes here     <<<<p>MsgBox "My routine is running now"

End Sub

如果您真的只关心在 UserForm 上的 TextBox 中隐藏密码(在您键入时),那么您可以使用内置功能。

实际上有 属性 可以为任何 TextBox 设置密码屏蔽字符。虽然字符被设置的字符屏蔽,但仍然可以引用 TextBox 并检查其值,并且 UserForm1.TextBox1.Value 将 return 未屏蔽的字符串(在 VBA 中)。查看下面的屏幕截图,如果这能回答您的问题,请告诉我。

在您的 VBA 项目中,您可以添加一个 UserForm(插入 -> 用户窗体)。将 TextBox 从工具箱拖到窗体上。然后您可以右键单击新表单并 select 'View Code'

在代码编辑中 window 您可以包含以下代码:

Private Sub UserForm_Initialize()
    Me.TextBox1.PasswordChar = "*"
End Sub

当您 运行 表单时,您将看到您键入的每个字符 *