Solved

Button to Close form

Posted on 2014-04-28
8
445 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
 

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
Backup Your Microsoft Windows Server®

Backup 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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now