Solved

Button to Close form

Posted on 2014-04-28
8
448 Views
Last Modified: 2014-04-29
I have a button on a form that opens another form. Once the new form is open, I would like the button to then close the current form. Is there code for this? Thanks!
0
Comment
Question by:cansevin
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40028420
Not sure what you want, but this will close the current form
docmd.close acForm, me.name
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 40028423
Add another line to the code at the very end (after opening the form (and setting the focus to it) to
DoCmd.Close acForm, "forname", acSaveNo

Kelvin
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40028555
However, if you use the acDialog window mode for the 2nd form, the code that opens it will pause until that form is closed or hidden.

Generally, if I open a second form in Dialog mode, I want to leave the original form on screen, but just wanted to make sure you were aware of that "feature" of the acDialog value of the WindowMode argument of the docmd.OpenForm method.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cansevin
ID: 40029458
Hey guys... sorry for being vague. I still can't figure it out.

I have an open form titled frmDailyReport which has a button that opens another form titled: MainForm.

When I hit the button that opens MainForm, I also want it to close the form it is on which is frmDailyReport. The "On Click" event is a Macro... which I don't know how to use. I am hoping to write a code in the "On Focus" or another event area.

I tried the the code:

Private Sub Command1_GotFocus()
 DoCmd.Close acForm, Me.frmDailyReport
End Sub

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40029472
The "On Click" event is a Macro...
I'm not sure what you mean, all events including the "On Focus" event are events.

This should do what you want, it will close the calling form:
Private Sub Command1_Click()
 DoCmd.Close acForm, Me.Name
End Sub

Open in new window

0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40029494
Ok, delete the macro from the On Click event of the command button

If the button is simply opening the form, MainForm, but not to a particular record, you should be able to use:
Private Sub cmd_OpenMain_Click

    docmd.openform "MainForm"
    docmd.close acform, me.name

End Sub

Open in new window

Note that I renamed Command1 to cmd_OpenMain.  One of the first things you need to learn, besides how to normalize your data, is that you need to use a naming convention for all of your controls.  There are several Leszynski and Reddick come to mind, but most programmers develop their own over time.  This will help you or anyone else that is trying to understand your code know without having to look at the form, what type of control it is and potentially what data it is bound to.

If you are attempting to open the main form to a specific record, then the OpenForm method takes just a little more effort.
Private Sub cmd_OpenMain_Click

    Dim strCriteria as String

    'Assuming that the record you are going to on the main form is identified by a 
    'numeric value on the current form
    strCriteria = "[NumericPKField] = " & me.txtPK
  
    'If the record on the main form is identified by a string, then you need to wrap the
    'value of that string in quotes, something like:
    strCriteria = "[TextPKField] = " & chr$(34) & me.txtPK & chr$(34)

    docmd.openform "MainForm",,,strcriteria
    docmd.close acform, me.name

End Sub

Open in new window

0
 

Author Closing Comment

by:cansevin
ID: 40029546
Thanks! Worked great.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40029619
glad I could help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question