473,785 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicating records in subform

5 New Member
Hello.

I’m new to writing VBA code but getting there thanks, for the most part, to the excellent advice on this site.

I trying to create the code which will allow all records from a subform to be copied and duplicated back in the original underlying table for that subform.

To give a bit more background, the main form contains records of individual ‘reports’ (held in table called CPG1) and the subform (CPG3_Subform) identifies the ‘milestones’ for each report (the data for which is held in a table called (CPG3). Each milestone (there could be several for each report) is referenced back to corresponding report via its unique ID .The datadase has a command button allowing users to copy a report, in order to an updated report based on it. And I would like this copy process to include the ‘milestones’ held against that ‘report’.

The code I have patched together so far is as follows (line 6-17 are simply included for reference purposes):

1. Private Sub Command28_Click ()
2. On Error GoTo Err_Command28_C lick

3. Dim var As Variant
4. Dim rst As Recordset
5. Dim PreRPT As Integer

6. 'This section duplicates the latest report held, setting rpt ref and current date.
7. Me.MaxCPG1_subf orm.Requery
8. PreRPT = [MaxCPG1_subform]![MaxOfRPTRef]
9. DoCmd.RunComman d acCmdSelectReco rd
10. DoCmd.RunComman d acCmdCopy
11. DoCmd.RunComman d acCmdRecordsGoT oNew
12. DoCmd.RunComman d acCmdSelectReco rd
13. DoCmd.RunComman d acCmdPaste
14. ReportDate.Valu e = Now()
15. RPTRef.Value = PreRPT + 1
16. Me!Combo8.Reque ry
17. Me.Combo8 = Me.CPG_NoteID

18. 'This section duplicates the milestones



19. Set rst = CurrentDb.OpenR ecordset("CPG3" , dbOpenDynaset)
20. For Each var In Me.CPG3_subform
21. rst.AddNew
22. rst!CPGID = CPG_NoteID.Valu e
23. rst!Scheme = CPG3_subform.Co lumn(2, var)
24. rst!Description = CPG3_subform.Co lumn(3, var)
25. rst.Update
26. Next
27. rst.Close
28. Set rst = Nothing


29. Exit_Command28_ Click:
30. Exit Sub

31. Err_Command28_C lick:
32. MsgBox Err.Description
33. Resume Exit_Command28_ Click

34. End Sub

Lines 19-28 are the bits causing the problem (the previous lines work fine to simply copy the report). I’m getting an error message "Method or data member not found".

If this has already been type of query has already been addressed, would mind please pointing me in the right direction. I appreciate I may be going off entirely down the wrong track. I would be most grateful for any suggestions
Mar 12 '08 #1
7 2051
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. The error message about the method or member not found is because there is more than one type of recordset, and you need to use the DAO object library version. Change your Dim rst to
Expand|Select|Wrap|Line Numbers
  1. Dim rst As DAO.Recordset
If you now get a compilation error you will need to add the DAO object library in your project references - from the VB editor select Tools, References, then tick the entry for the DAO x.x object library, where x.x is the highest one of the choices that may be available to you in the list.

I hope you find that your code works as expected after you select the correct recordset type.

-Stewart
Mar 12 '08 #2
Albennett
5 New Member
Hi. The error message about the method or member not found is because there is more than one type of recordset, and you need to use the DAO object library version. Change your Dim rst to
Expand|Select|Wrap|Line Numbers
  1. Dim rst As DAO.Recordset
If you now get a compilation error you will need to add the DAO object library in your project references - from the VB editor select Tools, References, then tick the entry for the DAO x.x object library, where x.x is the highest one of the choices that may be available to you in the list.

I hope you find that your code works as expected after you select the correct recordset type.

-Stewart
I've added the DAO 3.6 Object Library, but unfortuately I'm still getting the same compliation error: 'method or data member not found'. Error appears to be occuring at line 23.

Regards

Alistair

Any further suggestions please ?
Mar 12 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. In my last post I should also have spotted that your combo column references were invalid (column(n, var)). Replace these with
Expand|Select|Wrap|Line Numbers
  1. rst!Scheme = CPG3_subform.Column(2)
  2. rst!Description = CPG3_subform.Column(3)
-Stewart
ps I'm assuming thet these two items are combo columns and not references to controls on a subform. If it is the latter you would have to refer to them like this:
Expand|Select|Wrap|Line Numbers
  1. Me![subformname].form![control name].Column(2)
Mar 12 '08 #4
Albennett
5 New Member
Hi. In my last post I should also have spotted that your combo column references were invalid (column(n, var)). Replace these with
Expand|Select|Wrap|Line Numbers
  1. rst!Scheme = CPG3_subform.Column(2)
  2. rst!Description = CPG3_subform.Column(3)
-Stewart
ps I'm assuming thet these two items are combo columns and not references to controls on a subform. If it is the latter you would have to refer to them like this:
Expand|Select|Wrap|Line Numbers
  1. Me![subformname].form![control name].Column(2)
Stewart

Sorry about this.

The latter two are fields within the subform (please excuse my ignorance about the coding). I've made the amendments you outlined above; changing the references for these fields and the column references.

But I'm now getting 'Object doesn't support this property or method' error, and no new records have been added. I tried removing the ".Column(2) "; thinking that I may not need this since I was naming the fields specifically... but that did work either.

Alistair
Mar 12 '08 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
...The latter two are fields within the subform (please excuse my ignorance about the coding). I've made the amendments you outlined above; changing the references for these fields and the column references. But I'm now getting 'Object doesn't support this property or method' error, and no new records have been added. I tried removing the ".Column(2) "; thinking that I may not need this since I was naming the fields specifically... but that did work either.
Hi Alistair. Not sure why you need to refer to items on your subform at the same time as items on your main form; this is not normal practice. It begs the question of what you are really trying to do, and I'm somewhat puzzled by it.

Subforms generally reflect one-to-many relationships between the mainform table and the subform table - in referring to the subform which many-side record would you be referring to? Also, the mainform is linked to the subform by common parent/child field links - why is it necessary to refer to the subform at all?

Having said all that, there must be some kind of error in your reference to the subform controls if the 'method...' error arises on those lines. For text boxes and the like you do not need the Column property, as you suspected, and the reference just becomes
Expand|Select|Wrap|Line Numbers
  1. me![subformname].form![control name]
As to how to resolve all this, firstly, and most importantly, could consider why you need to refer to a subform value whilst referring to the mainform elswhere in your processing, and, secondly, if you continue to experience errors after you alter names of controls etc could you post the revised code back on your next reply so I can check it?

Thanks

-Stewart
Mar 12 '08 #6
Albennett
5 New Member
Hi Alistair. Not sure why you need to refer to items on your subform at the same time as items on your main form; this is not normal practice. It begs the question of what you are really trying to do, and I'm somewhat puzzled by it.

Subforms generally reflect one-to-many relationships between the mainform table and the subform table - in referring to the subform which many-side record would you be referring to? Also, the mainform is linked to the subform by common parent/child field links - why is it necessary to refer to the subform at all?

Having said all that, there must be some kind of error in your reference to the subform controls if the 'method...' error arises on those lines. For text boxes and the like you do not need the Column property, as you suspected, and the reference just becomes
Expand|Select|Wrap|Line Numbers
  1. me![subformname].form![control name]
As to how to resolve all this, firstly, and most importantly, could consider why you need to refer to a subform value whilst referring to the mainform elswhere in your processing, and, secondly, if you continue to experience errors after you alter names of controls etc could you post the revised code back on your next reply so I can check it?

Thanks

-Stewart
Hi Stewart

I’ll try to explain what it is I’m trying to achieve. I’ve probably missed something blatantly obvious here and tried to over complicate things!

The part of the database I’m working on captures monitoring information (in the form of a series of ‘reports’) for a programme of projects. Several ‘reports’ will be produced for each project. So, as well as obviously at table of projects, I have a table of ‘reports’ (called CPG1). I have designed a form, based on the CPG1 table to facilitate the compilation of these reports.

A large proportion of the information required for each report will have already been recorded on an earlier report for that scheme. I have therefore given users the option of using that info as the basis of their new report, by allowing them to create a duplicate of that earlier report.

Each report also includes a series of milestones. So I have a separate table of ‘Milestones’(ca lled CPG3). There may be several milestones per report. Perhaps strangely, even though these milestones may be largely the same from report to report, the users would like the milestones to be specific to each report. So at the same time as the report is duplicated I would also like to duplicate the individual milestones attached to that report. Obviously, each report contains its own uniqueID [CPG_NoteID], so as each milestone is copied I need to update the reference to the new version of the report (hence the need for line 22 in my code).

So in summary, there are a series of reports, each of which will have a series of milestones attached. I am trying to allow users to duplicate a report and it’s associated milestones.

Hope this helps

Many thanks for your time and patience so far.

Alistair
Mar 12 '08 #7
Albennett
5 New Member
Hi Stewart

I’ll try to explain what it is I’m trying to achieve. I’ve probably missed something blatantly obvious here and tried to over complicate things!

The part of the database I’m working on captures monitoring information (in the form of a series of ‘reports’) for a programme of projects. Several ‘reports’ will be produced for each project. So, as well as obviously at table of projects, I have a table of ‘reports’ (called CPG1). I have designed a form, based on the CPG1 table to facilitate the compilation of these reports.

A large proportion of the information required for each report will have already been recorded on an earlier report for that scheme. I have therefore given users the option of using that info as the basis of their new report, by allowing them to create a duplicate of that earlier report.

Each report also includes a series of milestones. So I have a separate table of ‘Milestones’(ca lled CPG3). There may be several milestones per report. Perhaps strangely, even though these milestones may be largely the same from report to report, the users would like the milestones to be specific to each report. So at the same time as the report is duplicated I would also like to duplicate the individual milestones attached to that report. Obviously, each report contains its own uniqueID [CPG_NoteID], so as each milestone is copied I need to update the reference to the new version of the report (hence the need for line 22 in my code).

So in summary, there are a series of reports, each of which will have a series of milestones attached. I am trying to allow users to duplicate a report and it’s associated milestones.

Hope this helps

Many thanks for your time and patience so far.

Alistair
Code currently stands as follows:

Expand|Select|Wrap|Line Numbers
  1. Set rst = CurrentDb.OpenRecordset("CPG3", dbOpenDynaset)
  2. For Each var In Me.CPG3_subform
  3. rst.AddNew
  4. rst!CPGID = Me!CPG_NoteID.Value
  5. rst!Scheme = Me![CPG3_subform].Form![Scheme]
  6. rst!Description = Me![CPG3_subform].Form![Description]
  7. rst.Update
  8. Next
  9. rst.Close
  10. Set rst = Nothing
Mar 13 '08 #8

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

Similar topics

1
367
by: pwys | last post by:
Hello to everyone WOndering if there anyone could help me with this. I have a Primary & a secondary table with the unique key (InvoiceNo) Waht i want is to make duplicate copy of a selected invoice no (both Primary & secondary) into a new invoice no. can some one help me in this?
6
4083
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
6
2506
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new products). Tables: tblCategoryDetails CategoryID SpecID
1
4443
by: phaddock4 | last post by:
Being fairly inexperienced at Access 2000, i've been reading many posts here for the last several days, and testing myself to find the best approach to do the following in A2K: SET UP: I have created a MainTable (and related form), which has an associated SubForm (popup) along with its underlying, separate Table. The tables' relationship is one to many respectively. The primary key
8
5764
by: Josetta | last post by:
I have found a wealth of information here on how to duplicate records in forms with subforms. I have adapted code found here to work with my forms. It works beautifully the first time I hit the "duplicate" button. It copies the main form data, the subform data and moves to the new record. Great! Here's the problem...if I move to another record (or stay on the newly created record, for that matter), and hit the Duplicate button...
2
2436
by: mavmavv | last post by:
I have a Form where I have created a duplicate record button, no problem... The subform is where my problem lies. The subform displays data matching the mainform's ID, these two values are linked. The subform has no primary key, since there are multiple matching entries. Basically the mainform displays Customer info + order totals, and the subform displays the products. When I goto duplicate the record, the ID from the duplicated...
4
2217
by: sparks | last post by:
I am trying to fix a database that someone did about 4 yrs ago in access97. The main table just contains demographics and is on the main form of the database. It has a subform on a tab that contains the other information. it is set 1 to 1 in the relationships. simplify if I can
3
5388
by: paquer | last post by:
On my Main form I have a Command Button that opens a Subform in order to create a new Subform record. At this point I want the subform to show only the new record being created. Not all the records the subform's table has. I cannot put the subform as Data Entry because I cannot print the main form & subform together if the subform is "data entry". (comes up blank every time)
0
1850
by: jwbird | last post by:
I have a main form with multiple Subforms that are all displaying Datasheet information related to the Main form record. When I make changes/adds to the records(s) in the subform and tab to another subform, it saves those previous subform records fine, but when I make changes to the subform records and then change the mainform criteria, it doesn't save those subform records. I can use an event to prompt for saving the records on the...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.