code to determine whether data is new or modified (1 Viewer)

eggwater

life as a rehearsal
Local time
Today, 02:56
Joined
Aug 14, 2003
Messages
69
I'm building a form and I have the following code which works to a degree:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.date_stamp = Now()
Me.Export_ctrl = "2"
End Sub

the code adds a modification date if a record is modified and also flags the record as a '2' which means it has been amended..this works...

however... when I add a new record instead opf saving it as a '1' flag (i.e. new) it saves it as a '2'

Is there a way of coding that will determine whether the record is new or modified?

cheers in advance
 

Mile-O

Back once again...
Local time
Today, 02:56
Joined
Dec 10, 2002
Messages
11,316
Code:
If Me.NewRecord Then
 

eggwater

life as a rehearsal
Local time
Today, 02:56
Joined
Aug 14, 2003
Messages
69
sorry to be a pain

but how does that code fit with my original code

I'm not too familiar with VB (if this wasn't obvious!)
 

SilentBreaker

Registered User.
Local time
Today, 02:56
Joined
Aug 7, 2003
Messages
28
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

	If Me.Dirty or Me.NewRecord Then
	'add statements
	'....
	'....
	'To cancel the change or new record
	Cancel = True
	Else
	'accept new or changed record
	'....
	'....
	End If

Or you can do the other way around

Cheers :D
 

eggwater

life as a rehearsal
Local time
Today, 02:56
Joined
Aug 14, 2003
Messages
69
this is my code

it appears to be working actually
 

eggwater

life as a rehearsal
Local time
Today, 02:56
Joined
Aug 14, 2003
Messages
69
this was my code

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.date_stamp = Now()
Me.Export_ctrl = "2"
If Me.NewRecord Then Me.Export_ctrl = "1"

End Sub



it doesn't accept an end if does this matter?
 

Mile-O

Back once again...
Local time
Today, 02:56
Joined
Dec 10, 2002
Messages
11,316
Try this:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    With Me
        If .Dirty Then
            If .NewRecord Then
                .date_stamp = Now()
                .export_ctrl = "1"
            Else
                .date_stamp = Now()
                .export_ctrl = "2"
            End If
        End If
    End With
End Sub
 

eggwater

life as a rehearsal
Local time
Today, 02:56
Joined
Aug 14, 2003
Messages
69
spot on

cheers pal!! it works a treat

that looks a lot more pro than what i coded (although it did appear to be working)

excellent
 
R

Rich

Guest
You don't need the Dirty part, the Before Update event will only fire when the record is dirty, so it's redundant code
 

Users who are viewing this thread

Top Bottom