Default value on a text box

E

Ed

I have a Main form. Each record in the main form has a number of assocated
records in a subform. I want to manually sort the subform records using a
Sort field with an integar. When I create a new subform record, I want the
default value to show the next available sort number based on the Main form
record.
Main form = frmMain (Single Form)
subform = subformDetail (Datasheet)
=updateSort() in txtSort.defaultvalue

Public Function updateSort()
updateSort = DMax("Sort", "tblDetail", "[ID] = " &
[Forms]![frmMain]![txtID]) + 1
End Function

When I create a new subform record, the default works OK. When I create a
second record the default value does not re-run the function. The sort
number is repeated.
 
D

Douglas J. Steele

Put code in the form's AfterUpdate event to set the default value as opposed
to just setting the property of the text box.
 
S

Sandra Daigle

Try using the BeforeInsert event of the subform's form instead -

Private Sub Form_BeforeInsert(Cancel as Integer)
me.txtSort = NZ(DMax("Sort", "tblDetail", "[ID] = " & _

[Forms]![frmMain]![txtID])) + 1

End Sub


You can also alter this to use the parent property instead of using the
fully qualified reference to the mainform:

me.txtSort = NZ(DMax("Sort", "tblDetail", "[ID] = " & _
me.parent.[txtID])) + 1
 

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