HandsOn Using IfThen Else Conditional Statement

1. Insert a new module and enter the following WhatTypeOf Day procedure in the module's Code window.

Sub WhatTypeOfDay()

Dim response As String

Dim question As String

Dim strMsgl As String, strMsg2 As String

Dim myDate As Date question = "Enter any date in the format mm/dd/yyyy:" _

& Chr(13)& " (e.g., 11/22/1999)" strMsgl = "weekday" strMsg2 = "weekend" response = InputBox(question) myDate = Weekday(CDate(response))

If myDate >= 2 And myDate <= 6 Then MsgBox strMsg1

Else

Introduction to Access 2003 VBA Programming

MsgBox strMsg2 End If End Sub

The above procedure asks the user to enter any date. The user-supplied string is then converted to the Date data type with the built-in CDate function. Finally, the Weekday function converts the date into an integer that indicates the day of the week (see Table 5-3). The integer is stored in the variable myDate. The conditional test is performed to check whether the value of the variable myDate is greater than or equal to 2 (>=2) and less than or equal to 6 (<=6). If the result of the test is true, the user is told that the supplied date is a weekday; otherwise, the program announces that it's a weekend.

Table 5-3: The Weekday function values

Constant

Value

vbSunday

1

vbMonday

2

vbTuesday

3

vbWednesday

4

vbThursday

5

vbFriday

6

vbSaturday

7

2. Run the procedure a few times, each time supplying a different date. Check the Visual Basic answers against your desktop or wall calendar.

0 0

Post a comment