【问题标题】:VBA User Form creation- Password MaskingVBA 用户表单创建 - 密码屏蔽
【发布时间】:2016-06-26 22:36:34
【问题描述】:

我开始使用 InputBox 作为 UI 来获取密码以从数据库运行 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

【问题讨论】:

  • 新错误:“不信任对 Visual Basic 项目的编程访问”并指代码的“Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)”。
  • 哎呀,请参阅下面的链接以修复程序错误

标签: vba excel passwords userform


【解决方案1】:

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

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

【讨论】:

  • 我在工作,但遗憾的是防火墙不允许我看到照片。我需要在表单上的单独按钮被打孔后创建此用户表单,然后在表单上的“确定”或“取消”按钮被点击后消失。因此,必须将其创建为由该初始按钮上的冲压引起的宏的一部分。如果输入框可以屏蔽文本会容易得多。
  • 仍然不允许。我回家看看。谢谢!
【解决方案2】:

当它到达第一个插入线命令时,似乎在本节中 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

【讨论】:

  • 我在到达 X = 0 之前就遇到了错误。如果我将 Dim 从注释行中取出并按原样运行,我会收到以下错误:“编译错误:用户-定义类型未定义”。它突出显示“Dim NewTextBox as MSForms.TextBox”作为问题。我认为它不喜欢 MSForms.TextBox 部分
  • 在 VBE 中单击工具/参考,并确保您在 Microsoft Forms 2.0 对象库旁边进行了检查
  • 好的,所以没有检查。现在它移动到“Application.VBE.MainWindow.Visible = False”,因为“对象'_Application'的方法'VBE'失败”。这是我需要包含的另一个库吗?
  • 我不这么认为,但您可能需要这样做:stackoverflow.com/questions/25638344/… ... 我拥有的库是:Visual Basic for Applications、Microsoft Excel 14.0 对象库、Microsoft Office14.0 对象库、OLE 自动化、Microsoft CDO 1.21 和 Microsoft CDO for exchange 2000。如果该链接上的说明无法修复它,我将按该顺序一次启用它们。我无法想象它是最后 3 个中的任何一个。
  • 我看到你提到你在工作防火墙后面看不到图片。修复程序的简而言之是在excel(不是VBE)文件/选项/信任中心/宏设置中并检查“对vba项目模型的信任访问”
【解决方案3】:

在您的 VBA 项目中,您可以添加 UserForm (Insert->UserForm)。将TextBox 从工具箱拖到表单上。然后您可以右键单击新表单并选择“查看代码”

在代码编辑窗口中,您可以包含以下代码:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2021-08-27
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多