Passing data between two userforms doesn't work - excel

I am working on a userform that inserts data into a DB. There is a modify button that allows to update data but I want allow confirmation of update with a password. Therefore, in the Code of the modify button, I am calling another userform that prompts the user to enter a password. The problem I am having is that I have created a boolean variable that is set to True when the password is correct and to False other. When the password is correct, the code for the modify button continues but the True info is not transmitted to the first userform
I have seen a lot of similar questions here but none of the answers that I have seen has helped me solve my problem. I have created some public variables in both codes but still does not work
Here is the code for the modify button (First userform)
Private Sub modify_Click()
On Error GoTo handler
Dim connec As Object
Dim rst As Object
Dim stringconn As String
Dim sqlupdt As String
Dim modifmat As String
Dim msg
--Some code here
if [condition] then
goto 1
end if
1 password.show
if pwcorr= True then
goto 2
else
msgbox "Incorrect password"
exit sub
end if
3 --Some code here
end sub
and here is the code for the password userform (2nd userform)
Public pwcorr
public sub OkBtn_Click()
Dim pw As String
Dim pwin As String
Dim pwcorr As Boolean
pw= "abcde"
pwin = Me.pwin.Text
If pwin = vbNullString Then
MsgBox "No password was entered", vbCritical
ElseIf pwin = pw Then
Me.pwcorr = True
Unload Me
Else
MsgBox "Password incorrect", vbCritical
End If
End Sub
Whenever I get to the point where I check If pwcorr = True in the first code, the pwcorr is always empty. Don't know why

Related

Excel VBA password protect a command button

I've got a very large and somewhat fragile spreadsheet that I'm working on, and I'd like to limit access to more complicated macros--ones that delete or add data. I'm less worried about security than I am about someone who doesn't know what they're doing deleting unfortunate things. I've used the 'inputbox' password trick, but it becomes slightly annoying to have to keep putting in the password over and over. Is there any way to have the macro 'remember' that I put in the password once, and then reset after I close the sheet, without storing it in a cell somewhere?
Here's the code I'm currently using:
Sub ControlPanel()
Dim PassProtect As Variant
PassProtect = InputBox(Prompt:="Please enter the password to unlock the updater." & vbCrLf & "(Case Sensitive)", Title:="Control Panel")
If PassProtect = vbNullString Then Exit Sub
If PassProtect = "password" Then
ControlPanelForm.Show vbModeless
Else: MsgBox Prompt:="Incorrect password. Please try again.", Buttons:=vbOKOnly
End If
End Sub
Thanks!
You can use a public variable to store a value when the workbook is open, so the code would become:
Public pblnEnteredPassword As Boolean
Sub ControlPanel()
Dim PassProtect As Variant
If pblnEnteredPassword Then GoTo DoStuff
PassProtect = InputBox(Prompt:="Please enter the password to unlock the updater." & vbCrLf & "(Case Sensitive)", Title:="Control Panel")
If PassProtect = vbNullString Then Exit Sub
If PassProtect = "password" Then
pblnEnteredPassword = True
GoTo DoStuff
Else
MsgBox Prompt:="Incorrect password. Please try again.", Buttons:=vbOKOnly
Exit Sub
End If
DoStuff:
ControlPanelForm.Show vbModeless
End Sub
Use a static string var within the CommandButton1_Click sub procedure. Once entered correctly, it will be 'remembered' for the duration of the session.
Option Explicit
Private Sub CommandButton1_Click()
Static pwd As String
try_again:
If pwd <> "thePassword" Then
'password challenge
pwd = InputBox(Prompt:="Please enter the password to unlock the updater." & vbLf & "(Case Sensitive)", _
Title:="Control Panel")
'check if the password challenge was cancelled
If pwd = vbNullString Then Exit Sub
'compare again
GoTo try_again
End If
'all the good code once the password challenge has been passed
Debug.Print "pass"
End Sub

VBA for password does not continue after password was entered in text field

In my spreadsheet I have a button that is linked to the following VBA:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
With this button I trigger a user form which has the following VBA:
Private passwordStatus As Boolean
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
passwordStatus = False
If Password = a Then
MsgBox "Password Correct.", vbInformation
passwordStatus = True
Unload Me
Else
MsgBox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
Function checkPassword() As Boolean
UserForm1.Show
checkPassword = passwordStatus
End Function
The UserForm1 opens without any issue but when I enter a password into the userform the process does not continue. The UserForm1 just stays there.
I cannot find the mistake in my code that blocks the macro from continuing after I entered the password. Can you help me?
To me your code works perfectly, apart from:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
This piece of code will error out.
Change:
Sheet1.Range("C3").Value = 1
To:
Sheets(1).Range("C3").Value = 1
Clicking on a button on the sheet with Button_01 assigned to it will open the userform1.
On the opened userform I can click OptionButton1 and it will run it's click_event
If I have "123" typed in textbox1 it will hide the userform, a wrong password triggers a msgbox to retry.
Passing information between a userform and a regular module is normally done via public variables. You should put the bulk of your code in the regular module, and only use the userform to pass the check back.
Regular module:
Public CorrectPassword As Boolean
Sub Button_01()
CorrectPassword = False
UserForm1.Show
If CorrectPassword = True Then
Sheets(1).Range("C3").Value = 1
Else
Msgbox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
Userform module:
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
If Password = a Then CorrectPassword = True
Unload Me
End Sub

Select ComboBox AfterUpdate Excel VBA

I am writing a script for a UserForm through which users can register to gain access to a Database. The UserForm has three fields, Username, Password and Confirm Password.
I have made it so that, after the user chooses a username, the script runs a VLookUp through the existing usernames to check if the username chosen already exists. If so, a MsgBox pops up, advising the selection of another username. In this case, all three fields of the UserForm are cleared. I would like to make the cursor be positioned in the Username field so that the user can straight away fill in a different username. However, after all fields are cleared, the password filed is the one selected instead. How can I solve this? Thank you for your help.
This is the code I have written:
Private Sub usernameinput_AfterUpdate()
Dim username As String
username = usernameinput.Text
Dim temp As String
On Error Resume Next
temp = WorksheetFunction.VLookup(Me.usernameinput.Value, Range("Usernames"), 1, 0)
If username = temp Then
MsgBox ("The username chosen already exists. Please chose a different username."), vbOKOnly + vbInformation, "Existing Username"
Err.Clear
temp = ""
Me.usernameinput.Value = ""
Me.passwordinput.Value = ""
Me.passwordconfirm.Value = ""
Me.usernameinput.SetFocus
On Error GoTo 0
End If
End Sub
you could act like follows:
in your UserForm code pane:
declare a userform scoped variable
Dim reset As Boolean
insert this Sub
Private Sub HandleReset()
If reset Then
Me.usernameinput.SetFocus
reset = False
End If
End Sub
add all other UserForm controls Enter event handler to call HandleReset() like follows:
Private Sub passwordconfirm_Enter()
HandleReset
End Sub
Private Sub passwordinput_Enter()
HandleReset
End Sub

How can i make keep the value of a variable constant from module to string?

I have been trying to create this password protection type thing in excel VBA for Applied ICT project. So I have created this module which protects all the sheet in the work books:
Public Sub Auto_Open()
Dim ws As Worksheet
Dim psword As String
psword = "Unit 12"
For Each ws In Worksheets
ws.Protect Password:=psword
Next ws
End Sub
Now the thing that i want to do is that whenever someone will open an userform an inputbox asking for the password will appear and if someone enters an invalid password a message box would appear. It will be something like this:
Private Sub UserForm_Activate()
If ActiveSheet.ProtectContents = True Then
x = InputBox("Please type in the password", "Password")
If x = psword Then
Call Module1.Sheets_unlock
Else
Msgbox("The password is invalid")
End
End If
End If
Now the problem is the of password changes to nothing in the UserForm_Activate() sub. I have tried the public psword as string, but it doesn't work. What can i do?
Its where you Dim it.
Dim psword in a standard module above all other subs in the module like:
Public psword As String

Individual Worksheet Passwords

I've used the 'protection worksheet' option in Excel but in addition, I 'd like to ask the user to input a password upon clicking on each sheet. There's a specific password per sheet. If you don't know the password, I don't want the user to be able to modify anything on the sheet. They can print but can't modify anything. I have a password per sheet but the code I'm using isn't working...Maybe it's too basic...Can someone please assist?
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim Msg As String
Dim UserEntry As Variant
Msg = InputBox("What is the password?")
Do
Sheet1.Activate
UserEntry = InputBox(Msg)
If UserEntry = "test" Then Exit Sub
If UserEntry = False Then
Msg = "Invalid Password!"
Msg = Msg & vbNewLine
Msg = Msg & "What is the password?"
Loop
Sheet1.Activate = UserEntry
End Sub
Although it might depend on your version of Excel (I tested in 2007), just right-click the tab and select "Protect Sheet". You can set a password for each sheet seperately.
A user will just go to the Review tab in the ribbon and select "Unprotect sheet" and enter the password.
You really want to avoid setting passwords via VBA since it's really easy to go into the code to find the password.
Here's the basics, you can add back the check for an invalid password.
Private Sub Worksheet_Activate()
Dim Msg As String
Dim UserEntry As String
Msg = InputBox("What is the password?")
If Msg = "test" Then
ActiveSheet.Unprotect ("password")
End If
End Sub
try this in ThisWorkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo WrongPassword
Sh.Unprotect
CleanUp:
Exit Sub
WrongPassword:
MsgBox "Wrong Password"
End Sub

Resources