Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

Validating Help Needed Please!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adi - 04 Nov 2004 21:47 GMT
Hi,

I'm relatively new to VBA but I have learnt my way around the basics
over the last few weeks to create a UserForm in Word. This USerform
basically asks aload of questions to create a standardised document.

I have crawled through the newsgroups and various web sites but I
can't find anything about how to validate a userform correctly to make
sure everything has been filled in!

The code I am using so far goes under the "OK" command button and it
does work in popping up a message box telling the user a particular
part hasnt been filled in, but after the user "ok's" the message box
it also gets rid of the user form!

Can anyone help with adding/changing the code or using a different
code all together so that the userform remains loaded?

If txtbox1 = "" Then
Msgbox = "Please check the text box has been filled in."
End If

Also, I do have option buttons on my form - is there a specific code
to make sure one of those is selected.

Many thanks to anyone who can help!
Jay Freedman - 04 Nov 2004 22:57 GMT
Hi Adi,

The thing that makes the userform go away is a statement at the end of the
OK click procedure that says either Unload Me or Me.Hide (depending on how
the userform was created). If the data entry fails validation, that
statement should not be executed.

One way to do this is to place an Exit Sub statement immediately after the
error MsgBox for each validation test. This simply skips all the remaining
tests and puts the user back in control of the userform. The next time they
click the OK button, all the validatioin will be done again.

Some programmers feel strongly that each procedure should have a single
entry point and a single exit point, and multiple Exit Sub statements
violate that principle. The alternatives are (1) to make all the validation
tests in a series of nested If .. Then clauses or (2) to maintain a Boolean
(True/False) variable that starts as False and is set to True if any test
fails, and execute the Unload statement only if that Boolean is still False
at the end of the procedure. This is suitable for a large procedure with a
dozen or more tests, but it's overkill for the usual small one.

You test option buttons by looking at their .Value properties -- if they're
all False, no button has been selected. (You can avoid having to validate
this if the Userform_Initialize or Userform_Activate procedure sets the
.Value for one option button to True. There's then no way for the user to
deselect any button without selecting a different one.)

Here's some sample code:

Private Sub CommandButton1_Click()
  If TextBox1.Text = "" Then
     MsgBox "Please fill the text box"
     Exit Sub
  End If

  If (OptionButton1.Value = False) And _
     (OptionButton2.Value = False) Then
     MsgBox "Please select one option"
     Exit Sub
  End If

  Unload Me
End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Hi,
>
[quoted text clipped - 22 lines]
>
> Many thanks to anyone who can help!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.