Using Auto Open

The following example illustrates how the Auto_Open procedure can be used to provide a start up message. The example shown in Listing 3.1 will give a different message for each day of the week. On Monday the message given is "Have a nice week", on Tuesday the message is "One down, four to go!", and so on. The procedure works by creating a variable that will contain the digits from 1 to 7 representing the days of the week. It then finds out what day it is, using a built-in function that returns the current day (more on this in Chapter 9), and a Case statement to decide what to do for each day of the week (more on this in Chapter 9). Don't worry about your understanding of this just yet, as long as you can see how Auto-Open() works. The line select Case dayNum is used to choose the current day and to associate the appropriate message so that it can then be output in a message box.

Listing 3.3 Using the Auto_open macro

'Example auto open macro to give a day of the week message Dim dayNum As Integer dayNum = Weekday(Date) Select Case dayNum

Case 2: MsgBox "Today is Monday! Case 3: MsgBox "Today is Tuesday!" Case 4: MsgBox "Today is Wednesday!" Case 5: MsgBox "Today is Thursday!" Case 6: MsgBox "Today is Friday!" Case Else: MsgBox "Happy Weekend!" End Select End Sub

Note how the Case statements are indented between the Select Case dayNum and End Select block.

Figure 3.12 Sample MsgBox from Listing 3.1

0 0

Post a comment