X saves the record

Pusher

BEOGRAD Put
Local time
Tomorrow, 00:51
Joined
May 25, 2011
Messages
230
Hi all,
I made a Msgbox popup when i press make new record that did not fill all the conditions for making a new record. But when i click x on my window it makes a new record in my table. How do I disable this?
Also what happens when you click on the x of the window and how can i implement a msgbox that will prompt my exit, and maybe condition it on something else?
Here is my Button code if you need it.
Code:
  Private Sub Nova_intervencija_Click()
  On Error GoTo Err_Command76_Click
   
  Cancel = False
  ' If Field1 is Empty then Cancel = True
  Cancel = Cancel Or (Nz(Me.DATUM_IZVESTAJA, "") = "")
  ' If Field2 is Empty then Cancel = True
  Cancel = Cancel Or (Nz(Me.ID_RASKRSNICE, "") = "")
  Cancel = Cancel Or (Nz(Me.ID_OSOBE_PRIJAVE, "") = "")
  Cancel = Cancel Or (Nz(Me.ID_STANJA_PRIJAVE, "") = "")
  Cancel = Cancel Or (Nz(Me.VREME_KVARA, "") = "")
   
  If Cancel = True Then
  ' If Cancel = True then show message
    MsgBox "Ako polja Datum, Raskrsnica,Osoba Prijave,Stanje prijave i Vreme kvara nisu popunjene ne možete da otvorite sledeæu intervenciju.", vbInformation + vbOKOnly
    
    Else
    DoCmd.GoToRecord , , acNewRec
  End If
   
  Exit_Command76_Click:
      Exit Sub
   
  Err_Command76_Click:
      MsgBox Err.Description
      Resume Exit_Command76_Click
      
  End Sub
Thanks
 
It's because you use the wrong event to validate a record, use the event
Form_BeforeUpdate which has a Cancel argument.

The all you need in your button click event is this:

Code:
Private Sub Nova_intervencija_Click()
If Me.Dirty Then Me.Dirty = false
End Sub

That will cover all the bases, you click button will try and save and the beforeupdate event will validate the record

JR
 
So What do i code where? Something like this? Where do i put Me.Dirty?
Code:
Private Sub Form_BeforeUpdate()
 If IsNull(Me.DATUM_IZVESTAJA) Or IsNull(Me.ID_RASKRSNICE) Or IsNull(Me.ID_OSOBE_PRIJAVE) Or IsNull(Me.ID_STANJA_PRIJAVE) Or IsNull(Me.VREME_KVARA) Then
        MsgBox "All Required Fields must hold data before can move to, or create, another record"
        Cancel = True
        Exit Sub
    End If

End Sub
 
Fist of that code would not even compile because of a missing parameter in the event statement.

Should be:
Code:
Private Sub Form_BeforeUpdate([COLOR="Red"]Cancel As Integer[/COLOR])
 If IsNull(Me.DATUM_IZVESTAJA) Or IsNull(Me.ID_RASKRSNICE) Or IsNull(Me.ID_OSOBE_PRIJAVE) Or IsNull(Me.ID_STANJA_PRIJAVE) Or IsNull(Me.VREME_KVARA) Then
        MsgBox "All Required Fields must hold data before can move to, or create, another record"
        Cancel = True
        Exit Sub
    End If

End Sub

Where do i put Me.Dirty?

Behind your "Save"-button's click event. It will only fire if the record has been dirtied.

You really do not need a "save" button on a bound form since Access automatically tries to save when you move off the record or close the form. If the Forms_BeforeUpdate event fails to validate the data, then the record would not be saved to the table unless your users correct their entry.

JR
 
Where do i put If Me.Dirty Then Me.Dirty=False so i can’t make new records but can go back thru old records? I put it in make new record and move to record
Code:
[B]If Me.Dirty Then Me.Dirty = False
DoCmd.GoToRecord , , acNext[/B]
and
Code:
[B]If Me.Dirty Then Me.Dirty = False
DoCmd.GoToRecord , , acNewRec[/B]
And now it fires also when i want to go back thru old records... Help
 
Lets start at the beginning, you stated this in your first post:

But when i click x on my window it makes a new record in my table. How do I disable this?


Well the answer is that you rely on your users to use your validation/recordmove button "Nova intervencija", and when they don't you get into trouble. So the answer to this problem is to use the CORRECT event to validate records which is the FORM_BEFOREUPDATE event.

Record events is as follows:

Current -> BeforeUpdate (Last chance to cancel an invalid entry) -> AfterUpdate (Record committed to table) -> Current (You are now at the next record)

Here is a site that list events in more detail: http://office.microsoft.com/en-us/access-help/order-of-events-for-database-objects-HP005186761.aspx

As for record navigation do you really need buttons for that? Access has that built in if you set Navigationbuttons to Yes on the property sheet of the form.

The natural way for my users to navigate is to just enter data in the controls and when they exits the last control on the form it jumps to the next record, and if required controls is not filled in the BeforeUpdate event of my form kicks in and stops the user form going further without correcting their mistakes.

If you want to create a custom navigation buttons see here: http://www.access-programmers.co.uk/forums/showpost.php?p=978335&postcount=2

JR
 
Last edited:

Users who are viewing this thread

Back
Top Bottom