473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Main form ID to subform using Access 2010 vba code

I have a form with a sub form that is loaded by code. Everything works except the main form ID does not appear on the subform. When new records are added manually everything works fine. All realtionships and links are in place. This is all done in Access 2010 vba code. Any and all help would be greatly appreciated. I know it has to be something simple I am missing.

Here is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command35_Click ()
  2. Dim dbs As DAO.Database
  3. Dim rst1 As DAO.Recordset
  4. Dim rst2 As DAO.Recordset
  5. Set dbs = CurrentDb()
  6. Set rst1 = dbs.OpenRecordset("InventoryRequestQuery", dbOpenDynaset)
  7. Set rst2 = dbs.OpenRecordset("Inventory Request Details", dbOpenDynaset)
  8.  
  9. rst1.MoveFirst
  10.  
  11. Do Until rst1.EOF
  12.  
  13. rst2.AddNew
  14.  
  15. rst2!Nomenclature.Value = rst1!Nomenclature
  16. rst2!ProductCode.Value = rst1!ProductCode
  17. rst2!NSN.Value = rst1!NSN
  18. rst2!UnitPrice.Value = rst1!UnitPrice
  19. rst2!txtDate.Value = rst1!txtDate
  20. rst2!Quantity.Value = rst1!Quantity
  21. rst2.Update
  22. rst1.MoveNext
  23.  
  24. Loop
  25.  
  26. Me.Form.Refresh
  27.  
  28. rst1.Close
  29. rst2.Close
  30.  
  31.  DoCmd.GoToControl "SiteID"
  32.  DoCmd.RunCommand acCmdCopy
  33. DoCmd.OpenForm "InventoryRequest5A", acNormal
  34. DoCmd.GoToRecord , , acNewRec
  35. DoCmd.GoToControl "SiteID"
  36. DoCmd.RunCommand acCmdPaste
  37.  
  38. End Sub
This code works perfect except for not placing the mainform ID on the subform. I beleive it has to be ssomething simple I am missing. This is my first question
submitted so ask questions if I didn't make my issue clear.
I want say thank you for all the help.
I like to think positive.
Mar 25 '16 #1
3 3028
NeoPa
32,556 Expert Mod 16PB
I'm not sure what the code tells us, or why it's included, but you say the sub-form's ID control isn't populated automatically when the Master/Child links are not defined, yet they are when the links are defined.

That describes how things work and are expected to work. Are you saying you want a sub-form but don't want to define the Master/Child links?
Mar 31 '16 #2
zmbd
5,501 Expert Mod 4TB
(UPDATE 2016-05-03: See my next post... learned something new; however, using the direct methods as originally mentioned the following may still have some merit.)

AFAIK: This is because, behind the scenes, MS adds the related field entries on a bound form; however, VBA doesn't have the same scope (i.e. the table relationships) - therefore, you have to provide the related value information yourself when working directly with the underlying record source(s).

Doesn't matter if you are accessing the underlying table/query or the form's recordset under VBA (I've tried both ways in the past), in either case, you have to pull the related information and supply it to the new record because VBA has no knowledge of how the tables are related.

You'll find this true about a lot of things in databases, where the SQL engine will have scope about one thing yet nothing about the form's information (or the reverse) amongst other situations.

-Z
May 2 '16 #3
zmbd
5,501 Expert Mod 4TB
I have been playing with this in one of my subform databases and discovered that there is a way to accomplish what I believe you were trying to do - learned something new!

What I found is that, if you set the focus to the subform container, use the DoCmd.GoToRecord record:=acNewRec to move to a new record in the subform, you can then set the control's value to whatever is valid for the control, finally check the subform to see if it is "dirty" and if so save the newly created record.

Normally I don't just post code; however, this is something I've never tried before and appears to have some value:

In this case, I was simply entering an arbitrary value for a new "child" in the child subform for the current parent using a command button...
Expand|Select|Wrap|Line Numbers
  1. Private Sub zctrl_cmd_addachild_Click()
  2.     Dim zSfrm As Form
  3.     '
  4.     'strictly speaking, one doesn't need to create the form object; however, this way there's less chance for typos
  5.     '
  6.     Set zSfrm = Me!zctrl_sfc_Child.Form
  7.     '
  8.     'Here You have to reference the sub-form-control that contains the actual sub-form... I always rename these controls
  9.     Me!zctrl_sfc_Child.SetFocus
  10.     '
  11.     'now that the sub-form has the focus, move to the new record
  12.     DoCmd.GoToRecord record:=acNewRec
  13.     '
  14.     'enter the value you want for the control (or the field in the recordset of a bound form)
  15.     'I suggest referencing the control - this way we're sure that we're acting on the form!
  16.     'probably should add code to verify that we're actually on a new record in the subform; however, I leave that for the developer.
  17.     zSfrm.zctrl_txt_child_name = "cmd_" & Format(Now(), "yymmddhhnnss")
  18.     '
  19.     'check to see if the subform has an edit and save if so
  20.     If zSfrm.Dirty Then zSfrm.Dirty = False
  21.     '
  22.     'NORMALLY, you'd want error trapping in this code
  23.     'however, just for the demo, I'll simply clear the object
  24.     Set zSfrm = Nothing
  25. End Sub
I've tried the above code and it works in my database (Access2013 both in Win7 and Win8.1) I believe that the reason this works is because we're acting directly upon the form which still has scope for the Master/Child field relationships established at the form level; thus, Access knows to create the related field entries for the new record.
May 3 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
3
by: Bobby | last post by:
Hi Can anybody tell me why the VBA code below works fine in Access 2003 but doesn't work in Access 2007? I do not get any error message, even if I put a deliberate error into the code. Also, if I...
2
by: John Lewis | last post by:
I have created a windows form application using an Access 2003 db. I used the drag and drop methods to place the fields on the forms and the same with the second form using the related table from the...
0
by: farid121 | last post by:
I am trying to update the main form with each record in the subform. I have two tables Invoice and Payment table. The main form has the payment details which I need to update for each invoice...
5
by: Sally Bullard | last post by:
I have a form with a button to filter records and another to show all records. When the records are filtered I want the color of the button to change so that the user can immediately see that the...
2
by: Corwin Moyne | last post by:
Hello. I am currently creating an Access database. I am a second year programming student where I am learning java and C#. Can I use C# instead of VBA when it comes to Access? I am pretty good with...
1
by: olugard | last post by:
Hello All! I am a relative new comer to VBA/Access/SQL Queries, I have been working for several weeks on a database and I am looking to determine the time period between 2 days are denoted in...
1
by: jdks2006 | last post by:
I work with a company who is needing to use Access 2010 to receipt in checks for paid invoices. Our issue came when our auditor found we had duplicate receipt numbers using serial numbered receipt...
14
by: rd3Po | last post by:
I have an Access DB that I'm attempting, for display purposes, to add an unbound text field to a continuous subform that contains a row number for each record. From a really old previous post I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.