473,385 Members | 2,044 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,385 software developers and data experts.

Create New Record using VBA code from two tables

I have a button on a form that creates a new record, based on the current record, when the user clicks the button. The original code from the MASTER table worked fine. Now I need to add additional fields to the new record from another table: Related. When I added the code for the Related table, the fields in the Related table did not populate on the new record. Can someone please help? The code is below. Thanks

Expand|Select|Wrap|Line Numbers
  1. Private Sub Add_Click()
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     Dim rs2 As DAO.Recordset
  5.  
  6.     On Error Resume Next
  7.  
  8.     Set db = CurrentDb
  9.     Set rs = CurrentDb.OpenRecordset("SELECT * FROM [MASTER]")
  10.     rs.AddNew
  11.     rs![Number] = Me. Number.Value
  12.     rs![Title] = Me.Title.Value
  13.     rs![Year] = Me.Year.Value
  14.     rs![Schedule] = Me.Schedule.Value
  15.     rs![Comments] = Me.Comments.Value
  16.     rs![Statement] = Me.Statement.Value
  17.     rs![HistoryChanges] = Me.HistoryChanges.Value
  18.     rs![Orig_PubDate] = Me.Orig_PubDate.Value
  19.     rs![Last_PubDate] = Me.Last_PubDate.Value
  20.     rs![Keywords] = Me.Keywords.Value
  21.     rs.Update
  22.     rs.Close
  23.     db.Close
  24.     Set rs = Nothing
  25.  
  26.     Set db = CurrentDb
  27.     Set rs2 = CurrentDb.OpenRecordset("SELECT * FROM [Related]")
  28.     rs2.AddNew
  29.     rs2![Related_1] = Me.Related_1.Value
  30.     rs2![Related_2] = Me.Related_2.Value
  31.     rs2![Related_ 3] = Me.Related_3.Value
  32.     rs2![Related_ 4] = Me.Related_4.Value
  33.     rs2![Related_ 5] = Me.Related_5.Value
  34.     rs2.Update
  35.     rs2.Close
  36.     Set rs2 = Nothing
  37.  
  38.     db.Close
  39.     DoCmd.Close
  40. End Sub
  41.  
Mar 25 '14 #1

✓ answered by zmbd

ah... ok,

Just noticed line23 and line 26 in your original post, delete these two lines.
Insert after line38 me.requery the form normally only looks at the table once unless something happens to force it to update.

you might want to take a look at what Allen Browne has done here:

Simpler method:
Assign default values from the last record

And then the more complex situation:
Duplicate the record in form and subform

This is post in a related thread Thread955552/Post#6

7 4801
zmbd
5,501 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. rs2![Related_1] = Me.Related_1.Value 
  2.     rs2![Related_2] = Me.Related_2.Value 
  3.     rs2![Related_ 3] = Me.Related_3.Value 
  4.     rs2![Related_ 4] = Me.Related_4.Value 
  5.     rs2![Related_ 5] = Me.Related_5.Value 
  6.  
Note the spaces between the underscore and the numerics 3 thru 5? Was that intentional as there is no such space for 1 and 2.

By, simply stating that your code "doesn't work," not posting the error along with code (and omitting on which line the error occured), that appears to have had very little if any troubleshooting performed, and expecting someone to help doesn't usually result in much of an answer and may result in your thread being deleted.
Mar 25 '14 #2
@zmbd
Hi zmbd,

My mistake! The original code does not have the spaces. I don't get an error. I tried to create a record using this code, but only part of the new record was created. The fields in the Related table did not pull over to the new record and I don't know why. I am sorry if this is vague. Thanks
Mar 25 '14 #3
zmbd
5,501 Expert Mod 4TB
You should double check that all of your Me.Related_1.Value type lines are actually returning what you think they are... debug.print them out and check the immedate window by pressing <ctrl><g>

Why are you duplicating data between tables, that isn't done in a > Normalized Table Structures.
Mar 25 '14 #4
I guess what I am asking is the structure of my code right, assuming I have all of the table/field names correct? If it is, then I will have to find the problem somewhere else. I'm not sure why the new record does not include the fields Related_1, Related_2, etc.
Mar 25 '14 #5
Hi zmdb,I am not duplicating data between tables. I am pulling data from both tables on a form using a query. I then need to create a new record with most of the fields on the form. The reason is so that the users don't have to re-enter the same data on the new record as only a few fields are being changed on the new record.
Mar 25 '14 #6
zmbd
5,501 Expert Mod 4TB
ah... ok,

Just noticed line23 and line 26 in your original post, delete these two lines.
Insert after line38 me.requery the form normally only looks at the table once unless something happens to force it to update.

you might want to take a look at what Allen Browne has done here:

Simpler method:
Assign default values from the last record

And then the more complex situation:
Duplicate the record in form and subform

This is post in a related thread Thread955552/Post#6
Mar 25 '14 #7
Thanks zmbd. Good information. I will read through this and try and fix my code. I appreciate your help
Mar 25 '14 #8

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

Similar topics

1
by: vector | last post by:
I want to dynamically create tables in a SQL Server 2000 database that are modelled after XML schemas. I will not know what the schemas might look like until the documents arrive. For example,...
6
by: ApexData | last post by:
I have 2 tables: Table1 and Table2. Neither one has a primary key because each table will only have 1-record. My form is a SingleForm unbound with tabs (my desire here). Using this form, in...
4
by: elainenguyen | last post by:
I am trying to write a code that seaching a record in 3 tables and if the record exist, show the data, if the record doesn't exist, show the warning message. The following is my code that I am...
1
by: rbg | last post by:
I am using derived tables to Page data on the SQL Server side. I used this link as my mentor for doing paging on the SQL Serverhttp://msdn2.microsoft.com/en-us/library/ms979197.aspx I wanted to...
7
by: ApexData | last post by:
Hello I currently Link the FE/BE using the LinkTables Option and the Linked Table Manager. Any time I need to move the BE to another location, I have to go through this process over again. I...
1
by: biolie1984 | last post by:
Plz Help me guys... I want to delete a record from defferent tables e.g. tb1 is a parent table tb2 , tb3 , tb3 are a child table then if i want to delete the parent the record from...
1
by: pkpanda | last post by:
I have similar but bit different issue that is I have a table INVOICE when user enter data in invoice I have to insert record into Transactions (Master ) and Transaction_details (detail) both...
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
30
by: CD Tom | last post by:
I found Stephen Lebans modReportToPDF and have been trying to get it to work in my application. I've downloaded his database and when I run his test it works fine. I then added it to my application...
1
by: sknaina | last post by:
Hi Eveybody, I've a main form with a continuous subform based on 'DonorTranSmasterF' & 'DonorTranSdetailsF' , both tables are linked via 'DonorTransiD' field. I know & learn , How to save record...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...

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.