469,366 Members | 2,198 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How do I close an open form?

sueb
379 256MB
I have a little form that I want to:

1. accept a new primary key (chart number) (this is the only field on the form),
2. add a new record with this primary key,
3. open the main form, and
4. close itself

So far, it does 1 through 3, but doesn't do 4, and also gives me an error, saying that there's an operator missing on line 12 of the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ChartNum_Exit(Cancel As Integer)
  2.     Dim stDocName As String
  3.     Dim stMainForm As String
  4.     Dim stLinkCriteria As String
  5.  
  6.     stMainForm = "Patient_IUR_Overview"
  7.     stDocName = "Patients: Add"
  8.  
  9.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  10.     'DoCmd.Close stDocName
  11.  
  12.     stLinkCriteria = "[Patients.Patient Index]=" & Me![Patient Index]
  13.     DoCmd.OpenForm stMainForm, , , stLinkCriteria
  14.  
  15. Exit_ChartNum_Exit:
  16.     Exit Sub
  17.  
  18. Err_ChartNum_Exit:
  19.     MsgBox Err.Description
  20.     Resume Exit_ChartNum_Exit
  21.  
  22. End Sub
  23.  
I'm pretty sure that it has something to do with the index, but I don't know how to fix that line (I cribbed it from another module that behaves similarly).

Thanks in advance, because I know someone will have posted the answer almost before I can come back to check for it!
Jan 13 '11 #1

✓ answered by Rabbit

The reason the form doesn't close is because you didn't specify what type of object you're closing.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.Close acForm, "FormName"
However, you can not close the form while it is processing a form event. You will need to move the code out of that event into a different one.

I suggest that you move the code to the after insert event of the form because that will also solve your other problem. That problem being that you only want the code to trigger when a Patient ID is available and it will only be available after the record is inserted into the table.

29 28528
hype261
207 100+
It looks like you commented out closing the form on line 10. Was this on purpose?
Jan 13 '11 #2
sueb
379 256MB
I got a "type mismatch" on that line, and I wasn't even sure what this "Close" command was going to attempt to close.
Jan 13 '11 #3
hype261
207 100+
Here is the MSDN link on the DoCmd.Close method. You got the type mismatch because you are sending a string where it expects an AcObjectType.

http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
Jan 13 '11 #4
sueb
379 256MB
Thanks for that link, but here is the line I'm using, and it gives me a run-time error of 2585, saying: "This action can't be carried out while processing a form or report event."

Expand|Select|Wrap|Line Numbers
  1. DoCmd.Close acForm, stDocName, acSaveYes
What's wrong with this line?
Jan 13 '11 #5
Rabbit
12,516 Expert Mod 8TB
Your form is named "Patients: Add"? Bad choice of name. Does "[Patients: Add]" work?
Jan 13 '11 #6
sueb
379 256MB
So I changed the two lines of code to read:

Expand|Select|Wrap|Line Numbers
  1.     DoCmd.RunCommand acCmdSaveRecord
  2.     DoCmd.Close , stDocName
  3.  
and here's what happens:

1. I add the new chart number, and the correct window opens.
2. I close that window manually, and find that the original "add" window is still there.
3. When I click the window's "X" button, I get: "Syntax error (missing operator) in query expression '[Patients.Patient Index]='."

How can it be opening the right window if it's having a problem with that particular line? It's actually adding a record for the chart number I entered, and it opens the correct window to look at all the fields for that record, so what's going on? And why does this error only show up when I manually try to close the "add" window?
Jan 14 '11 #7
eneyardi
180 100+
on deactivate event macro,put type close on action
Jan 14 '11 #8
sueb
379 256MB
Do what, where, now?
Jan 14 '11 #9
Rabbit
12,516 Expert Mod 8TB
[Patients.Patient Index] is the name of the field? If it's not, then that syntax is wrong. Just use [Patient Index] or [Patients].[Patient Index].
Jan 14 '11 #10
sueb
379 256MB
Neither of those syntaxes work. Just to refresh things, what happens is:

- the add window pops up
- I enter a new number (The only action that causes the patient window to come up is TAB; RETURN just clears the field. However, if I manually open the patient form, and search for the new number, it's been added.
- the patient window opens
- the add window remains open (behind the patient window), and if I close it using the "X" button in the upper-right-hand corner, I get the error message "Run-time error 3075: Syntax error (missing operator) in query expression '[Patient Index]]='."

Here's the module as it stands:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ChartNum_Exit(Cancel As Integer)
  2.     Dim stDocName As String
  3.     Dim stMainForm As String
  4.     Dim stLinkCriteria As String
  5.  
  6.     stMainForm = "Patient_IUR_Overview"
  7.     stDocName = "Patients: Add"
  8.  
  9.     DoCmd.RunCommand acCmdSaveRecord
  10.  
  11.     stLinkCriteria = "[Patient Index]=" & Me![Patient Index]
  12.     'stLinkCriteria = "[Patients].[Patient Index]=" & Me![Patient Index]
  13.     DoCmd.OpenForm stMainForm, , , stLinkCriteria
  14.  
  15.     DoCmd.Close , stDocName
  16.  
  17. Exit_ChartNum_Exit:
  18.     Exit Sub
  19.  
  20. Err_ChartNum_Exit:
  21.     MsgBox Err.Description
  22.     Resume Exit_ChartNum_Exit
  23.  
  24. End Sub
  25.  
Jan 15 '11 #11
Rabbit
12,516 Expert Mod 8TB
Can you upload your database?
Jan 15 '11 #12
From your code the docmd needs an oject to work with, if your code is attached to the form then try DoCmd.Close acForm, Me.Name
Jan 16 '11 #13
sueb
379 256MB
Rabbit, here's my database.

And, john garvey, I think I'm already trying that on line 15. Or is that line wrong? or something?
Attached Files
File Type: zip IURs.zip (476.4 KB, 147 views)
File Type: zip IURSTATUS_BE.zip (90.2 KB, 145 views)
Jan 17 '11 #14
You need to tell Access what to close and what its name is
Jan 18 '11 #15
Rabbit
12,516 Expert Mod 8TB
Turns out you can't save or close a form while the form is processing a form event. You can do it on a button click, but not during a form event. The reason you're getting the patient id error is because there is no patient id on a new record.
Jan 18 '11 #16
sueb
379 256MB
Rabbit, so line 9 (in post # 11) doesn't write the record?

Edit: But wait, it does write the record, because the supplemental form does come up appropriately (with the just-added medical record number in it). Only the closing of the "add" window gets an error.
Jan 18 '11 #17
Rabbit
12,516 Expert Mod 8TB
I get an error on the save when I run it from my computer.

But you can't close the form during the processing of a form event. That's what the error message says.
Jan 18 '11 #18
sueb
379 256MB
Hmmm... You must be getting a different error message from me. Mine says:

"Run-time error 3075: Syntax error (missing operator) in query expression '[Patient Index]]='."

And I get this even when I comment out the line that would try to close the "add" form.
Jan 18 '11 #19
Rabbit
12,516 Expert Mod 8TB
That's not because of the close form line. It's because of the open form line. When you trigger that function while it's in a new record, Patient Index has yet to be filled in, therefore it is either null or blank and the Form is unable to use that where statement.
Jan 18 '11 #20
sueb
379 256MB
Yes, that makes sense, but it leaves me wondering how this function is managing to work correctly. As you'll notice in both my original question and in post # 11, after I enter a new ID in the "add" window, the correct patient record window opens (displaying the new ID), and the record has already been entered into the database (I can access that new record, containing only the new ID, from other forms, users, etc.).

It's only when I try to close the "add" window that I get the error, and I get the same error whether or not the close command is commented out.

It's crazy--I'm tempted to just leave it alone since, if you don't try to close the "add" window, but allow it to be re-used, you only get the error when you finally shut down the database at the end of the day.
Jan 19 '11 #21
Rabbit
12,516 Expert Mod 8TB
You only get the error when you try to close the window (I assume by clicking on the x in the upper right of the form) because it then triggers the function and attempts to open the form using the null Patient ID. If you comment out the open form line, you will not get the error. But when you comment out the open form line, you will get the other error I was talking about.
Jan 19 '11 #22
sueb
379 256MB
But then how do I get the new record window to open? (Which it does, by the way, and is exactly what I want to have happen.)
Jan 19 '11 #23
Rabbit
12,516 Expert Mod 8TB
Wait.. are you trying to exit the form without putting in a new chart number? That's when you get the error right? Only if you don't enter in a new chart number.

Try moving the code to the AfterInsert event of the form instead. That way, it should only trigger when a new record is added.
Jan 19 '11 #24
sueb
379 256MB
No. As I specified in my descriptions, I enter the new number, press <TAB> to exit the field, and the new window opens, in which I can finish entering the rest of the data. I only get the error when I try to close the "add" window AFTER successfully inserting my new record.

Edit: Well, actually, I ALWAYS get the error when I close the "add" window, regardless of what's gone before.
Jan 19 '11 #25
Rabbit
12,516 Expert Mod 8TB
Oh.. you're tabbing? Since the chart number is the only control on there, when you tab, it will trigger the event. Which will open the form for the one you just added. Then it will move on to a new record. When you try to close that form, the event is triggered again and it fails because you are now on a new record that has no patient ID.

Also, your form isn't closing because you didn't specify what type of object you're trying to close. You need to specify that it's an acForm object you're closing. But when you do though, you will get the error that says you can't do that while processing a form event. Therefore, you should move the code into the AfterInsert event.
Jan 19 '11 #26
sueb
379 256MB
Okay, that worked to eliminate the error. Do you think there's any way that I can make the "add" window close automatically?
Jan 19 '11 #27
Rabbit
12,516 Expert Mod 8TB
Yeah, you just need to specify the type of object you're closing.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.Close acForm, "frmName"
Jan 19 '11 #28
sueb
379 256MB
Ah, that's it now! So the whole problem was that I had my code in the wrong event.

Thanks, Rabbit!

Would you consider summing up the advice you've given me here in a single post so that I can put the "best answer" on that one, making it easier for those who come to this thread later?
Jan 19 '11 #29
Rabbit
12,516 Expert Mod 8TB
The reason the form doesn't close is because you didn't specify what type of object you're closing.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.Close acForm, "FormName"
However, you can not close the form while it is processing a form event. You will need to move the code out of that event into a different one.

I suggest that you move the code to the after insert event of the form because that will also solve your other problem. That problem being that you only want the code to trigger when a Patient ID is available and it will only be available after the record is inserted into the table.
Jan 19 '11 #30

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Richard Hollenbeck | last post: by
1 post views Thread by Vai2000 | last post: by
5 posts views Thread by =?Utf-8?B?Q2hyaXM=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.