saving form records when changes

R

rudwan

i have a form , i want during typing or reviewing data <
to aske wethere user to save what he change ,
i design this code :


Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim m As Integer
Dim ctl As Control

Dim intnewrec As Integer

intnewrec = Me.NewRecord
If intnewrec = True Then
MsgBox " you insert a new record "
Else

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Or
ctl.ControlType = acComboBox Then
If ctl.OldValue <> ctl.Value Then
m = InputBox(ctl.ControlName & " value
already changed Enter your password to save ?")

If m = 1 Then

Cancel = False


Else
Cancel = True
ctl.Undo
End If


End If
End If


Next ctl
End If

End Sub

it works ok but for some fields , if i changed some fields
it allert me , in other controlls it changed without allert
but notice , some controlls are text , the other are number
 
A

Allen Browne

The cases that are slipping through the cracks are probably those where
there is a null value. You could avoid the problem like this:

If Not Me.NewRecord Then
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
If (ctl.Value = ctl.OldValue) Or (IsNull(ctl.Value) And
IsNull(ctl.OldValue)) Then
'do nothing
Else
MsgBox ctl.ControlSource & " changed from " & ctl.OldValue
& " to " & ctl.NewValue
End If
End If
Next
End If

More info on nulls:
http://allenbrowne.com/casu-12.html

BTW, do you really want to ask the user to enter their password repeatedly?
Once for each of the fields that were changed? Do you have field-level
passwords here?
 
R

rudwan

hi
i think it is a good idea , but the problem now is when
the code rech to this line :
If (Ctl.Value = Ctl.OldValue) Or (IsNull(Ctl.Value) And
IsNull(Ctl.OldValue)) Then

it send me message :
object , table , macro ; ms acces cannot fined it
in spite of ctl defined already as object
 
A

Allen Browne

At the top of this routine, try defining it as a Control instead of an
Object, i.e.:
Dim ctl As Control

Not all controls have a Value, but the code has already eliminated that
issue by limiting itself to text boxes and combos. Not all text boxes and
combos have an OldValue, e.g. those bound to read-only fields. Could that be
the issue?

There is also a problem with bound controls in a form that has no records
and no new records can be added.
 

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