我有一个表格,允许用户在顶部选择日期。当用户更改日期时,应该更改记录以反映出来。如果已经有该日期的记录,则切换到该记录,但是如果没有该日期的记录,请创建一个新记录。该表被设置为不允许在日期列上重复。这是我在Form_Load事件上拥有的代码以及我分别调用的子代码:
Private Sub Form_Load()
Me.cobYear.Value = Year(Date)
Me.cobMonth.Value = Month(Date)
DaysChange Me
Me.cobDate.Value = Day(Date)
UpdateDate Me
DoCmd.Maximize
End Sub
Sub DaysChange(objForm As Form)
Dim i As Integer
Dim DaysInMonth As Integer
Dim LeapDay As Integer
LeapDay = 0
If (Int(objForm.cobYear / 400) = (objForm.cobYear / 400)) Or ((Int(objForm.cobYear / 4) = (objForm.cobYear / 4)) And Not (Int(objForm.cobYear / 100) = (objForm.cobYear / 100))) Then
LeapDay = IIf(objForm.cobMonth = 2, 1, 0)
End If
DaysInMonth = DLookup("DaysInMonth", "tblMonths", "MonthNumber =" & objForm.cobMonth) + LeapDay
For i = 1 To DaysInMonth
objForm.cobDate.AddItem Item:=i
Next i
End Sub
Sub UpdateDate(objForm As Form)
If Not objForm.cobDate = "" And Not objForm.cobMonth = "" And Not objForm.cobYear = "" Then
objForm.tbDate.Value = DateSerial(objForm.cobYear, objForm.cobMonth, objForm.cobDate)
DayOfWeek = Weekday(objForm.tbDate.Value, 2)
'Me!subfrmDispatchSheet.Form.cobRouteID.Requery
objForm.lblDayOfWeek.Caption = WeekdayName(Weekday(objForm.tbDate.Value))
DateOfRecord = objForm.tbDate.Value
End If
End Sub
这是用户更改日期的代码:
Private Sub cobDate_Change()
UpdateDate Me
ChangeRecord
End Sub
Private Sub cobMonth_Change()
DaysChange Me
UpdateDate Me
ChangeRecord
End Sub
Private Sub cobYear_Change()
DaysChange Me
UpdateDate Me
ChangeRecord
End Sub
我已经尝试了几种方法来做到这一点。
1)我完全在代码中尝试过:
Private Sub ChangeRecord()
If DCount("ShiftDate", "tblShiftRecap", "ShiftDate =" & Me.tbDate.Value) = 0 Then
Else
Me.tbShiftID.Value = DLookup("ShiftID", "tblShiftRecap", "ShiftDate =" & Me.tbDate.Value)
End If
Me.Requery
End Sub
如何在单个表格上执行此操作?我知道如何添加子表单,但如果所有字段都在单个表单中,则不知道。
不幸的是,这会在我加载表单时尝试添加新记录。
2)我也尝试在查询中这样做
SELECT tblShiftRecap.ShiftID, tblShiftRecap.MQFStartTime
FROM tblShiftRecap
WHERE (((tblShiftRecap.ShiftDate)=GetDateOfRecord()));
和SQL调用的功能:
Public Function GetDateOfRecord()
GetDateOfRecord = DateOfRecord
End Function
答案 0 :(得分:0)
如果我正确回答了您的问题,则您希望根据条件导航到当前表单中的特定记录
要浏览表单,最简单的方法是打开一个记录集克隆,使用.FindFirst
,然后将表单上的当前记录更改为找到的记录:
Dim rs As Recordset
Set rs = Me.RecordsetClone 'Load form records into recordset clone
rs.FindFirst "ShiftDate = " & Format(DateOfRecord, "\#yyyy-mm-dd\#") 'Navigate to date
If Not rs.NoMatch 'If there's a matching record
Me.Bookmark = rs.Bookmark 'Navigate to it
End If