Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this OnCurrent code?

Status
Not open for further replies.

dylan03

Technical User
Mar 17, 2003
15
0
0
US
I have a Sales form which needs to automatically enter a TransactionID. I want the number entered to be in the format of 2002-### )the second portion being the next sequential number.

In [Sales.TransactionID] I have set the input mask to >0000\-00;0

The field name on the form is [TransID]

In the OnCurrent property of the form, I have the following code:

Private Sub Form_Current()

If Me.NewRecord = True Then
If IsNull(DMax(Sales.TransactionID)) Then
TransID = Right(Year()) + DMax(Sales.TransactionID)
Else
TransID = Sales.TransactionID
End If

End If


End Sub



PLEASE HELP! I really got myself into a pickle trying to figure this out!
 
Sorry to disappoint you, but everything is wrong:
1. Current event is NOTsuitable for your purpose, as it will always write to a new record, even though you're just navigating... Correct event for this: BeforeInsert for single user applications and Form_BeforeUpdate (together with some locking routine) for multi-user

2. DMax syntax is wrong. Correct:
DMax("FldName", "TblQryName", "CriteriaIfAny")
And you should use the criteria for what you need.

3. The Right syntax is wrong. Correct:
Right(Expression, NoOfChars)

So...
Private Sub Form_BeforeInsert(Cancel As Integer)
TransID = Val(Year(Date) & Format(nz(DMax("TransactionID","Sales","Left(TransactionID & " ", 4) = Year(Date())),1),"000"))
End Sub


Or, with BeforeUpdate:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Start:
'If Locking routine of your choice allows
If Not Locked Then
TransID = Val(Year(Date) & Format(nz(DMax("TransactionID","Sales","Left(TransactionID & " ", 4) = Year(Date())),1),"000"))
Else
'Else go back to start until locking routine allows
Cancel = True
GoTo Start
End If
End Sub

This may get you into an infinite loop, but you can add a counter to the retries... It's just an idea...


HTH

Dan [pipe]
Daniel Vlas
Systems Consultant
 
Thanks for your help, Daniel!! One quick thing....

When I insert the Before Insert Code into my form, I receive a Microsoft Visual Basic compile error that states "Expected: list seperator or )". In the VB window, it highlights this section of the code:

", 4) = Year(Date())),1,"

Can you help me again?? Thanks, Daniel!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top