2017-01-14 43 views
-2

我想使用VBA代碼在我的數據庫開始時彈出警告消息,並且告訴我,他們的年齡過50的人的名字只是爲了當最後我能到達是需要一個警告彈出消息在訪問

For i = 1 To 4  

If [Forms]![1]![years] >=50 Then 

MsgBox "employees:" & Me.name 

End If 
Next i 
+2

這是沒有實際意義。你不應該對50歲以上的人提出警告。我們中的許多人還活着。 – Gustav

+1

@古斯塔夫:的確,我們是。 :D – Andre

+0

@Amr:你的問題很不清楚。請閱讀[問]。 – Andre

回答

1

你可以用我簡單的函數:

Public Function AgeSimple(_ 
    ByVal datDateOfBirth As Date) _ 
    As Integer 

' Returns the difference in full years from datDateOfBirth to current date. 
' 
' Calculates correctly for: 
' leap years 
' dates of 29. February 
' date/time values with embedded time values 
' 
' DateAdd() is used for check for month end of February as it correctly 
' returns Feb. 28. when adding a count of years to dates of Feb. 29. 
' when the resulting year is a common year. 
' After an idea of Markus G. Fischer. 
' 
' 2007-06-26. Cactus Data ApS, CPH. 

    Dim datToday As Date 
    Dim intAge As Integer 
    Dim intYears As Integer 

    datToday = Date 
    ' Find difference in calendar years. 
    intYears = DateDiff("yyyy", datDateOfBirth, datToday) 
    If intYears > 0 Then 
    ' Decrease by 1 if current date is earlier than birthday of current year 
    ' using DateDiff to ignore a time portion of datDateOfBirth. 
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0) 
    End If 

    AgeSimple = intAge 

End Function 

而且你的窗體上按鈕的click事件的循環(舉例):

Dim rs As DAO.Recordset 

Set rs = Me.RecordsetClone 
If rs.RecordCount > 0 Then 
    rs.MoveFirst 
End If 
While Not rs.EOF 
    If AgeSimple(Nz(rs!DOB.Value, Date)) >= 50 Then 
     MsgBox "Employee: " & rs![Name].Value, vbInformation + vbOKOnly, "50+" 
    End If 
    rs.MoveNext 
Wend 
Set rs = Nothing 

當然,將字段/控件名稱替換爲實際表單的名稱。

編輯

的演示是here

+0

所以,因爲它是或改變的東西 –

+0

沒有使用它,因爲它是。 – Gustav

+0

我是這樣寫的,但沒有工作是缺少的東西? 昏暗RS作爲DAO.Recordset 集RS = Me.RecordsetClone 儘管不RS.EOF 如果AgeSimple(ME m6.Value!)> = 50然後 MSGBOX 「員工:」 與我[M1]。價值! ,vbInformation + vbOKOnly,「50+」 End If rs.MoveNext Wend Set rs = Nothing –