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

pla*_*ton 0 passwords excel vba userform

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

我找到了这篇文章(http://www.mrexcel.com/archive/VBA/19882a.html),它似乎可以帮助我完成大部分工作,我可以添加一些我知道该怎么做的事情。当我将其放入空白电子表格中时,我得到了来自其中的完整错误列表,并且由于该帖子太旧了,我想也许 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
Run Code Online (Sandbox Code Playgroud)

代码:

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
Run Code Online (Sandbox Code Playgroud)

Ral*_*lph 5

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

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

在此输入图像描述