How to implement a password in a button - vba

I have a button to lock unlock a form with a subform. To enable the button I would like to set a password with a msgbox. But I have an issue in my code.
I first use the code to show the password msgbox with conditional, if the password is right the user locks/unlocks the forms. But I have end with statement in wrong place.
Private Sub bloquear_Click()
With Me.bloquear
Dim strPasswd
strPasswd = InputBox("Enter Password", "Restricted Form")
If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInformation, "Required Data"
Exit Sub
End If
If strPasswd = "Password" Then
If .Caption = "Unlock" Then
Me.AllowAdditions = True
Me.AllowEdits = True
Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = True
Me.CONSULTA_PRODUCTOS.Form.AllowEdits = True
.Caption = "Lock"
Else
Me.AllowAdditions = False
Me.AllowEdits = False
Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = False
Me.CONSULTA_PRODUCTOS.Form.AllowEdits = False
.Caption = "Unlock"
Me.Refresh
End If
End With
Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If
End Sub
The msgbox shows
Compile error, end with without with
I don't know what is wrong because the with is there.
Thanks

Related

how I validating this

Private Sub Command261_Click()
If IsNull(CompanyName.Value Or ID.Value Or Date_Received.Value Or Clerk_Revceived) = "" Then
MsgBox "Please Complete Company Name"
cancel = False
Else
DoCmd.Close
End If
End Sub
Probably something like this:
Private Sub Command261_Click()
If IsNull(CompanyName.Value + ID.Value + Date_Received.Value + Clerk_Revceived.Value) Then
MsgBox "Please Complete Company Info"
' Cancel = True ' Nothing to cancel here.
Else
DoCmd.Close
End If
End Sub

How to Set Different Privileges for Different Users of an Excel File

maybe you can help me with this issue:
I am trying to set for one excel sheet different kind of privileges.
For example, there will be an Admin with all right and a guest, how is only allowed the change an range of cells.
I started to setup 2 different kind of logins, the one for the admin is working well however the one for the guest not at all.
What am I doing wrong here?
Ps: I just started to learn VBA ☺
Private Sub CommandButton1_Click()
Dim objTargetWorksheet As Worksheet
'Gast
If (TextBox1.Value = "Gast" And TextBox2.Value = "123") _
Or (TextBox1.Value = "Amy" And TextBox2.Value = "345") _
Or (TextBox1.Value = "Paul" And TextBox2.Value = "456") Then
Me.Hide: Application.Visible = True
For Each objTargetWorksheet In ActiveWorkbook.Worksheets
If objTargetWorksheet.Name = TextBox1.Value Then
Range("K3:K50").Locked = True
ActiveSheet.Protect Password:="12345", Contents:=True
Else
Range("K3:K50").Locked = True
ActiveSheet.Protect Password:="12345", Contents:=True
End If
Next
'Admin
ElseIf TextBox1.Value = "Admin" Then
If TextBox2.Value = "" Then
MsgBox "Please Input the Password"
ElseIf TextBox2.Value = "123" Then
Me.Hide: Application.Visible = True
Else
MsgBox "Please Input the right User Name and the right Password"
End If
Else
MsgBox "Please input the right user name and the right password"
End If
End Sub
Private Sub CommandButton2_Click()
ThisWorkbook.Application.Quit
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Application.Quit
End Sub
You need to reference the sheet you are working on.
For Each objTargetWorksheet In ActiveWorkbook.Worksheets
If objTargetWorksheet.Name = TextBox1.Value Then
objTargetWorksheet.Range("K3:K50").Locked = True
objTargetWorksheet.Protect Password:="12345", Contents:=True
Else
objTargetWorksheet.Range("K3:K50").Locked = True
objTargetWorksheet.Protect Password:="12345", Contents:=True
End If
Next
UPDATE: Cells are locked by default so you actually have to unlock them before protecting the sheet. Try this:
For Each objTargetWorksheet In ActiveWorkbook.Worksheets
If objTargetWorksheet.Name = TextBox1.Value Then
objTargetWorksheet.Cells.Locked = False
objTargetWorksheet.Range("K3:K50").Locked = True
objTargetWorksheet.Protect Password:="12345", Contents:=True
Else
objTargetWorksheet.Cells.Locked = False
objTargetWorksheet.Range("K3:K50").Locked = True
objTargetWorksheet.Protect Password:="12345", Contents:=True
End If
Next

Excel VBA Forms Error Message loop

i currently have a problem with a simple login form in excel (VBA), when having an error, continuing and having another error it still gives me two more MsgBoxes with errors but with the "Unload Me" and "Goto Ende" it should close itself completely.
Any guesses why this isn't working? I know this is very basic and probably very redundant, but it should still work.
Public Name As Variant
Public Password As Variant
Private Sub Btn_Register_Cancel_Click()
Unload Me
End Sub
Private Sub Btn_Register_Register_Click()
Start:
Dim Error As Integer
Error = 0
Name = Tbx_Register_Name.Value
Password = Tbx_Register_Password.Value
'Check for Name, Password, Password2 if empty
If Tbx_Register_Name.Value = "" Then
Error = MsgBox("Please enter a username.", _
vbRetryCancel, "Error")
If Error = 2 Then
Unload Me
GoTo Ende
Else
Application.ScreenUpdating = False
Register.Hide
Register.Show
Application.ScreenUpdating = True
GoTo Start
End If
ElseIf Tbx_Register_Password.Value = "" Then
Error = MsgBox("Please enter a password.", _
vbRetryCancel, "Error")
If Error = 2 Then
Unload Me
GoTo Ende
Else
Application.ScreenUpdating = False
Register.Hide
Register.Show
Application.ScreenUpdating = True
GoTo Start
End If
ElseIf Tbx_Register_Password2.Value = "" Then
Error = MsgBox("This field cannot be empty.", _
vbRetryCancel, "Error")
If Error = 2 Then
Unload Me
GoTo Ende
Else
Application.ScreenUpdating = False
Register.Hide
Register.Show
Application.ScreenUpdating = True
GoTo Start
End If
End If
With Workbooks("General Makro.xlsx").Worksheets("User")
'Check for Username match in registration list
For i = 1 To 100
If .Cells(i, 1).Value = Name Then
Error = MsgBox("This username is already taken.", _
vbRetryCancel, "Error")
If Error = 2 Then
Unload Me
i = 100
GoTo Ende
Else
Application.ScreenUpdating = False
Register.Hide
Register.Show
Application.ScreenUpdating = True
GoTo Start
End If
End If
Next i
End With
'Check for the passwords to match
If Tbx_Register_Password.Value = Tbx_Register_Password2.Value Then
With Workbooks("General Makro.xlsx").Worksheets("User")
For i = 1 To 100
If .Cells(i, 1) = "" Then
.Cells(i, 1).Value = Name
.Cells(i, 2).Value = Password
Tbx_Register_Password.Value = ""
Tbx_Register_Password2.Value = ""
Application.ScreenUpdating = False
Register.Hide
Login.Show
Tbx_Login_Name.Value = .Cells(i, 1).Value
Tbx_Login_Password.Value = .Cells(i, 2).Value
Application.ScreenUpdating = True
i = 100
GoTo Ende
End If
Next i
End With
Else
Error = MsgBox("The passwords have to match!", vbRetryCancel, "Error")
If Error = 2 Then
Unload Me
GoTo Ende
Else
Application.ScreenUpdating = False
Register.Hide
Register.Show
Application.ScreenUpdating = True
GoTo Start
End If
End If
Ende:
End Sub
Edit: I Actually Tried to do the 2nd UserForm for the login, and i happen to get the same problem there. Everything works just fine, until i close the whole program, then the error-message appears again. Am i unloading the userform incorrect? Maby the login userform says open and continues when everything is getting closed.
Edit 2: I could just turn off alerts but that would be an ugly solution and definitely nothing i want to implement on every close button in the program.
You can verify blank values in textboxes with this:
If TextBox.Text = "" Then
MsgBox "Is blank!"
Unload Me
GoTo Ende
End If
'Your code
Ende: Exit Sub
To verify the username and password in a database, you can do this:
Dim sh As Worksheet
Dim LastRow As Long
Dim UserRange As Range
Dim UserMatch As Range
Set sh = ThisWorkbook.Sheets("database")
LastRow = sh.Range("A" & Rows.Count).End(xlUp).Row
Set UserRange = sh.Range("A1:A" & LastRow)
Set UserMatch = UserRange.Find(What:=UserTextBox.Text, LookIn:=xlValues)
If Not UserMatch Is Nothing Then
MsgBox "User exists!"
If PwdTextBox.Text = UserMatch.Offset(0, 1) Then
MsgBox "Pwd matched!"
'do something
Else
MsgBox "Wrong password!"
'do something
End If
Else
MsgBox "User dont exists!"
'do something
End If
This will work if in the database the usernames are in column A and the passwords in column B.

How can I pass cursor and set focus on a Userform textbox in VBA?

In below program, I receive this error: Run-time error '-2147467259(80004005)': Unspecified error. by highlight this code: txtStartDate.SetFocus in this line: If txtStartDate.Text = "" Then txtStartDate.SetFocus
Private Sub txtTimeUnit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsError(Application.Match(txtTimeUnit.Text, Range("intTable[Units]"), 0)) Then
lblStatusBar = "Please correct value."
Cancel = True
Exit Sub
End If
lblStatusBar = vbNullString
Range("CToDate").Value = txtTimeUnit.Text
If txtStartDate.Text = "" Then txtStartDate.SetFocus
If txtEndDate.Text = "" Then txtEndDate.SetFocus
End Sub
Can anyone help me about this error and passing text box focus (Cursor) to another text box?
Replace
If txtStartDate.Text = "" Then txtStartDate.SetFocus
by
If txtStartDate.Text = "" Then
txtStartDate.SetFocus
Exit Sub
End If

Selected Value(not selected Text) in Combobox: VBA Access

I am writing the following code to populate Combobox.
strSQL = "Select BankID, BankName As [Please Select Bank] from tblBank"
With Me.cmbBank
.RowSource = strSQL
.ColumnCount = 2
.BoundColumn = 2
.ColumnWidths = "0in.;1in."
.ColumnHeads = True
.LimitToList = True
.Requery
.Value = "Please Select Bank"
End With
on Button CLick, I am checking combobox selected value and code is here.
MsgBox Me.cmbBank.Value
It shows Text, Is there any way to get Selected Value(BankID)?
Change this to a valid value of BankID:
.Value = "Please Select Bank"
or your combo returns Null which cause the MsgBox to fail, or correct with Nz:
MsgBox Nz(Me!cmbBank.Value, "No bank selected.")
Private Function Validate() As Boolean
If IsNull(Me.cmbBank.Column(0)) = True Then
MsgBox "No bank selected"
Me.cmbBank.SetFocus
Validate = False
Exit Function
End If
Validate = True
End Function