![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: May 2002
Posts: 206
|
For the record I did a search on passwords, read them all. Also have had my nose in Excel help file. Trying, but stuck. I know about Sub Form_Load(), but not sure how to implement it.
I'll bet I've gone about this wrong, but here's what I've got- Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) UserForm1.Show End Sub Private Sub CommandButton1_Click() If TextBox1 = "pw" Then MsgBox "Password Accepted", vbInformation Unload Me Else If TextBox1 <> "pw" Then MsgBox "Invalid Password", vbOKOnly + vbCritical Unload Me ***What Can I put here to abort the save?*** End If End If End Sub [ This Message was edited by: dsnbld on 2002-05-11 03:09 ] |
|
|
|
|
|
#2 |
|
New Member
Join Date: Feb 2002
Location: Birmingham
Posts: 13
|
You could try cancelling the save before you show the userform. Then if the user gets the password right save the workbook yourself.
Either that or just hide the useform when the user clicks on OK and change your code to Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) UserForm1.Show If UserForm1.TextBox1 <> "pw" Then Cancel = True End If Unload UserForm1 End Sub |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: May 2002
Posts: 206
|
Good work Gary- Thanks. I thought I tried cancel... Seems to be working fine, just got to disable the close command on the form.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) UserForm1.Show If UserForm1.TextBox1 <> "pw" Then Cancel = True End If Unload UserForm1 End Sub Private Sub CommandButton1_Click() If TextBox1 = "pw" Then MsgBox "Password Accepted", vbInformation UserForm1.Hide Else If TextBox1 <> "pw" Then MsgBox "Invalid Password", vbOKOnly + vbCritical Unload UserForm1 End If End If End Sub |
|
|
|
|
|
#4 |
|
New Member
Join Date: Mar 2002
Posts: 18
|
I use this code. It will only allow a user to save the file if the correct password is given and will have a pop-up that says "not saved...." if it is incorrect:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim Password As String Dim EnteredPassword As String Password = "Secret" EnteredPassword = InputBox("Enter password to save changes") If EnteredPassword <> Password Then Cancel = True MsgBox ("Password incorrect, file not saved") End If End Sub Mav |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: May 2002
Posts: 206
|
Mav, that is too fine. The Sub Form_Load() I was talking about is basically what you've got, but I had trouble getting it to work. Kept missing some little something.
Great work guys. I should have posted a long time ago, but I learned quite a bit in the process. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|