show a message box if none of the objects in the form was changed (1 Viewer)

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
how can I tell the access 2007 that if no text boxes was changed so when I click on save button don't save and show a message box that says " you don't change any Information"
 

VilaRestal

';drop database master;--
Local time
Today, 14:14
Joined
Jun 8, 2011
Messages
1,046
Me.Dirty
Is true if anything has been changed and the record needs saving.

So code something like:
Code:
If Not Me.Dirty Then 
    MsgBox "No changes have been made", vbExclamation, "Nothing to Save"
Else
    RunCommand acCmdSaveRecord
End If
 

boblarson

Smeghead
Local time
Today, 06:14
Joined
Jan 12, 2001
Messages
32,059
However, Me.Dirty can be true if nothing has changed. If a field has been changed but changed back, it is still dirty. So you can't rely solely on that. If you really care then you would need to iterate the controls to check their .Value with their .OldValue to see if they are the same and if they were not you could set then exit the loop and tell them that there is stuff to save and if not the other message.
 

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
Dear Vila and bob

thank you for your kindly help

Bob! could you please help me with writing the code? I'm not good in vba.
 

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
How can I tell access stay on the current textbox if it is null?

i wrote this:

Private Sub ORGANIZATION_NAME__LostFocus()
If IsNull(ORGANIZATION_NAME_) = True Then
MsgBox "Please enter organization name."
Me.ORGANIZATION_NAME_.SetFocus
End If
EndSub

if the textbox is empty, when I press tab key, it shows the message box but after click on ok, it goes to next text box. but I want it stay on current textbox.
 

VilaRestal

';drop database master;--
Local time
Today, 14:14
Joined
Jun 8, 2011
Messages
1,046
The usual way to do it is on the BeforeUpdate event of the textbox:

Code:
Private Sub ORGANIZATION_NAME__BeforeUpdate(Cancel As Integer)
    If Nz(Trim(ORGANIZATION_NAME_.Value),"") = "" Then
        MsgBox "Please enter organization name."
        Cancel = True
    End If
End Sub
 

derekburleigh

New member
Local time
Today, 15:14
Joined
May 16, 2011
Messages
14
If you need to check that the user has not left controls blank, set the
field's Required property in table design. That will automatically prompt them for data in the field.
 

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
Dear Derek!
I know what you say but I don't want the access tell the user that inter organization name when I want to save the record,
what I want is, access don't allow the user leave the textbox until the textbox be filled
 

derekburleigh

New member
Local time
Today, 15:14
Joined
May 16, 2011
Messages
14
Hi Samira,

OK, I know what you are saying, just thought it may be easier this way. :D

I had a similar problem to yours a while ago and solved it eventually, where access insisted on tabbing to the next field. Can't remember exactly which database it was in though, I'll have a hunt around and see if I can find the code I used to solve the problem and post it here for you.

Speak soon!!
 

Trevor G

Registered User.
Local time
Today, 14:14
Joined
Oct 1, 2009
Messages
2,341
Check Fields Validation

Create a form and select design view. Select the Fields you need to be completed. Open the properties and select Tag (Check the other Tab). Add a word in this case “Vital”.


Private Sub cmdButton_Click()
Dim ctl as Control
Dim Flag as Boolean

For Each ctl in Form_Name
If ctl.tag=”vital” Then
If isNull(ctl.Value) Then
Ctl.BackColor=vbRed
Flag=True
Else
Ctl.BackColor=vbWhite
End If
End IF
Next
If Flag = True Then
MsgBox”Please Enter data in the fields indicated with a red background"
Else
DoCmd.RunCommand acCmdSaveRecord
End If
End Sub
 

derekburleigh

New member
Local time
Today, 15:14
Joined
May 16, 2011
Messages
14
Hi Samira,

Found it!! This works.

Private Sub ORGANIZATION_NAME__LostFocus()
If IsNull(ORGANIZATION_NAME_) = True Then
MsgBox "Please enter organization name."
Me.ANYOTHERFIELD.SetFocus
Me.ORGANIZATION_NAME_.SetFocus
End If
EndSub

Don't know why, but that was the only way I could get it to work.
 

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
Thank you Derek!

thank you thank you thank you very very very mmmmmmmmmmmmmmuch
 

s_samira_21

Registered User.
Local time
Today, 17:44
Joined
Jun 8, 2011
Messages
52
what about my first question??
how can I tell the access 2007 that if no text boxes was changed so when I click on save button don't save and show a message box that says " you don't change any Information"
 

derekburleigh

New member
Local time
Today, 15:14
Joined
May 16, 2011
Messages
14
Hi Samira,

Just curious, but why does it matter if they haven´t changed any fields? Just do the save action, probably less processing involved in that than the code to check for changes!

Otherwise use the Me.Dirty suggestion from above, this will be correct in all cases except if they change a field and then change it back to it´s original value, in which case Me.Dirty will be true even though the values have not changed, are the users likely to do this?

Failing that I think the only way to be absolutely sure to loop through all the controls comparing .Value to .OldValue. which is a bit tiresome!

I´ll see if I can think of an easier way..........
 

Users who are viewing this thread

Top Bottom