Dragon Drop - A Visual Basic Software Consultancy

Word Code / Best Practices

How To Create A Simple Template With A User Form

Step Nine

Here we write the Event Handler. In other words we write the code which will run when the Document_New event has been raised.

First, we must discuss how we are going to undertake this task. When a document is being created we want the dialog box to appear. It would be a nice touch to have today's date already entered in the Date field as well. This is because most memos go out on the day that they are made and, besides, the user could always change the date if she so wished.

The user would then fill in the text boxes and then the data would be transferred to the bookmarks on the document, the dialog box would vanish and then the user would be ready to start typing from where the bmkStartHere bookmark is situated.

As a further requirement if the user decides to cancel the operation, by pressing the Cancel button or clicking on the cross in the top right hand corner then the document is to be closed down there and then.

The last part is the interesting part. We need to know what the user has done. Now, each object has a Tag property which can contain a string. So, what we can do is to initialise this property when we create the form and then change it when the user clicks on OK. When the form vanishes from view the form's Tag is inspected and if it clear that the OK button the document is filled out otherwise it is removed.

Enter the following code into the Document_New() routine:

Private Sub Document_New()

  Dim oForm As frmMemo
  
  
  Set oForm = New frmMemo
  oForm.Tag = "Cancel"
  oForm.txtDate.Text = Format$(Date, "d mmmm yyyy")
  oForm.Show
  
  

End Sub

The first line is to define a oForm variable. Actually this is a pointer to an object of type frmMemo.

Next we create an instance of the frmMemo class and we point the oForm pointer to the object. This is done in the Set statement. One can always tell when pointers are being used because the Set statement is used to set the value of a pointer to the location of an object.

The next line is setting the tag property of the object to "Cancel" and then, in the line after, we are putting the day's date into the date text box on the form. Lastly, we are showing the form.

If were to run the form now then this is what we would see. Note that none of the buttons would have any functionality at all. That part comes next.

Note that we are not doing frmMemo.Show - this is a bad programming habit. What happens in this instance is that a default object is created and there are various problems and issues with these 'magic forms'. Suffice it to say when it was announced that .Net would not have magic forms the VB community, as a whole, breathed a sigh of relief. We have seen problems galore with magic forms - the simple rule is never to use them!

<< Back Next >>

Updates or Comments

If there are any suggestions for updates or comments then please drop us a mail at malcolm.smith@dragondrop.com.