473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 29886
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, 155 views)
File Type: zip IURSTATUS_BE.zip (90.2 KB, 152 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

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

Similar topics

2
by: Richard Hollenbeck | last post by:
I have buttons to open forms all over the place and they all work. Now I'm trying to open the form at the end of a subroutine in another form but it just stops there. Here's the code: ...
5
by: Viper | last post by:
.... say, like MS Word opens another instance of himself when you open another document. If you close the first 'Word', the second document remains alive. I can't figure out how to do the same...
2
by: Paul | last post by:
Hi this is related to a previous post, hopefully just a bit clearer description o the problem. I have a parent form that opens a new form (child form) while still leaving the parent form open....
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
1
by: Vai2000 | last post by:
Hi All! We all know how to close a form on a client side (self.close) etc... but does someone know how to close a form on server side (C#) I am opening a pop up window (loading an aspx form)...
3
by: kev | last post by:
Hi folks, I have a form for registration (frmRegistration) whereby i have two buttons. One is Save which saves record using the OnClick property. I used wizard to create the save button. The...
2
by: Del | last post by:
I have a popup form that consist of a single field called EnteredBy and a Subform that has three fields. The popup form also has a button in the Form Footer called close. In the On Click event I...
6
by: Chandrajit | last post by:
Hi, I am new to vb.net. Please help me in .. I have two form in my application. when i put some data from second from to first form i want to close second form. I am using me.close, but it's not...
8
by: Mai Le | last post by:
Hello, I used Microsoft Access to create a login form with Name Password Access Level Admin and User I would like to let Admin login and open MRB form and User login then open other form like...
5
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am showing a form by ShowModal(). I put a button on that form. When a user clicks the button a MsgBox will show with question "do you want to close?" yes/no. How to handle the situation: - user...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.