line item numbers in a subform

G

Guest

Does anyone know how to set up a field in a subform to enter sequential numbers? I have a field in the main form which is the "one" side, then my subform contains the "many" details for the main form. In the subform, as the user enter records I need a field to automatically enter numbers (i.e. 1, 2, 3) and so on for each record. I tried an autonumber, but that carried the numbers for all the records, I need this field to start with 1 for the first record in the subform

Thanks!
 
R

Rick Brandt

Amy said:
Does anyone know how to set up a field in a subform to enter sequential
numbers? I have a field in the main form which is the "one" side, then my
subform contains the "many" details for the main form. In the subform, as
the user enter records I need a field to automatically enter numbers (i.e.
1, 2, 3) and so on for each record. I tried an autonumber, but that
carried the numbers for all the records, I need this field to start with 1
for the first record in the subform.

In the BeforeUpdate event of the subform...

If IsNull(Me!SubID) = True Then
Me!SubID= Nz(DMax("SubID", "SubTableName", "MainID = " &
Me.Parent!MainID & ""),0) + 1
End If
 
R

Roger Carlson

On my website (see sig below) there is a small sample database called
"FormSubform.mdb" which illustrates how to do this. It is basically the
same method that Rick mentioned, but you can see it in action.

--
--Roger Carlson
www.rogersaccesslibrary.com
Reply to: Roger dot Carlson at Spectrum-Health dot Org

Amy said:
Does anyone know how to set up a field in a subform to enter sequential
numbers? I have a field in the main form which is the "one" side, then my
subform contains the "many" details for the main form. In the subform, as
the user enter records I need a field to automatically enter numbers (i.e.
1, 2, 3) and so on for each record. I tried an autonumber, but that carried
the numbers for all the records, I need this field to start with 1 for the
first record in the subform.
 
A

Albert D. Kallal

The best way is to not actaully store the number, as then if a deletion
occurs, you have to re-number.

Hence, you can:

You can put the follwing code in the sub form:

Function Rpos(vId As Variant) As Long

Rpos = 0
If IsNull(vId) = False Then
Me.RecordsetClone.FindFirst "id = " & vId
If Me.RecordsetClone.NoMatch = False Then
Rpos = Me.RecordsetClone.AbsolutePosition + 1
End If
End If

End Function

Then, you can put a un-bound text box in the continoues form,and

=(rpos([id]))

The above assumes you have a key field called id. It also assumes dao.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top