Wrox Programmer Forums
|
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 November 7th, 2007, 09:16 AM
Authorized User
 
Join Date: Oct 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert Into Parent Child Table

Hello Code Gurus,

               Can you please help me regading following problem?

TABLE1:

studentID(PK)
studentName

TABLE2:

studentID(FK)
studentMarks

These two table has one-to-one relationship.Now how to insert data into two tables simultaneously?Is it possible with one SQL statement?

Thanks in Advance.

 
Old November 7th, 2007, 04:21 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I think you mean one to many? Table2 would have these fields:

TABLE2
Table2ID
studentID(FK)
courseID(FK)
studentMarks

This would make it a junction table, and therefore you have a many-to-many relationship.

If you want to insert into Table2, you will want to have the studentID AND the courseID. You can take these on a form, and then you would have a grade selection as well on the form. If these were all combos (including the grade - value list) then you would do this:

Dim iStud As Integer
Dim iCourse As Integer
Dim sGrade As String
Dim sSQL As String

iStud = Me.cboStudent
iCourse = Me.cboCourse
sGrade = Me.cboGrade

sSQL = "INSERT INTO Table2(studentID, courseID, studentMarks)" & _
       " VALUES(" & iStud & ", " & iCourse & ", '" & sGrade & "')"

DoCmd.RunSQL(sSQL) etc

If you want to create a student and a course and a grade all at one time, that is a little more problematic. Did this help?



mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old November 8th, 2007, 02:31 AM
Authorized User
 
Join Date: Oct 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mmcdonal for ur reply.

But my both tables have one-to-one relationship. And I want to insert data into both tables simultaneously. For example.

For Table1-(S0001,Mr.John)
For Table2-(S0001,60%)

I want to insert above data using one SQL statement,so that both data will be inserted at the same time.Is it possible? Please help.

 
Old November 8th, 2007, 11:18 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Data normalization issues aside, you will probably have to use two transactions since there may not be a StudentID PK when you do this all at once. Is it a problem to do this in two steps in one sub?

Like:

"INSERT INTO Table1(studentID, studentName)" & _
       " VALUES(" & iStud & ", '" & sSName & "')"

"INSERT INTO Table2(studentID, studentMarks)" & _
       " VALUES(" & iStud & ", '" & sGrade & "')"





mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
parent child connect s2mo SQL Server 2005 1 February 20th, 2007 07:16 AM
Parent - Child Combo babloo81 BOOK: Professional Jakarta Struts 0 April 27th, 2005 01:54 PM
Parent - Child Combo babloo81 JSP Basics 0 April 27th, 2005 01:46 PM
Update parent table with the sum of child table gbrown SQL Language 2 November 9th, 2004 07:53 AM
Parent and Child Records in Same Table owain SQL Language 12 October 27th, 2004 10:02 AM





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