You can use the DLookup function to examine the value of the avail column in
any particular row returned by queryspaceavail. You will need to identify
the row by means of the class ID column's value.
From your description I assume each row in the subform represents a single
space being purchased at a session. To prevent a row being inserted for a
fully booked session (which I take it would be indicated by a value of zero
in the avail column for the session in question in the queryspaceavail
query's result set) put code in the subform's BeforeUpdate event procedure.
This has a Cancel argument whose return value can be set to True to prevent
the row being inserted into the table. So the code would be like this:
Const conMESSAGE = "There are no places available for this session."
Dim strCriteria As String
strCriteria = "[class ID] = " & me.[class ID]
If DLookup("avail", "queryspaceavail", strCriteria) <= 0 Then
MsgBox conMESSAGE, vbInformation, "Warning"
Cancel = True
Me.Undo
End If
Should the Class ID column happen to be of text data type amend the code as
follows:
strCriteria = "[Class ID] = """ & me.[class ID] & """"
You'll notice that I've tested for <= 0 rather than for = 0. This is just
in case its possible for a session to be deliberately oversold. You wouldn’t
be able to oversell a session via the subform of course as once you hit zero
the above code makes it impossible to save the row, but you might have some
other means of dong so. Even if its never possible to oversell a session
legitimately the code will still work.
Apologies in advance if you respond to this and I don't get back to you, but
after tomorrow I'll be away incommunicado for a while.
Ken Sheridan
Stafford, England
Needsomehelp said:
Sorry to be such a pain...
How do I reference the query so vba knows I'm looking for [avail] in
[queryspaceavail]? I get an error "method or data member not found", and teh
RST.[avail] line is highlighted. My form is not based on this query.
My form is based on an individual child's record, and the information is
going into [sessioninfo], a subform that records the classes paid for. The
queryspaceavail sums up the classes purchased, and in that query, I have a
calculated field [avail] that is made up of the field [ttl inventory] - [sum
of classes purchased].
Thanks!
Lord Kelvan said:
ok iif is the if satatement for queries
if is for VBA
try
if recordset.[avail]>0 then
msgbox("good job")
{do the update query commands here}
else
msgbox ("don't oversell this class " & recordset.[avail] & " spots
are avaliable")
end if
....... actually i just realised what you are doing you are using bound
objects rather than recordsets.
i am not that familer with bound objects as i use recordsets all the
time they are more reliable and eaiser to control.
how are you storing the data for the classes are you having a max
value in one table and in the other table where poeple come in and
trying to compare thoes two values or are you subtracting a number
form that value till it reaches 0
if you want to control it you are going to have to use a record set
try this code in the after update
(note this isnt tested)
dim db as database
dim rst as recordset
set db = currentdb()
Set rst = Db.OpenRecordset("space available", dbOpenDynaset)
if rst.[avail]>0 then
msgbox("good job")
DoCmd.DoMenuItem acFormBar, acEditMenu, acSaveRecord
else
msgbox ("don't oversell this class " & rst.[avail] & " spots are
avaliable")
DoCmd.DoMenuItem acFormBar, acEditMenu, acundo
end if
hope this helps
Regards
Kelvan