Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 11th, 2009, 01:27 AM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating new record from subform

Hi guys,

I've got a problem with programmatically creating a new record in a subform from a different subform. Currently, the main form (frmMain) alternatively displays either subform 1 (sfrmSubform1) or subform 2 (sfrmSubform2), depending on what the user has selected. This switching is done via:
Code:
Forms!frmMain!container_form.SourceObject = "sfrmSubform1"

Where container_form is the name of the container holding either subform 1 or 2.

From subform 1 (i.e.: the VBA code-behind for subform 1), I'm trying to add a new record to subform 2. I've tried:
Code:
Forms!frmMain!container_form.SourceObject = "sfrmSubform2"
Forms!frmMain!container_form.Form.SetFocus
DoCmd.GoToRecord acActiveDataObject, , acNewRec
But this does not work. The runtime error I get is that sfrmSubform2 is not open.

Does anyone have any ideas how I can solve this?

Thanks,

Simon
 
Old February 12th, 2009, 08:24 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Where are you getting the error?

I think the problem is that the form is NOT open by the time your last line runs.

You could troubleshoot this by creating a 3rd variation of your form based on sfrmSubForm2 (call it sfrmSubForm3) and then do this:

Code:
Forms!frmMain!container_form.SourceObject = "sfrmSubform2"
Forms!frmMain!container_form.Form.SetFocus
And then on the on open event of sfrmSubForm3, put this line:

Code:
DoCmd.GoToRecord acActiveDataObject, , acNewRec
Or something similar.

I think the setfocus will also give you problems

Did that help?
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old February 12th, 2009, 06:09 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Solved

Hi mmcdonal, thanks for your help but I found a better way to work around the problem.

By first manually inserting a new record into the table (via a SQL INSERT INTO statement), I then set subform 2's RecordSource to point to the new record (via a SQL SELECT statement). So the final code is:
Code:
CurrentDb.Execute ("INSERT INTO tblTask (storycard_id, task_id, description, " & _
            "estimate) VALUES ('" & storycard_id & "', '" & task_id & "', " & Chr(34) & _
            " " & Chr(34) & ", '0')")
      Forms!frmMain!tree_form.SourceObject = "sfrmSubform2"
      Forms!frmMain!tree_form.Form.RecordSource = "SELECT * FROM tblTask WHERE " & _
            "storycard_id = " & storycard_id & " AND task_id = " & task_id
Thanks for your help anyway!

Simon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a record from a subform captainlove Access VBA 3 May 1st, 2008 11:38 AM
How do I delete a record from a subform captainlove SQL Server ASP 1 April 29th, 2008 12:01 AM
setting subform to new record skinny Access 4 January 3rd, 2006 02:57 PM
Creating a new record in a subform dlamarche Access 3 February 18th, 2005 12:33 AM
Subform only may contains 1 record Stanny Access 5 December 21st, 2004 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.