hi Tony,
If you open the forms you will see there is a control called Deal Nbr. I've
set the default value to 1. if you add more records in the subform for the
same company I want this to increase by 1. If you input a new company data it
should start from 1 again. If you start a new month it should start at 1
again for all companies.
As far as I understand it, you need the deal number for [tblhvdealspt1].
Normally you would use a Date/Time field to order it correctly:
DealNo:
DCount(
"*";
"tblhvdealspt1";
"txtcompany='" & [txtcompany] &
"' AND Format(txtmonth,'yyyy-mm') = '" &
Format([txtmonth];"yyyy-mm") & "' AND [txtmonth] <=" &
Format([txtmonth];"\#yyyy-mm-dd hh:nn:ss\#")
)
Unfortunately you do not store the complete time in [txtmonth]. So we
need to use the [ID] as order criteria
DealNo:
DCount(
"*";
"tblhvdealspt1";
"txtcompany='" & [txtcompany] &
"' AND Format(txtmonth,'yyyy-mm') = '" &
Format([txtmonth];"yyyy-mm") & "' AND [ID] <=" & [ID]
)
So built a query based on [tblhvdealspt1] and add one of the above fields.
In your case it may be sufficient to calculated the number before
inserting a new record:
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim maxDealNbr As Variant
maxDealNbr = DMax( _
"txtdealnbr", _
"tblhvdealspt1", _
"txtcompany = '" & Me![txtcompany] & "' AND " & _
"Format(txtmonth,'yyyy-mm') = '" & Format([txtmonth], "yyyy-mm") _
)
Me![txtdealnbr] = Nz(maxDealNbr, 0) + 1
End Sub
btw, prefixing table names may be necessary, but doing the same with
field names in your case 'txt' makes no sense. It adds unnecessary noise
to the code.
mfG
--> stefan <--