Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot seem to CLOSE form in code

Status
Not open for further replies.

JaneC

Programmer
Jul 18, 2001
125
0
0
GB
I have virtually finished my database and have run into a slight glitch.

When adding a new record I need the form to close if do data is entered in the first field.

I have a check on LostFocus for the field which successfully deletes the incomplete record - but I cannot get it to then close the form.

Run-time error 2585
This action can't be carried out while processing a form or report event.

Clicking on Debug goes to the Close line in my code.


If IsNull(Me.ReqDate) Then
MsgBox ("No data entered - deleting record")

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
DoCmd.Close


End If


Any ideas gratefully received !

Jane

 
Cant close the form which the code is included in. MUST open/set focus to SONE other form (where are you going?). Close the previous form from there. Forms navigation needs some implementation. To Close the "Current" form, you need to know where (Which FORM) you will go to. You also need to keep track of where you came from. In the simplistic sense, you can have a single form (Switchboard?) you use as the default "GoTo" guy. This eliminated tracking the "where shall I go?" part. Still (normally) need a flag (global STRING) "ClosePrevForm"? Set the name of the current form into the flag, open the GoTo form. In the onCurrent event of the GoTo form, check the "flag". If it is enpty, do nothing, else close the form whose name id in it, then set it to the empty string.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thanks for your help Michael, unfortunately I'm still really struggling with this!

I've got a variable IsFirstNewRec that I define as public boolean on the main form (MainMenu). The onLoad event for this form sets it to False.

When I open the input form (RequestsForm) I set this to True if there are no records in the recordset. After I've deleted the incomplete record I move the focus to the first control on MainMenu.

In the onActivate event for MainMenu I check the value of IsFirstNewRec and if it is true I (try) to close the input form.

I'm still getting the same error message.

This is the code from requestsform that moves the control back to the mainmenu form.

If Forms.Item("mainmenu").IsFirstNewRec Then
MsgBox "setting focus back to mainmenu"
Forms![MainMenu].SetFocus
Exit Sub

End If

Is there a problem here with the way I am trying to go back to MainMenu ??

Please help me !!!!

Jane
 
I have always set focus by setting it to a specific control on a form, rather than a form.

However, I suspect you are actually getting the error message because the data is in effect floating. What command are you using to delete the incomplete record. Unless the user has moved record, the incomplete record will not have been saved, hence deleting it will be impossible, & as a result the delete process will still be trying to run.

Try changing the docmd.domenuitem... to:
docmd.runcommand accmdundo
me.refresh

That way it should undo the user input, & refresh itself as well. Hopefully this will finish any processes currently running. I would then point to a control on your main form:

[forms]![mainmenu]![controlname].setfocus
docmd.Close acForm, "frmName"

try that & see if it helps.... James Goodman
j.goodman00@btinternet.com
 
This appears to be "O.K." - by itself. However it does NOT cover the situation. How was "[RequestsForm]" opened? If it was opened 'modal', you cannot get to anyother form w/o closing it first. If it was opened non-modal, then how was "[MainMenu]" treated? If it was "CLOSED" somewhere, then setting focus may be problematic.

I've got a variable IsFirstNewRec that I define as public boolean on the main form (MainMenu).

I generally aviod having globally accessed variables declared in a forms module, as their scope (really existance in this case) is determined by the FORM. I would place this in a GLOBAL (general) module, then it's existance is guarnteed throughout the program session.

The onLoad event for this form sets it to False.

Generally, this should NOT be done - At least not in the On-Load event as an arbitrary line of code. Generally, the recordcount would be the criteria WHENEVER the status is set, as in " ... When I open the input form (RequestsForm) I set this to True if there are no records in the recordset ..."

Also, you are not supplying any agruments to the DoCmd.Close statement. From Ms. Help

"If you leave the objecttype and objectname arguments blank (the default constant, acDefault, is assumed for objecttype), Microsoft Access closes the active window. If you specify the save argument and leave the objecttype and objectname arguments blank, you must include the objecttype and objectname arguments' commas.

Note If a form has a control bound to a field that has its Required property set to 'Yes,' and the form is closed using the Close method without entering any data for that field, an error message is not displayed. Any changes made to the record will be aborted. When the form is closed using the Windows Close button, the Close action in a macro, or selecting Close from the File menu, Microsoft Access displays an alert. The following code will display an error message when attempting to close a form with a Null field, using the Close method."


Reviewing your original post, it APPEARS that this is part of te dateentry form', code. If so, it may be thaat there is a required field, and simply closing the form without data in them (the required fields) is the source of the err.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thanks for the responses :

James.

I've changed the

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
to
docmd.runcommand accmdundo
me.refresh

as you suggested but this has not made any difference.

I've also set the focus to a control on the mainmenu form, after setting the focus to the form.(just setting the focus to the control didn't work at all)

Michael.
To cover your points/questions - The form MainMenu is, as the name implies, the opening form for the database.
RequestsForm is opened from a control button on MainMenu, MainMenu remains loaded.

AS MainMenu remains loaded I think declaring the variable in the form is OK.

The OnLoad event for MainMenu sets the variable to False as this is the default value I want for it. Granted this bit of code may well be redundant as I set it to True or False in the OnCurrent event of RequestsForm.

The DoCmd.Close you refer to is from my first attempt at this - when I was trying to close RequestsFrom from itself. It also didn't work when I was more explicit and specified the form name though.

There is indeed a required field on the form, [ReqDate], it is this control I check to see whether or not to delete (undo) the record.

To summarise the current situation.

MainMenu loads.Declares IsFirstNewRec as public variable.

Clicking on the relevant Control Button loads the RequestsForm form.

Hitting Enter on the first field (ReqDate) results in a message saying "no data entered - deleting record"

The 2 setfocus commands then trigger.

The Activate event for MainMenu triggers (I have a message box in here so I can see what is going on).

And then I get the error message!

If I comment out the lines that try to close the form then MainMenu appears over the top of the REquestsForm so I know the SetFocus works.

Heres the relevant Code:

MainMenu
--------
Option Compare Database
Option Explicit
Public IsFirstNewRec As Boolean

Private Sub Form_Activate()
MsgBox "activate"
If IsFirstNewRec Then
DoCmd.Close acForm, "requestsform"
End If
End Sub



Private Sub Form_Load()
IsFirstNewRec = False

End Sub


RequestsForm
------------
Private Sub Form_Current()
Dim FrmFirst As String
Dim FrmLast As String
Dim db As Database
Dim rst As Recordset
Dim resp As Boolean
resp = UpdSubs()
'MsgBox Forms.Item("mainmenu").IsFirstNewRec
Set db = CurrentDb
Set rst = db.OpenRecordset("BaseQuery")

' Check which navigation controls should be active

If rst.EOF Then
Me.GotoNext.Enabled = False
Me.GotoPrev.Enabled = False
Me.GotoLast.Enabled = False
Forms.Item("mainmenu").IsFirstNewRec = True
AddNew_Click

Else
Forms.Item("mainmenu").IsFirstNewRec = False

rst.MoveFirst
FrmFirst = rst!ReqID
Do Until rst.EOF
FrmLast = rst!ReqID
rst.MoveNext
Loop
rst.Close
Me.GotoNext.Enabled = True
Me.GotoPrev.Enabled = True

If Me.ReqID = FrmFirst Then
Me!GotoPrev.Enabled = False
End If

If Me.ReqID = FrmLast Then
Me!GotoNext.Enabled = False
End If
End If


Private Sub ReqDate_LostFocus()
If IsNull(Me.ReqDate) Then
MsgBox ("no data entered - deleting record")
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("nextreq")
rst.MoveFirst
rst.Edit
rst!nextrequest = rst!nextrequest - 1
rst.Update
rst.Close
Set db = Nothing

'DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
DoCmd.RunCommand acCmdUndo
Me.Refresh

If Forms.Item("mainmenu").IsFirstNewRec Then
MsgBox "setting focus back to mainmenu"
Forms![MainMenu].SetFocus
Forms![MainMenu]!OpenRequestForm.SetFocus


Exit Sub

End If

Else
Me.TabCtl0.Enabled = True
Me.PAtName.Enabled = True
Me.Request_Details.SetFocus
Me.PAtName.SetFocus


End If

End Sub


Pheww!! Hope you haven't lost interest because this is so long!

Any other ideas ???

Thanks very much,

JaneC

 
I think I now understand your problem.

I believe the problem is that the lostfocus event is not actually running to completion. As a result it will not let you close the form. If you move the lostfocus code onto a command button it works fine. I just tried it, & it will successfully undo the record, switch the focus & close the form.

Thinking about this though, would it not make more sense to set the required property of the ReqDate field to true?? That way the user would not be allowed to leave it blank. They would also be told that the record would not be saved rather than simply undoing the action & closing itself without warning the user first... James Goodman
j.goodman00@btinternet.com
 
Hi James,

Reqdate is required. I am trying to cater for the user opening the form for the first time and then closing it straightaway. The form (requestsform) loads and then generates a unigue identifier for the new record (which obviously then creates a new record), if they then enter through reqdate which is the next field then I want it to undo the record add, and then close the form.

It seems so simple!

JC
 
Would the forms DIRTY property help in this? Take a look at the example in help... Terry M. Hoey
 
Hi!

I have to agree with James. Run you code through a command button. This will give the users the most flexibility when using your form. Make the caption of the button "Close without saving" and they will know to use the button if they don't want to save their input. Having set the field to required in the table will keep any record from being saved without any data in that field.

Jeff Bridgham
bridgham@purdue.edu
 
Hello everyone and thanks for your input.

I'm not sure that the DIRTY property covers what I want.

I'm only trying to cater for the very first time the user opens the form (ie no records at all), if there are already records and this control is entered through then the form diplays the previous record (which is OK).

Reqdate has the focus and all other fields are disabled. So if they enter through I want to close the form. If I can close it manually using the 'x' then I don't really understand why I can't do it in code.


Yours in Deep Despair !

JaneC
 
The problem is an issue with access itself. Basically time can be used to achieve the effect you are looking for.
Follow this Psudeo code.

Form - onload
'assuming you arent using a timer interval now for anything.
me.timerinterval =0

on whatever field.lostfocus
if whatever conditions warrant it then
me.timerinterval = 1000
ReqDate.tag ="close"
do your menu command
end sub

on timer
if Reqdate.tag="close" then docmd.close acform,me.name

 
why whould they even open it if they are not planning on entering a record for it?
why would it automatically generate an id on form open. that should only be generated when they actually try to enter a record... James Goodman
j.goodman00@btinternet.com
 
James,

Why would a user do anything ? I'm working on the principle that if they can then they will.

It generates the ID number as soon as they go into the data entry form so that this can be given to the caller as a reference, although this could be done later.

Anyway, thanks everyone for the help. I've adopted Tim's strategy as it covers exactly what I need in these circumstances.

Thanks Tim!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top