We help IT Professionals succeed at work.

Simple Code to Save All Objects

cssc1
cssc1 used Ask the Experts™
on
I am looking for simple vba code to:
1.  save all data on all objects on frmOne to Table1
2. Close frmOne
3. Open frmTwo
4. Refresh data on formTwo

This to be put on the click event of Command1 and Command2.
Code that I can easily modify and use on other forms/Cmmand buttons.

Sample db is attached to work with.

No macros
Test-OpenClose.accdb
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant
Commented:
Am I missing something here?

Can't you just close frmOne and the data will be saved?

For frmTwo, if you open it the data will be refreshed automatically.

Here's the code for closing:

Private Sub Command9_Click()
    DoCmd.Close acForm, Me.Name
End Sub

Here's the code for refreshing but I'm not sure you need it.

Private Sub Command5_Click()
    Me.Refresh
End Sub

In both Me refers to the form the code is in.

Author

Commented:
imnorie:
  I can't get code to work?
Please see attached db
Test-OpenClose-VER2.accdb
NorieAnalyst Assistant

Commented:
It should be Me.Name not Me.<form name>.

Me refers to the form but to close it using this method we need it's name.

Author

Commented:
What did I do wrong now?

Private Sub Command9_Click()
    DoCmd.Close acForm, frmOne.Name
End Sub
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
<What did I do wrong now?>

We can't tell, because you didn't tell us what happened. We're not sitting in front of your machine, so we can't see what's going on - you have to tell use what happened (and perhaps show us a screenshot), and then we can help you to fix it.

I'd also avoid wording your questions as if they were project requirements and we were employees assigned to complete this task (for free, since we're not paid for any of this). This rubs many Experts the wrong way (me included) and can get your questions ignored by many of the top level Experts here.

Author

Commented:
LSMConsulting:
  Sorry for my very poor wording of my questions. I did not mean to create and problems by my poor wording.

The problem I am having with the code:

Private Sub Command9_Click()
    DoCmd.Close acForm, frmOne.Name
End Sub


is this code should close frmOne and it is not doing that.
Please advise.

Thanks
NorieAnalyst Assistant

Commented:
Where did you put that code?

It should be in the module for the form.

Try this:

1 Open frmOne in design view.

2 Select the command button.

3 In the properties sheet under the Event tab you'll find OnClick (or Click), select it.

4 Click the dropdown on the right, choose [Event Procedure].

5 Then click the ellipsis ... and you should now see this:

Private Sub Command9_Click()

End Sub

6 This is where the code to close the form should go.
Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
Commented:
The more standard version of that code would be:

DoCmd.Close acForm, "frmName"

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sorry:

DoCmd.Close acForm, "frmOne"

Author

Commented:
LSMConsulting:
  Thanks, this code closes frmOne GREAT.

Woulkd this be the  code to open frmTwo?


Private Sub Command41_Click()
DoCmd.Close acForm, "frmOne"
DoCmd.Open acForm, "frmTwo"
End Sub

????
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Docmd.OpenForm "frmTwo"
NorieAnalyst Assistant

Commented:
What happened to using Me.Name?

If the code was in  frmOne then Me.Name would equal 'frmOne'.