This example is for entering CD albums in the main form, and the tracks of
the CD in the subform (1 ~ 15 or whatever.)
It assumes you have:
- a table for entering the albums, with an AlbumID primary key;
- a related table named tblTrack, with fields:
AlbumID relates to AlbumID in the album table;
TrackNum the number you need to set to the next available.
It checks that the main form is not at a new record (where AlbumID would be
blank), and then uses DMax() to get the highest value assigned so far for
this album, Nz() to convert that to zero if there's none, and adds one.
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim strWhere As String
If Me.Parent.NewRecord Then
Cancel = True
MsgBox "Enter or select a record in the main form first."
Else
strWhere = "[AlbumID] = " & Me.Parent![AlbumID]
Me.[TrackNum] = Nz(DMax("TrackNum", "tblTrack", strWhere),0) + 1
End If
End Sub
If you need help with the DMax(), it uses the same arguments as DLookup(),
so this should help:
Getting a value from a table: DLookup()
at:
http://allenbrowne.com/casu-07.html
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"tbl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there a good way to offer a default, incremented
> line-number in a subform, where the first subform record
> would offer up a line number of "1", and each successive
> subform record would have the default incremented by 1?
>
> The number of subform records would never be more than 15,
> for any given main form record.
>
> This would differ from the common "invoice number" request,
> in that each new main form record would start the subform
> record (line) numbering default anew (1 thru xx).
>
> After 3 hours of trying this and that, I've only produced
> impared lucidity!
> --
> Thanks,tbl