Solved

Button to Close form

Posted on 2014-04-28
8
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 48

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 48

Expert Comment

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Outlook VBA question - How do I mark a message as read 2 60
CISCO WIFI 6 73
question about a text field with default value 5 31
Ms Access 2010 Setup (Executable file) 4 68
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

752 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