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 firstname.lastname@example.org.
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 email@example.com
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.
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 Else ' invalid date logic ' you can put your error messages here or in the BadDate event ucDate.SetFocus 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
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: http://www.codeoftheweek.com/membersonly/bi/0073.html