How come form saves data without clicking saverecord button?

rodvaN

Registered User.
Local time
Today, 09:09
Joined
May 20, 2009
Messages
92
Hello..
I got a Form that introduces info into a table..
When I edit (Me.field.Enabled = True ) is on. Then I change info but I do not click on saverecord button, it stills saves the info, the same with the new records.
How can I make it obligatory to click on save button to be able to modify or add new records?.. and when I modify in case of not clicking saverecord, then not modifying anything?.
Thanks in advance
 
because when you close a form, any active item is saved, so you need to test this before the form closes

you could test this in forms unload event (i think)

Code:
if me.dirty then
   cancel = true
   msgbox("Save or Scrap current record")
   exit sub
end if

i think this will work
 
i think this will work
Nope, you have to cancel in the form's BEFORE UPDATE event. If you get to the Unload event it has already been saved.
 
SO how can I add a function on the form that doesnt saves the record until I click on saverecord button, and If I was addding a new record but decided to close the form and not clicked the saverecord button, then it displays a message that it doesnt saved, that if i would like to proceed.
Is this possible?
 
Create a variable in the Declarations section of your form module:
Code:
Private blnSave As Boolean
In the click event of your save button use:
Code:
If Me.Dirty Then Me.Dirty = False
blnSave = True

In the BEFORE UPDATE event of your form use:
Code:
If Not blnSave Then
   ...Check your fields here to see if they have enough to save and if not then
      Cancel = True
      If MsgBox("You have not filled in all of the fields." & vbCrLf & _
           "Do you want to cancel this record?", vbQuestion + vbYesNo, "Cancel?") = vbYes Then
            Me.Undo
      End If
End If

(air code - not tested)
 
If Not blnSave Then
...Check your fields here to see if they have enough to save and if not then
Cancel = True

How I check my fields therE?
Which would be the code?
Thanks for fast response SOS
 
How I check my fields therE?
Which would be the code?
Thanks for fast response SOS

Don't know all of your fields but something like:
Code:
If Len(Me.YourTextBox1 & "") = 0 Then
   Cancel = True
   If MsgBox("You have not filled in TextBox1." & vbCrLf & _
                 "Do you wish to cancel this record?", vbQuestion + vbYesNo, "Cancel?") = vbYes Then
      Me.Undo
   End If
End If
 
Nope, you have to cancel in the form's BEFORE UPDATE event. If you get to the Unload event it has already been saved.

yep you are right

i do use unload event for some similar things - like checking booleans in some cases - obviously beforeupdate is one to go for.
 
Don't know all of your fields but something like:
Code:
If Len(Me.YourTextBox1 & "") = 0 Then
   Cancel = True
   If MsgBox("You have not filled in TextBox1." & vbCrLf & _
                 "Do you wish to cancel this record?", vbQuestion + vbYesNo, "Cancel?") = vbYes Then
      Me.Undo
   End If
End If


Ok created what you said before, I made a declaration for blnsave
Then I entered the code in the save button.
But this last code before update I got it like this.
Code:
Private Sub Entradas_beforeupdate()
If Len(Me.Lote & "") = 0 Then
   Cancel = True
   If MsgBox("You have not filled in LOTE." & vbCrLf & _
                 "Do you wish to cancel this record?", vbQuestion + vbYesNo, "Cancel?") = vbYes Then
      Me.Undo
   End If
End If
End Sub

Lote its because I got a textbox called Lote.But what If I like to add more?

I would like to show a message displaying that there was changes in the form when trying to go next form(or previous), that if I would like to save them ?, or when trying to do a new entry when i modified data in the fields..
Im really confused SOS.. sorry for all this, Im realy a newbie in VBA.
Thanks for all answers
 
You could do something like this:
Code:
Dim blnCancel As Boolean
 
If Len(Me.Lote & "") = 0 Then
   blnCancel = True
End If
 
If Len(Me.Field2 & "") = 0 Then
   blnCancel = True
End If
 
If Len(Me.Field3 & "") = 0 Then
   blnCancel = True
End If
 
If Len(Me.Field4 & "") = 0 Then
   blnCancel = True
End If

If blnCancel = True Then
   Cancel = True
   If MsgBox("You have not filled in LOTE." & vbCrLf & _
                 "Do you wish to cancel this record?", vbQuestion + vbYesNo, "Cancel?") = vbYes Then
      Me.Undo
End If
End Sub
 
Thanks for the help SOS and Husky..
I used the following code on each button I have on the form, unless the saving button.

Code:
If Me.Dirty Then
      MsgBox "Changed not made, please click on save button before browsing.", vbExclamation, "buhorus: change not made."
      Me.Undo
    End If

Thanks for the ideas!
 

Users who are viewing this thread

Back
Top Bottom