This article was previously published under Q210534
This article has been archived. It is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies only to a Microsoft Access database (.mdb).
If you type a credit card expiration date (month/year) in a Date/Timefield on a form, Microsoft Access 2000 assumes that the card expires on thefirst day of the month. This article shows you how to create a functionto correctly calculate the expiration date as the last day of the month.
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:
Start Microsoft Access and open a new database.
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.
Create a new module with the following function:
Option ExplicitFunction ExpirationDay (MyDate as Control) Dim NextMonth If IsNull(MyDate) Then Exit Function NextMonth = DateAdd("m", 1, MyDate) MyDate = NextMonth - DatePart("d", NextMonth)End Function
Create a new form based on the Cards table using the AutoForm: Columnar option. On the View menu, click Design View.
Click the Expiration text box and set its AfterUpdate property to the following expression:
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