Suggestions:
1. Never use Form_Current to assign a value to a bound control. There is no
point in dirtying the record just because you visited it. You are creating
many unnecessary writes, and increasing the chance of errors (e.g.
concurrency problems.)
2. Do you really need an event? If you set up your subform so the
LinkMasterFields/LinkChildFields work, you don't need any code at all, and
the child fields inherit the value of the master fields.
3. If you can't do that, use Form_BeforeInsert. This event fires when the
user starts entering something (so only when needed.)
The example below assumes that:
- Nrprojektu is a Text field (not a Number field.)
- Nr_poz is a Number field (not a Text field.)
- The subform has fields or controls named Nr_artykulu and nrpoz.
- You don't have to worry about multiple users entering data at the same
time, and getting the same number assigned.
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim strWhere as String
With Me.Parent
If .NewRecord Then
Cancel = True
MsgBox "Enter a record in the main form first."
Else
strWhere = "[Id_projektu]= """ & !Nr_artykulu & """"
Me.nrpoz = Nz(DMax("[Nr_poz]", "[ZESTAWIENIE_MATERIALOW]",
strWhere), 0) + 1
End If
End With
End Sub
--
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.
"Maciej Franciszkowski" <ma******@poczta.onet.plwrote in message
news:57**********************************@z72g2000 hsb.googlegroups.com...
Hello,
I dont know why only else part of if statement is effected when using
below
Private Sub Form_Current()
If Me.NewRecord Then
Me!nrpoz = DMax("[Nr_poz]", "[ZESTAWIENIE_MATERIALOW]",
"[Id_projektu]= '" & Me.Parent.Nr_artykulu & "'") + 1
Else
Me!nrpoz = Me!Nr_poz
End If
End Sub
Isn't it new record when entering first data to a row?