我开始使用InputBox作为UI来获取密码以从DB运行SQL。我发现InputBoxes不具备屏蔽输入字符的能力(例如*******)。然后我发现我需要使用用户表单来构建一个带有密码屏蔽字段的文本框。我以前从未这样做过。
我发现这篇文章(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
代码:
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
答案 0 :(得分:3)
如果您真的只关心在TextBox
的{{1}}中屏蔽密码(当您输入时),那么您可以使用内置功能。</ p>
实际上有一个属性可以为任何UserForm
设置密码屏蔽字符。当字符被设置字符掩盖时,TextBox
仍然可以被引用并检查其值,TextBox
将返回未屏蔽的字符串(在VBA中)。看看下面的截图,让我知道这是否能回答你的问题。
答案 1 :(得分:1)
当它到达第一个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
答案 2 :(得分:1)
在您的VBA项目中,您可以添加UserForm
(插入 - >用户窗体)。将TextBox
从工具箱拖到表单上。然后,您可以右键单击新表单,然后选择“查看代码”
在代码编辑窗口中,您可以包含以下代码:
Private Sub UserForm_Initialize()
Me.TextBox1.PasswordChar = "*"
End Sub
当您运行表单时,您会在键入的每个字符中看到*
。