次の方法で共有


Access で年齢を月単位と年単位で計算する 2 つの関数を作成する方法

高度な機能: 高度なコーディング、相互運用性、マルチユーザー スキルが必要です。

この記事は、Microsoft Office Access データベース (.accdb と.mdb) と Microsoft Access プロジェクト (.apd) に適用されます。

概要

この記事では、指定した日付に基づいて人または物の年齢を計算するために使用できる 2 つの関数を作成する方法について説明します。

注:

この記事で使用される手法のデモは、サンプル ファイル Qrysmp00.exe で確認できます。

詳細

関数の作成

次のコードを入力するか、モジュールに貼り付けます。

'==========================================================
' General Declaration
'==========================================================
Option Explicit

'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
'    Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
'    StartDate: The beginning date (for example, a birth date).
'
' RETURN
'    Age in years.
'
'*************************************************************
Function Age (varBirthDate As Variant) As Integer
   Dim varAge As Variant

If IsNull(varBirthdate) then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthDate, Now)
   If Date < DateSerial(Year(Now), Month(varBirthDate), _
                        Day(varBirthDate)) Then
      varAge = varAge - 1
   End If
   Age = CInt(varAge)
End Function

'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
'  Compliments the Age() function by calculating the number of months
'  that have expired since the last month supplied by the specified date.
'  If the specified date is a birthday, the function returns the number of
'    months since the last birthday.
'
' INPUT PARAMETERS:
'    StartDate: The beginning date (for example, a birthday).
'
' RETURN
'    Months since the last birthday.
'*************************************************************
Function AgeMonths(ByVal StartDate As String) As Integer

Dim tAge As Double
   tAge = (DateDiff("m", StartDate, Now))
   If (DatePart("d", StartDate) > DatePart("d", Now)) Then
      tAge = tAge - 1
   End If
   If tAge < 0 Then
      tAge = tAge + 1
   End If

AgeMonths = CInt(tAge Mod 12)

End Function

Age() 関数と AgeMonths() 関数のテスト

Age() 関数と AgeMonths() 関数をテストするには、次の手順に従います。

重要

次の手順では、コンピューターの日付を変更するように求められます。 手順 6 を完了して、日付を現在の日付にリセットしてください。

  1. コントロール パネルの [日付/時刻] ツールを使用して、現在の日付を書き留めてから、日付を 2001 年 6 月 3 日に設定します。

  2. モジュールを開くか、新しいモジュールを作成します。

  3. [表示] メニューの [イミディエイト ウィンドウ] をクリックします。

  4. 友人の誕生日が 1967 年 11 月 15 日で、今日が 2001 年 6 月 3 日であるとします。 [イミディエイト] ウィンドウに次の行を入力し、Enter キーを押します。

    ?Age("11/15/67")

    Microsoft Access は値 33 (年) で応答します。

  5. 次の行を入力し、Enter キーを押します。

    ?AgeMonths("11/15/67")

    Microsoft Access は値 6 で応答し、このユーザーの最後の誕生日から 6 か月が経過したことを示します。 あなたの友人は33歳6ヶ月です。

  6. コントロール パネルの [日付/時刻] ツールを使用して、手順 1 で説明した現在の日付に日付をリセットします。

Age() 関数と AgeMonths() 関数の使用

次の手順では、新しいコントロールに年齢値を配置して古い注文をマークする方法について説明します。

  1. サンプル データベース Northwind.mdbで、新しいモジュールで Age() 関数と AgeMonth() 関数を入力します。

  2. デザイン ビューで Orders フォームを開き、バインドされていないテキスト ボックス コントロールを追加します。

  3. 新しいテキスト ボックス コントロールの ControlSourceproperty に次の行を入力します。

    =Age([OrderDate]) & " yrs " & AgeMonths([OrderDate]) & " mos"

  4. フォーム ビューでフォームを表示します。 注文の年齢は、新しいテキスト ボックス コントロールに表示されることに注意してください。

関連情報

日付の違いの詳細については、Visual Basic エディターで、[ヘルプ] メニューの [Microsoft Visual Basic ヘルプ] をクリックし、Office アシスタントまたは応答ウィザードで datediff 関数を入力し、[検索] をクリックして記事を表示します。