Link to home
Create AccountLog in
Avatar of Richard
Richard

asked on

access requery/refresh/dirty

I have a subform.
The form creates invoices and gives a unique conseq number to a field in that form called UniqueInvoiceID by virtue of the D Max function.

There is a filed called DateOfInvoice with a date picker.

What I need is when the date is entered the UniqueInvoice ID is puled thru as this only seems to happen when the user (multi-user) leaves the form.

It needs to refresh/requery/calculate and enter the Dmax funnction when the date picker picks a date.

Ideas?
Avatar of Mike77
Mike77

How is the number given right now? As the default value of the field? If so, it is not up to date and may generate duplicates, and it is not saved in the database until the user either changes record or closes the form.

I would calculate it in the after_update event of the date field, then issue a "docmd.runcommand accmdsaverecord" to ensure it is saved in the database immediately. Be sure to add some error checking for duplicates anyways, because it could happen that another user adds a record between the time you assigne the value to the invoice ID and the save record.

Hope it helps,
Mike
Avatar of Richard

ASKER

Interesting as I have calculated it in the Unique ID field.

I have been struggling for some error code ----could you think of any?

Thanks,.
This would be assuming that the UniqueInvoiceID is indexed to allow no duplicates and that there are no other required fields, it's not the most elegant way, and we should probably add some maximum number of tries to ensure it never goes into and endless loop, but it should work :

Private sub DateOfInvoice_AfterUpdate
On Error GoTo Err_DateOfInvoice_AfterUpdate

	If nz(UniqueInvoiceID,"") = "" Then
		
GenerateNewID:
		Me.UniqueInvoiceID = DMAX("UniqueInvoiceID","TableName")
		Docmd.RunCommand acCmdSaveRecord

	End If

Exit_DateOfInvoice_AfterUpdate:
	Exit sub

Err_DateOfInvoice_AfterUpdate:
	If err.Number = 3022 Then  'Error returned when the value already exists
		Resume GenerateNewID
	else
		msgbox err.number & " - " & err.description
		Resume Exit_DateOfInvoice_AfterUpdate
	end if

End sub

Open in new window

Avatar of Richard

ASKER

Thanks, looks promising. Will test and return.
Oops, just noticed I didn't add a +1 to the DMax in that code example, be sure to add it or it's a definite endless loop hehe :

DMAX("UniqueInvoiceID","TableName")+1
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Richard

ASKER

fyed,

Ive got that thanks. It works.

I was just looking for a way to update the new DMAX filed without logging out.