PDA

View Full Version : UserForm Help



archimedes
11-15-2006, 02:35 PM
I have a MS Word tempalte file that I have created a UserForm in. The UserForm simply has a text label and text box. I also have a Clear and Register command button.

My text label: Registration Code
My text box is: txt_regcode
My Clear btn is: cmd_clear
My Register btn is: cmd_register
UserForm name: UserForm1
Registration Code: "1234"

I simply want to display the UserForm (UserForm1) when I open the MS Word document Template and require my fellow workers to enter a valid "registration code" that I will hard code in the VBA code before they can use the Template to create a document.

They will do this by entering the proper code "1234" in the txt_regcode field and then selecting the "Register" command button. If they successfully enter 1234 into the text box, I will close UserForm1 and then allow them to beging their new document based on my template. If they do not properly enter "1234" then I will display a message box asking them to try again. If they select the Cancel cmd button, it will close the UserForm and quit MS Word.

I have some limited VBA experience, I just need a pointer in the right direction on some of the syntax.


Thank you in advance.

lucas
11-15-2006, 03:11 PM
It's easy enough to get this to work but there are so many ways around it that you would also have to deal with....what if they just hit the X in the upper right hand corner of the form...etc.

fumei
11-15-2006, 04:56 PM
What is the point to this? There is nothing being "registered" anywhere, is there? Why are you trying to do this.

As Steve mentions, this would be fairly easy to do, even with blocking the user from acting on the Close "x". However, I would like to know more about what the business case is for doing it.

Especially the part about closing Word. What? Is it your template or nothing at all?

Lastly - what is this with "open the MS Word document Template". Templates should NEVER be opened by users.

archimedes
11-15-2006, 05:39 PM
Since english is my second language maybe I am not being as clear as possible. First, thank you for replying. We have a very sensitive template that we use at our company and we only want particular people to be able to create new documents based on this template. So, this is a matter of security for us. I know there are probably many ways to accomplish this, but I thought by requiring a user to enter some type of validation code before being allowed to use the template it will help control unautorized use.

I have a single text field form that I created in VBA with cancel and registration command buttons. I have the UserForm loading automatically when one of our users try to create a new document based on the template. So, all of that works as planned. I just need help compairing the string variable in the textbox to a constant and having a couple conditions occur based on the results. Condition 1: if the user enters the proper string the UserForm will close and they will simply start using the document. Condition 2: the wrong string was entered in the textbox and a message box appears asking them to enter the code again. If the user selects the Cancel command button it closes the UserForm and the new document.

Hopefully this makes more sense? Any help and guidance is appreciated.

Many thanks for helping a novice.

lucas
11-15-2006, 05:44 PM
I understood it the first time.....I'm just saying it won't be much security without a lot of work.

lucas
11-15-2006, 06:07 PM
Here is a start for you...see attached
password is 1234

mdmackillop
11-15-2006, 06:17 PM
Password protect your template. Add the following to your userform button, which should be stored in Normal or other global template, not the one you're trying to use.

Option Explicit
Private Sub cmdNewDoc_Click()
Dim Tries As Long
TryAgain:
On Error GoTo errH
Documents.Add Template:="C:\Templates\My Template.dot"
If ActiveDocument.AttachedTemplate.Name = "My Template.dot" Then
Unload Me
Exit Sub
End If
errH:
Tries = Tries + 1
If Tries = 3 Then
MsgBox "No more tries"
Unload Me
Exit Sub
End If
Resume TryAgain
End Sub

mdmackillop
11-15-2006, 07:00 PM
While it's not foolproof, you could also add code to record access attemps in a text file or elsewhere.