Check if cmd button has been clicked (1 Viewer)

tt1611

Registered User.
Local time
Today, 01:39
Joined
Jul 17, 2009
Messages
132
Hi all
I have a simple form with 3 text boxes and 2 command buttons. The buttons are for saving records entered on the form and other is to close the form on the basis of a warning message box ie

Code:
if confirm you wish to close = vbyes then close the window
 
else
cancel = true 'leave window open'

What I would like to incorporate is for access to check to see if the save button has been clicked. If this condition is true then once the user hits close, they will not be prompted with a warning. The window closes as normal.
else if the save button has not been clicked, then display warning message.

Is this at all possible.
I have a me.dirty condition on the close button that will check if data has been inputted into the form however the user still gets a warning prompt about unsaved data being lost even though they have clicked save before close.

Is this at all possible
Thank you in advance.
 

SpentGeezer

Pure Noobism
Local time
Today, 15:39
Joined
Sep 16, 2010
Messages
258
Try this, it uses an invisible label which changes if save has been clicked. There is probably a better way, but I am a NOOOOOB!!!!!!!!:D
 

Attachments

  • CHeckButt.mdb
    136 KB · Views: 476

vbaInet

AWF VIP
Local time
Today, 06:39
Joined
Jan 22, 2010
Messages
26,374
Let's see your Save record code. I think you're not using the right command there.
 

tt1611

Registered User.
Local time
Today, 01:39
Joined
Jul 17, 2009
Messages
132
Spentgeezer
I havent actually seen your code yet but i think i have an idea of where you might be going with the invisible label - that is definitely an optionn

Hey VBA
I am enclosing my curent save code. What are your thoughts. Let me know if you need any clarification. Just an fyi, my save code is doing exactly what its supposed to.


Code:
Private Sub cmdclose_Click()
If Me.Dirty = True Then
If MsgBox("All unsaved data will be lost. Are you sure you wish to close?", vbYesNo + vbExclamation, "Confirm close") = vbYes Then
Me.Undo
DoCmd.Close acForm, "WeightRecord"
Else
Cancel = True
End If
Else
DoCmd.Close acForm, "WeightRecord"
 
End If
End Sub
Private Sub cmdsaveR_Click()
If MsgBox("Are you sure you wish to save these measurements", vbYesNo + vbQuestion, "Confirm save") = vbYes Then
'DoCmd.RunCommand acCmdSaveRecord
Dim a, b As Double
Dim c As String
a = CDbl(Me.txtweight - Me.txtgoal)
b = DLookup("MostRecent", ("LastWeight"))
 
If b < Me.txtweight Then
c = "You have gained" & " " & (Me.txtweight - b) & "lbs since your last weigh in"
MsgBox "Cardio like crazy. We gained today", vbOKOnly + vbInformation, "Break out the fat burner"
 
Else
c = "You have lost" & " " & (b - Me.txtweight) & "lbs since your last weigh in"
MsgBox "Good Job mommy..we lost some weight", vbOKOnly + vbInformation, "CONGRATULATIONS"
 
End If
Me.txtnote = "You are" & " " & a & "lbs away from your goal" & vbNewLine & c
End If
End Sub

The close button will always prompt for lost data when clicked as you can see. I need to be able to check that if at some stage this button has been pressed then run some other code to exit without prompt (I havent written the code to close the window without the prompy yet but you get the idea).

Hence I am thinking spentgeezers idea of tagging an invisible label when the save button is clicked and have a check in the close button check for a value in the label may just work out for me.
 

smig

Registered User.
Local time
Today, 08:39
Joined
Nov 25, 2009
Messages
2,209
use a private boolean variable to save the data.
(declare it on top of all subs/functions to make it available to all subs/functions in form)
set it to false when form is loaded, change it to true when the button you look for clicked.

why do you need two buttons? why not simply close the form after you save the data (save and close)?
 
Last edited:

tt1611

Registered User.
Local time
Today, 01:39
Joined
Jul 17, 2009
Messages
132
Thank for your input. In fact this is the exact idea i was considering following a suggestion from spent but I think the boolean variable should also suffice.
I think i spent too much time trying to see if the command button had a property of some sort that would actually store click events.

Anyhow in answer to your question, I have a save and close button separate because this form can also be used to review data as well as input.
Based on search criteria, the form can be called by double clicking its recordid in another subform where the user can review specific records.

Thank you for your help. Ill run this boolean code tonight and see how it works out.
 

vbaInet

AWF VIP
Local time
Today, 06:39
Joined
Jan 22, 2010
Messages
26,374
I see you're using the right command there DoCmd.RunCommand acCmdSaveRecord. Why is it commented out? If you don't call that command then Me.Dirty will remain True.

Also, if Me.txtNote is bound, the form will become Dirty again and as a result the Close code will not work. Finally, if the form's 'X' close button is enabled and used to close the form, your close code (obviously) will not fire.

I've tidied up your code.
Code:
Private Sub cmdclose_Click()
    If Me.Dirty = True Then
        If MsgBox("All unsaved data will be lost. Are you sure you wish to close?", vbYesNo + vbExclamation, "Confirm close") = vbYes Then
            Me.Undo
            DoCmd.Close acForm, "WeightRecord", acSaveNo
        End If
    Else
        DoCmd.Close acForm, "WeightRecord", acSaveNo
    End If
End Sub
 

tt1611

Registered User.
Local time
Today, 01:39
Joined
Jul 17, 2009
Messages
132
Thanks vba
The save command was commented due to debugging in design mode. The close button on the form is disabled hence a close command button (too much drama in the past with unloading a form before closure - this makes life easier). Me.txtnote is unbound. I threw it on the form mainly for information only.

I appreciate your feedback. I mentioned earlier following a suggestion from smig to use a global variable that will store boolean true when the cmd is clicked. Ill let you know how I get on.

Thanks
 

vbaInet

AWF VIP
Local time
Today, 06:39
Joined
Jan 22, 2010
Messages
26,374
I'm not completely convinced that using a boole variable will be the most effecient way. There must be a reason why Dirty doesn't remain as True when a record is updated.

Can we see a stripped down version of your db.
 

KiEESH

Registered User.
Local time
Today, 01:39
Joined
Jan 16, 2013
Messages
32
I have a form that includes a basic macro-based "Save" button.


Building VBA code on the OnUnload event that:

A) Prompts user to complete required fields if "Save" button was clicked.
B) Checks if "Save" button was not clicked.
Then, verifies if user really doesn't want to save.
If not, then changes are undone and unsaved.


Problem: Not sure how to check if macro-based button was clicked.


Code:

Private Sub Form_Unload(Cancel As Integer)
Dim saveAns As Integer
'If <<Macro-based 'Save' Button IS Clicked>> Then


If IsNull(Me.BLOCK) Or IsNull(Me.LOT) Or IsNull(Me.ZONE) Or IsNull(Me.BLDG_NUM) Or IsNull(Me.STREET) Or IsNull(Me.ATERY) Or IsNull(Me.ADDED_BY) Or IsNull(Me.OIL_TYPE) Then
Cancel = True
MsgBox "Some required fields have not been completed."
End If



'If <<Macro-based 'Save' Button IS NOT Clicked>> Then


saveAns = MsgBox("Exit without saving?", vbYesNo)

If saveAns = vbYes Then
Me.Undo
Me.Hide: Unload Me
DoCmd.OpenForm "SWITCHBOARD"
Else
Cancel = True
End If
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 06:39
Joined
Sep 12, 2006
Messages
15,710
tt

seriously - having buttons to save records is unnecessary and can be confusing to experienced access users.

eg - if you choose to show record selectors, then you can save a record by clicking the pencil

it actually is quite hard to prevent users saving records in the standard way. (there are several ways to save records)

why do you want to do it that way.
 

KiEESH

Registered User.
Local time
Today, 01:39
Joined
Jan 16, 2013
Messages
32
This is an Add Only form.

I have chosen to hide navigation buttons, because I don't want them used.

We don't want users to see previous records.

Also, the DB will be used by very inexperienced users, who are very much accustomed to seeing "Save" and "Exit" buttons.

I am also open to converting my Save button to VBA if that makes things easier.
 

tt1611

Registered User.
Local time
Today, 01:39
Joined
Jul 17, 2009
Messages
132
Hey Gemma
To be honest - I would say most of the forms I have built in the past are the more user friendly with save buttons and confirmation messages that save was successful or failed.

Kieesh - in answer to your question, the approach i have used is as below.

Declare some global variable

Code:
Dim mybuttonclicked as boolean

On your form load event, set this to false

ie
Code:
mybuttonclicked = false

On your Macro-based 'Save' Button click event
let your code fire the macro and then set this variable to true. That tells VBA that the button has infact been clicked.

On your unload event all you would then have to do is check whether the variable is set to true or false

Code:
if mybuttonclicked = true then

bla bla

else

bla bla

end if
 

Users who are viewing this thread

Top Bottom