ACC2000: How to Calculate a Credit Card Expiration Date (210534)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210534
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

SUMMARY

If you type a credit card expiration date (month/year) in a Date/Time field on a form, Microsoft Access 2000 assumes that the card expires on the first day of the month. This article shows you how to create a function to correctly calculate the expiration date as the last day of the month.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To make a credit card expiration date the last day of the month entered, follow these steps:
  1. Start Microsoft Access and open a new database.
  2. Create a new table and add the following fields:

    Field Name: CardNumber
    Data Type: Text

    Field Name: Expiration
    Data Type: Date/Time

    Field Name: Name
    Data Type: Text

    Save the table as Cards, and then close it.

  3. Create a new module with the following function:
    Option Explicit
    Function ExpirationDay (MyDate as Control)
    
       Dim NextMonth
       If IsNull(MyDate) Then Exit Function
       NextMonth = DateAdd("m", 1, MyDate)
       MyDate = NextMonth - DatePart("d", NextMonth)
    
    End Function
    					
  4. Create a new form based on the Cards table using the AutoForm: Columnar option. On the View menu, click Design View.
  5. Click the Expiration text box and set its AfterUpdate property to the following expression:

    =ExpirationDay([Expiration])

  6. On the View menu, click Form View. Type 04/2000 in the Expiration text box, and then press the TAB key.
Notice that the date changes to "30-Apr-00," the last day of the fourth month of the year 2000. You can also use the following derivative of the ExpirationDay() function in calculated fields in a query:
Function ExpirationDay (MyDate)
    Dim NextMonth
    If IsNull(MyDate) Then Exit Function
    NextMonth = DateAdd("m", 1, MyDate)
    ExpirationDay = NextMonth - DatePart("d", NextMonth)
End Function
				

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbinfo kbProgramming KB210534