Solved

Button to Close form

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

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! Worked great.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
glad I could help
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

15 Experts available now in Live!

Get 1:1 Help Now