473,378 Members | 1,447 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,378 software developers and data experts.

Save subform record to a table

robin a
30
Hi,
I have a main form which contains an employee ID text box the user enters. Then from a combo box CboDept the user chooses dept which populates another combo box where the user chooses job title. CboJob_Title There is a subform (Class_Catalog subform)(set as continuouse) that is linked to the main form (Emp_New) by job title. From the job title the subform populates Class_ID. There may be anywhere from 0 to 15 lines populated based on selection in main form.

My dilemma is getting the subform info to save to a table named Classes_taken. Each record is comprised of Emp_ID and Class_ID. At the same time I need the main form to save its record to Emp table.

I have an INSERT INTO SQL statement but its not working:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdSave_Click()
  2.  
  3.      Dim mySQL As String
  4.  
  5.      mySQL = "INSERT INTO Classes_taken([Emp_ID],[Class_ID])VALUES(" & Forms![Class_Catalog subform]![TxtEmp] & "," & Forms![Class_Catalog subform]![TxtCID] & ");"
  6.  
  7.     Debug.Print mySQL
  8.     DoCmd.RunSQL mySQL
  9.  
  10.  
  11. End Sub
Jul 27 '12 #1

✓ answered by twinnyfo

Robin,

Perhaps this might work, as I've taken out the WHERE clause completely. Instead, this INSERT statement merely checks to see what the forms have as current data, then inserts into the table. Again, hope this helps......

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Classes_taken ( Emp_ID, Class_ID )
  2. SELECT [Forms]![Emp_New]![txtEmp_ID] AS Emp_ID, [Forms]![Emp_New]![Class_Catalog subform].[Form]![TxtCID] AS Class_ID;
Cheers!

8 10306
Rabbit
12,516 Expert Mod 8TB
There is no need to use an INSERT statement if your form is bound to the tables.
Jul 27 '12 #2
Jerry Maiapu
259 100+
If you set up your table relationship properly you do not need to insert into table again.

My advise: Since you want to see data on the subform that depends on the values filtered from the a main form, you should have two tables. Currently I guess you have one table which seems to be the source data for the subform.

Create another table (tbldepart), remove Department and the job title columns from the existing table and have it in tbldepart table with a new ID field as AUTONUMBER (SO 3 COLUMNS IN THIS TABLE.)

Now craete a foreign key in your existing table and have a many to one realtionship with tbldepart being the one side and your existing table being the many side of the realtionship.

Use form wizard to create a form by selecting the two tables-a main from and a subform will be created automatically.

Create 2 combos on the main form(one for department and another for job title) using wizard. Follow the wizard and select the option to filter the subform based on the values selected in the main form.

Cheers!

JM
Jul 28 '12 #3
robin a
30
Thank you for your responses. This is how mine is set up. My main form is named Emp_New and is based on the Emp table and saves to that table when i click save. I have two subforms, but I'll just worry about one for now. It is called Class_Catalog_subform. The link is parent: CboJOb_Title and Chid: Job_title. Based on the job title selection in the main form, the subform populates Class_Name and Class_ID which it gets from the Class_Catalog table. The Class_ID record sources is Class_ID from the Class Catalog table. The problem is, I want to save Class_ID to a table named Classes_taken. The subform also has a text box with =[Forms]![Emp_New]![txtEmp_ID] as the record source that simply lists the employee ID typed into txtEmpID on the main form for each class that is listed. I need this to save with each Class_ID listed to Classes_taken table. I'm pretty sure I need code to save the subform records where I need to.

I've attached my database, I've run out of ideas. all help is appreciated.
Attached Files
File Type: zip Training1.zip (203.1 KB, 376 views)
Aug 1 '12 #4
ariful alam
185 100+
In your database, U used a Main form that has two sub forms. 1st sub form has a link field but 2nd sub form has no field linked to the main form. you used save button for all the forms to save. but whenever you use the sub form in a main form, i think you do not need to use save button. whenever you save the main form data the linked sub form data will also save the specific table of the sub forms.

how many sub forms in a main form you have it doesn't matter. the matter is all the sub forms should linked to the main form using a specific field that is common in main form and sub form.
Aug 2 '12 #5
twinnyfo
3,653 Expert Mod 2GB
robin a,

Based on looking at your database, I'm not exactly sure what you are trying to do with your Class_Catolog subform. Are you trying to be able to select a particular class from the subform and then declare that it has been taken by the employee on a particular date? If so, there are no controls on your subform that would indicate that is what you are trying to do.

If this is the case, you should have a command button in the detail section of the form next to the course, so that when you click that button, the appropriate data is transferred to the Classes_taken table. This is very easily done, but I would use VBA instead of a macro, as you currently have a macro that simply goes to a new record.

Right now, form the database you provided, it's just not clear exactly what you are trying to do, so based on your question, this is the answer I provide.

Your Append Query also was not working properly, so I recommend the folowing to fix it. You could run this query from VBA or macro, but I recommend VBA to get accustomed to the better functionality of the code.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Classes_taken ( Emp_ID, Class_ID, Date_Taken )
  2. SELECT Emp.Emp_ID, Class_Catalog.Class_ID, Date() AS Date_Taken
  3. FROM Emp, Class_Catalog
  4. WHERE (((Emp.Emp_ID)=[Forms]![Emp_New]![txtEmp_ID]) AND ((Class_Catalog.Class_ID)=[Forms]![Emp_New]![Class_Catalog subform].[Form]![TxtCID]));
  5.  
Hope this helps out a bit for you....
Aug 2 '12 #6
robin a
30
Thank you everybody. I am very close! Twinnyfo, i used your code and left off the Date_Taken field because I don't need that in this instance. The code I'm left with for the append query is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Classes_taken ( Emp_ID, Class_ID )
  2. SELECT Emp.Emp_ID, Class_Catalog.Class_ID
  3. FROM Emp, Class_Catalog
  4. WHERE (((Emp.Emp_ID)=[Forms]![Emp_New]![txtEmp_ID]) AND ((Class_Catalog.Class_ID)=[Forms]![Emp_New]![Class_Catalog subform].[Form]![TxtCID]));
  5.  
When I run this on the click event of the save button on the main form it says that 0 records were appended. My question about the query is the SELECT statement, should it say the Emp_ID is coming from the Emp table when the Emp_ID is a new one that is being input on the Emp_new main form? I don't know if it makes a difference that it doesn't already exist in the Emp table. Other than that, I think the code should work. What do you think? I really appreciate this.
Aug 2 '12 #7
twinnyfo
3,653 Expert Mod 2GB
Robin,

Perhaps this might work, as I've taken out the WHERE clause completely. Instead, this INSERT statement merely checks to see what the forms have as current data, then inserts into the table. Again, hope this helps......

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Classes_taken ( Emp_ID, Class_ID )
  2. SELECT [Forms]![Emp_New]![txtEmp_ID] AS Emp_ID, [Forms]![Emp_New]![Class_Catalog subform].[Form]![TxtCID] AS Class_ID;
Cheers!
Aug 2 '12 #8
robin a
30
YOU ARE A GENIUS! THAT DID IT! Thanks to you I started a SQL training course. I am so happy I am beside myself! I now have to figure out how to save the All_classes subform emp_ID and class_ID to the Classes_taken form but now I think maybe a second append query may do it. I'll work on it. I seriously can't thank you enough!

Robin
Aug 2 '12 #9

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

Similar topics

2
by: dixie | last post by:
I know I've asked this before, but the answer elludes me and the message has long since disappeared from my newsgroup messages. How do you save a record using vba from a button or as part of an...
0
by: tnks | last post by:
Hello i need to save a virtual table (dataset table) into a SQL table somo one know how to do it ?? (this is part of my code) Dim sqladapt As SqlDataAdapter Dim dsOC As New DataSet Dim...
0
by: sara | last post by:
I have a simple app, and I'm trying to add Orders. I have tblOrders and tblOrderDetails. First, the user selects the customer, then "orders", "new Order" The frmNewOrder simply assures the...
4
by: tarafinlay | last post by:
Hi all, I am new to access and am finding it a bit unintuitive having worked with SQL server in the past... And I am in a bit of a hurry because my employer wants me to crank something out which...
1
by: tkhouk | last post by:
I'm using Access 2003... I'm date and time stamping a record (to fields in a table) whenever it is accessed in the form and then the control is then sent back to the lookup box. While on the form,...
0
by: neoupadhyay | last post by:
Hi community...plz help me...i wann to save this record in database via store procedure.....SP has no error...plz look... plz see the code and suggest where i mistake...coz i find a error to...
2
by: Ian | last post by:
I am trying to save the current record on a form before opening a report, doesn’t sound to hard does it? The code on a buttons on click event goes like this: DoCmd.DoMenuItem acFormBar,...
1
by: lionelm | last post by:
Hi there, I have the following tables: ProductionRun: {ProductionRunID(Primary Key), Product(foreign key referencing ProductRecipe table)} ProductRecipe: {Product(primary key), ....} ...
12
by: bluemoon9 | last post by:
hi, I've written this code to save the record without closing the form so instead of write the code as docmd.close, acform "fdlgNewAccount",,,acSaveYes docmd.save, acForm "fdlgNewAccount" 'to...
2
by: dvdsackey | last post by:
I want to save the record on a form before creating a report. Shit+Enter could do it but I would like to add a "save record" to a click event of a button that creates the report. Can anyone...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.