Solved

Button to Close form

Posted on 2014-04-28
8
446 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

778 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