User Form duplicates information when sub is cancelled

A

aehan

Hello all

I have made a user form to populate a Word 2003 template. I have added a
piece of code that exits the population of the document if the required
fields aren't filled in, it reads like the sample below:

If Len(Trim(cmbDiv.text)) <= 0 Or Len(Trim(cmbDept.text)) <= 0 Or
Len(Trim(txtEMail.text)) <= 0 Then
MsgBox "You must fill in all the required fields"
Exit Sub
ElectronicOption.Enabled = True
End If

The code works, however what happens is that if the code is exited the form
has already placed some data in the template, and it duplicates it. So
instead of for example seeing the Department name as "Sales and Marketing",
you see it as "Sales and Marketing Sales and Marketing". I thought I would
have to write something like me.refresh before Exit Sub, but I can't find
anything in Word vba like that, I tried Repaint, but that didn't work.

I think the problem is that the data is already there, and when the rest of
the fields are filled in and the OK button clicked, the fact that some of the
data was already there is not recognised, and in that case I don't know what
to do. Does anyone have any ideas?

Thanks for all your help
Aehan
 
D

Doug Robbins - Word MVP

Your validation code should be the first part of the routine that transfers
the data into the document so that nothing is transferred before it is run.

Another way around it however is to use document variables and docvariable
fields as you can set the value of the variable as many times as you want
and it is only the last value that it is set to that will be displayed in
the docvariable field.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top