Visual Basic Code of the Week (COTW)
Issue #73
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

In this Issue

In this issue we are introducing a user control text box that is designed to only accept dates.

This source code is designed for VB 5.0 and up. Questions? Email us at


The ucTextBoxDate class will show how you can create a simple text box User Control that can perform a specific function such as validating dates. This control can be modified to perform other functions such as numeric validation or a custom mask field.

Not every property is exposed for this control, for example BackColor and ForeColor. If this was going to be published as a commercial control we would be sure to include all the standard text box properties and methods. In order to save some room and shorten this issue we left them out. We have included the basic ones that are required to use this control. Please add any additional properties that you need for your application.

There is another routine (CharCount) included in this control that allows you to count the number of times a specific character appears in a string. This can be useful in other circumstances other than this control.

If you have any questions about using or creating this control, let us know at

Public DateFormat As String

This property allows you to set the format of the date that will be returned by the ucTextBoxDate control. The default is "Short Date". See the VB help command Format for more details on the various formatting options.

Public DateSeparator As String

Allows the programmer to control which date separator is used. The default separator is determined by a "hack" that we implemented that checks the returned string from the Format command. We are pretty sure there must be an API to determine this, but we were not able to locate it in time for this issue. In many countries the date separator is the slash (/).

Public Function Validate() As Boolean

Returns True if the date is valid and False if it is not. This function will also force the event BadDate to be raised if the date is not valid.

Public Property Let Text(sText As String)
Public Property Get Text() As String

Provides a method to set or get the current text of the ucTextBoxDate control.


See the above information for return values for the specific functions or properties. All routines might possibly return an error raised with the description set to the text of the error that occurred.

Sample Usage

The below sample assumes you have a form with a command button called Command1 and a ucTextBoxDate called ucDate.

Private Sub Command1_Click()
    If ucDate.Validate Then
        MsgBox "The date you entered is " & ucDate.Text, vbOKOnly
        ' invalid date logic
        ' you can put your error messages here or in the BadDate event
    End If
End Sub

Private Sub Form_Load()
    ucDate.DateFormat = "mm" & ucDate.DateSeparator & "dd" & ucDate.DateSeparator & "yyyy"
End Sub

Private Sub ucDate_BadDate()
    MsgBox "The date you entered is invalid."
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945