Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 7th, 2007, 09:16 AM
Authorized User
 
Join Date: Oct 2006
Location: Kolkata, West Bengal, India.
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.

Reply With Quote
  #2 (permalink)  
Old November 7th, 2007, 04:21 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #3 (permalink)  
Old November 8th, 2007, 02:31 AM
Authorized User
 
Join Date: Oct 2006
Location: Kolkata, West Bengal, India.
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.

Reply With Quote
  #4 (permalink)  
Old November 8th, 2007, 11:18 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:34 AM.


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