Dim mydb As DAO.Database
Dim myrst As DAO.Recordset
Dim Date1, Date2 As Date
Dim mysql As String
Dim qdf As QueryDef
Dim EmployeeID As String
EmployeeID = DLookup("lngEmpID", "tblEmployees", "Forms!frmEmployeeOrderForm.cboEmployeeName.Value = EmployeeName")
Me.EmpID = EmployeeID
PickupTime = DLookup("Pickup", "tblEmployees", "Forms!frmEmployeeOrderForm.cboEmployeeName.Value = EmployeeName")
Me.PickupDay = PickupTime
Set mydb = CurrentDb
Date1 = Me.dtmGiveAwayDate1
Date2 = Me.dtmGiveAwayDate2
mysql = ("Select tblOrders.lngEmpID, tblOrders.dtmGiveAwayDate1, tblOrders.dtmGiveAwayDate2 FROM tblOrders WHERE tblOrders.lngEmpID =" & EmployeeID & " AND tblOrders.dtmGiveAwayDate1 =" & Date1 & " AND tblOrders.dtmGiveAwayDate2 =" & Date2)
MsgBox mysql
Set myrst = mydb.OpenRecordset(mysql)
If myrst.EOF = False Then
MsgBox "You have already ordered for this PFG." & vbCrLf & "Please see Jody for help."
cmdCancel_Click
Exit Sub
End If
当我运行该程序时,我试图阻止员工在相同日期两次输入他们的名字。为了测试,我尝试复制同一员工的记录,并且不会触发myrst.eof = false并显示消息框,其中显示"您已订购此PFG"。任何帮助深表感谢。
提前谢谢。
答案 0 :(得分:0)
使用FindFirst方法:
myrst.FindFirst "field = " & value
If myrst.NoMatch Then
//code when record doesn't exist
End If
答案 1 :(得分:0)
检查与日期的相等性可能很棘手。由于VBA日期可以包括纳秒的时间,可能是一方(SQL /代码)有实时,而另一方有默认的午夜值?
答案 2 :(得分:0)
Set mydb = CurrentDb
Date1 = Format(Me.dtmGiveAwayDate1, "Short Date")
Date2 = Format(Me.dtmGiveAwayDate2, "Short Date")
mysql = ("Select tblOrders.lngEmpID, tblOrders.dtmGiveAwayDate1, tblOrders.dtmGiveAwayDate2 FROM tblOrders WHERE tblOrders.lngEmpID =" & EmployeeID & " AND tblOrders.dtmGiveAwayDate1 =" & "#" & Date1 & "#" & " AND tblOrders.dtmGiveAwayDate2 =" & "#" & Date2 & "#")
MsgBox mysql
Set myrst = mydb.OpenRecordset(mysql)
这是我使用的代码,现在可以使用了。谢谢。