Hi Graham,
Thankyou for the help. That really did sort me out and it appears to be
working now the way I wanted.
One more thing is that I find is that some crop plantings do not get
sprayed so in that case I will need to check first whethere there is an
entry
in tblApplications for PlantingdetailsID and if there is then the above
code
should fire bit if not then it should ignore the above code and carry
on
to
save the data.
tblPlantingDetails---<tblApplications have a one to many relationship
linked
by plantingdetailsID field as FK.
Here is what I'd like to do:
The code in there is just pyseudo code, just to give you an idea.
Private Sub cboPlantingDetailsID_BeforeUpdate(Cancel As Integer)
'Check to see if there is any spray applications for the crop planting
'Pyseudoco
If cboPlantingDetailsID.column(0) In tblApplications.PlantingdetailsID
then
******
' Check to see if this planting is still inside With Holding Period
Dim strMsg As String
Dim iResponse As Integer
Dim rs As DAO.Recordset
Dim dtWHP As Date
Set rs = CurrentDb.OpenRecordset( _
"Select Max(ApplicationDate + WHP) as WHPDate " _
& "from qryWHP where PlantingDetailsID = " _
& cboPlantingDetailsID.Column(0), dbOpenForwardOnly)
If rs.RecordCount > 0 Then dtWHP = rs!WHPDate
rs.Close
If Date < dtWHP Then
strMsg = "Still inside WHP. Do you wish to continue?" &
Chr(10)
strMsg = strMsg & "Click Yes to Continue or No to Back Out."
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save
Record?")
If iResponse = vbNo Then
PlantingDetailsID.Undo
Cancel = True
End If
End If
Else
*** carry on as normal
End Sub
--
niuginikiwi
Nelson, New Zealand
:
Hi niuginikiwi
For one planting, serveral spray products are used so I have to
actaully
check serveral of the WHPDates on the go to make sure they are not
more
than
today's date.
No you don't - you just need to check the latest (maximum) one.
So basically when I click on cboPlantingDetailsID to select a
planting,
the
before insert event should fire and compare todays date to several
date
entries in the qryWHP to make sure none of them are more than
today's
date,
and if they are, it fires the vbYesNo.
Just replace your DLookup by DMax and it should work for you.
A more efficient solution would be to open a recordset with only the
single
value you require:
Dim rs as DAO.Recordset
Dim dtWHP as Date
Set rs = CurrentDb.OpenRecordset( _
"Select Max(ApplicationDate + WHP) as WHPDate " _
& "from ApplicationsTable where PlantingDetailsID = " _
& cboPlantingDetailsID.Column(0), dbOpenForwardOnly)
If rs.RecordCount > 0 Then dtWHP = rs!WHPDate
rs.Close
If Date < dtWHP Then
...
End If
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Hi Graham,
Thank you so much for the reply with the corrections and
suggestions.
I have changed the code to suit your suggestions. See below.
I also want to give you a clear idea of the scenario and what I am
trying
to
do.
I have a list of crop plantings which get sprayed with different
products
that have different with holding periods (WHP).
Say if a crop planting was spray yesterday with a spray that had a
witholding period of 7 days, then I add the spray date
(ApplicationDate)
with
WHP (7days) which gives me a field called WHPDate. ie WHPDate:
ApplicationDate + WHP
For one planting, serveral spray products are used so I have to
actaully
check serveral of the WHPDates on the go to make sure they are not
more
than
today's date.
So basically when I click on cboPlantingDetailsID to select a
planting,
the
before insert event should fire and compare todays date to several
date
entries in the qryWHP to make sure none of them are more than
today's
date,
and if they are, it fires the vbYesNo.
Here is the code after your corrction. It appears to let me select a
planting from cboPlantingDetailsID but I know that I have some
WHPDate
values
for that selected planting which are higher than todays date, but
the
vbYesNo is not firing.
What else am I missing?
Private Sub cboPlantingDetailsID_BeforeUpdate(Cancel As Integer)
' Check to see if this planting is still inside WHP
Const conJetDate = "\#mm\/dd\/yyyy\#"
Dim strMsg As String
Dim iResponse As Integer
Dim varX As Variant
varX = DLookup("[WHPDate]", "qryWHP",
"[qryWHP.PlantingDetailsID]
= "
& cboPlantingDetailsID.Column(0))
If Date < CDate(varX) Then
strMsg = "Still inside WHP. Do you wish to continue?" &
Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard
changes."
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save
Record?")
If iResponse = vbNo Then
PlantingDetailsID.Undo
Cancel = True
End If
End If
End Sub
:
Hi niuginikiwi
By formatting your dates before you compare them, you are actually
comparing
two *strings*.
So "06/29/2009" would be less than "12/12/2008", which I'm sure is
not
what
you want!
If [WHPDate] is a date/time field, then varX should already be a
Date
data
type, so just compare it with Date() (which is already a Date):
If Date < varX Then ...
You might need to force a conversion to a Date:
If Date < CDate(varX) Then ...
Some other problems:
1. This should be in your BeforeUpdate event, not AfterUpdate. You
cannot
cancel the AfterUpdate event.
2. Instead of DoCmd.RunCommand acCmdUndo, use
cboPlantingDetailsID.Undo.
It
is much more specific and less prone to glitches.
3. Assuming this code is in the module of frmOrderDetailsSubform1,
you
do
not need the full form reference:
Forms!frmOrderDetailsSubform1!cboPlantingDetailsID
should be
cboPlantingDetailsID
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
message
I have a query called qryWHP which has a caluclated date field
which
I
would
like to compare with today's date and throw a vbYesNo popup
depeding
on
the
condition. If Date() is less than WHPDate then fire vbYesNo and
then
follow
depending on whether user chooses yes or no.
Here is what what my code on the afterupdate event of a combo
called
cboplantingdetailsID looks like. I even dont know if dlookup in
this
situation is the right approach.
Private Sub cboPlantingDetailsID_AfterUpdate()
Const conJetDate = "\#mm\/dd\/yyyy\#"
Dim strMsg As String
Dim iResponse As Integer
Dim varX As Variant
varX = DLookup([WHPDate], "qryWHP",
"[qryWHP.PlantingDetailsID]
=
" &
Forms!frmOrderDetailsSubform1!cboPlantingDetailsID)
If Format(Date, conJetDate) < Format(varX, conJetDate) Then
strMsg = "Crop still inside WHP. Do you wish to
continue?"
&
Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard
changes."
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save
Record?")
If iResponse = vbNo Then
DoCmd.RunCommand acCmdUndo
Cancel = True
End If
End If
End Sub