Form Doesn't Close on First Click of Button (1 Viewer)

DMerchen

Registered User.
Local time
Today, 14:47
Joined
Sep 9, 2008
Messages
94
I have a form with a close button on it. When I press the close button, I want it to close the form without saving the data. When I first open the form the close button doesn't work the first time I click it. It works every time after that, but I can not figure out why it doesn't work the first time. What am I doing wrong, or what am I missing?

Code:
Private Sub cmdOpticClose_Click()
Dim response As Integer

If IsNull(Me.Title) And IsNull(Me.Text13) And IsNull(Me.Text15) Then

response = MsgBox(prompt:="Close form without saving?", buttons:=vbYesNo)
 
   If response = vbYes Then
      Cancel = True
      Me.Undo
      DoCmd.Close
   Else
      Cancel = True
   End If
End If

End Sub

Thanks for the assist!!
Dave
 
I would guess that your form is bound to a recordsource and when it is opened, it will load the first record and populate at least one of your text boxes ie nothing happens on clicking your button. Then you go to a new record where all the text boxes are empty and the button does work.

You can test by putting a break point on the first line of the procedure.
 
It is acting strangely like I am not handling something. There is a form with a bunch of buttons, and one of the buttons opens this form. If I run the database normally, this first form loads, then I can open this other form. If I run it this way, I have to basically hit the close button twice. If I open the database with the shift key held and just run that one form, it behaves just like it should and closes the window on the first go.

Any idea what might cause this?

Unfortunately, I inherited this database, so I am working through all of the problems. One that drives me crazy is when a control or textbox is created and the default name is not changed.:banghead:

Thanks for the assist once again!
Dave
 
Okay, even weirder. If I open the database by holding the shift key down. I then run the startup form when the database runs normally, select my form, everything works great. All of the forms work as advertised. As soon as I run the database and the default startup form opens, then my form doesn't work the same. Then I have to basically hit the close button twice in order for the form to close. Something running when the database starts??

Any help is appreciated.
Dave
 
For what use do you set Cancel = True/False, where do you use it?
Code:
 ...
  If Response = vbYes Then
      Cancel = True
      Me.Undo
      DoCmd.Close
   Else
      Cancel = True
   End If
Make it any difference if you specify the form to close?
Code:
DoCmd.Close acForm, "YourFormName"
 
Like JHB I can't see the purpose of the cancel=true

You asked if something is running at start up.
Is there an Autoexec macro?
 
Do I remove the Cancel=True statements completely, or leave the one in the Else statement. I borrowed the code from another post, so I am trying to understand myself. Learning at every opportunity.

Yes, there is a autoexec Macro. I found this last night, and I am trying to figure out if it is really needed. I don't see the purpose for it. It looks like it closes the database automatically after time has elapsed without any changes. So if someone is working on it, steps away, there is no input and it closes the DB automatically.
 
Having a hard time following your scenario, but having to hit the close button twice, the first time you attempt it, suggests that the Form doesn't have Focus, initially...the first click moves Focus to the Form...the second click executes the button's code.

Linq ;0)>
 
I got this figured out...with everyone's help of course! There is an Autoexec Macro that runs on startup and opens a hidden form. It is a timer to test if the database has been sitting idle, then exits the database automatically once the specified time has expired. When I executed the close, the first form that opens with the Autoexec Macro closes, then I have to hit the close button again to close my form. Basically, when I hit close the first time, I am defeating the timer, so the database will stay open forever. JHB you were correct and using the code you suggested closes the actual form and keeps the timer going.

Thank you for all of your help!!
 
Just for info, the hidden form you've disabled can be very useful for kicking users out of a multi-user database when system maintenance is required.

Its especially useful for managing those users who leave a database open when its not in use otherwise they can prevent you being able to maintain it properly

If properly designed, it should NOT have the side effect you described!

By coincidence I've been explaining how to ADD that feature properly to another user in this thread:
https://www.access-programmers.co.uk/forums/showthread.php?t=295846

All necessary items / code were supplied

So IF the db is in a multi-user environment, can I suggest you look through that thread ...

HTH
 
Thanks ridders! I believe the form is working properly, it was the new code I was trying to implement that simply didn't take this into account. Now that I am aware of it, I know how to handle it.

Thank you for the information. I hadn't thought of using this as a tool for maintenance. Good info! I look forward to reading through the other thread!

Thanks!
Dave
 
Just for general discussion, having an unqualified DoCmd.Close is ALWAYS dangerous because without a name, because it is supposed to close the object in the open window. But if there is a hidden form in the same window (such as might be the case for a form that wasn't maximized), it is remotely possible to take the hidden object first - which would be consistent with your observation that the first close didn't SEEM to close anything.

The DoCmd.Close COULD make sense in that location if it wasn't trying to close itself, but because of it being unqualified (no parameters), you can't look at that code and instantly know what it is trying to close. While I know having defaults relieves you of extra typing, it DOESN'T relieve the developer / maintainer of the obligation to know what is open at the time in order to BE closed.

The reason you were questioned about "Cancel" is because that is the mechanism used to abort an event chain, BUT it only applies to the first event of a given chain. E.g. Form Open(Cancel) >> Load >> Current >> Activate >> Enter (or maybe Enter >> Activate, I never remember which.) You can stop a form from opening by returning TRUE to the Cancel variable in the form Open event, but after that, the form WILL open.

When you use the Event Wizards to build form events, they automagically build in the Cancel linkage IF AND ONLY IF the event can be canceled. If the event builder doesn't add the Cancel argument, then Access doesn't think that event CAN be canceled. And while it is true that in general, the wizards are dull and unimaginative, they are also never wrong (in my own experience) in details of this nature.

SO... the reason you were questioned about the Cancel is because there was not built-in linkage to a Cancel variable from the event call sequence, and having a global variable called Cancel will normally not work due to local vs. global "variable scope" rules.
 
Thanks Doc! Very good information. It is greatly appreciated!
 

Users who are viewing this thread

Back
Top Bottom