Solved

onclick of a command button insert data into mutliple tables at once in Access 2007

Posted on 2012-03-18
11
533 Views
Last Modified: 2012-03-26
Hello Experts,

I have table in Access 2007 called Students whose fields are
ID autonumber
Student ID Number
Student Name Text

I created a form based on this table called Student Form. on this form I have a command Button called Save. Basically What I am looking is onclick of this Save button I want to grab the Student ID from Student table and Insert into Student ID of Billing table, Student ID of Grades Table, Student ID of Financial Aid table.

Is this possible, I know I can insert into one table by using the code below

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click


   Dim strSql As String
   strSql = "INSERT INTO Billing([Student ID]) SELECT Students.[Student ID] from Students;"
   
   DoCmd.RunSQL strSql
   

Exit_cmdSave_Click:
    Exit Sub

Err_cmdSave_Click:
    MsgBox Err.Description
    Resume Exit_cmdSave_Click
   
End Sub

Open in new window



Can Insert into multiple tables using the same concept.

Thanks and appreciate it
0
Comment
Question by:niceoneishere
  • 6
  • 5
11 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 37736165
Just repeat lines 6-8 changing the table name
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37737806
Thanks for replying I changed the code to below and now its throwing an run time error 3134 insert into statement error on the line DoCmd.RunSQL strSql.

here is update code
Dim strSql As String
   strSql = "INSERT INTO Billing(Student_ID] SELECT Students.Student_ID from Students;"
   strSql = "INSERT INTO Grading(Student_ID] SELECT Students.Student_ID from Students;"
   strSql = "INSERT INTO [Financial Aid](Student_ID] SELECT Students.Student_ID from Students;"
   strSql = "INSERT INTO Sports(Student_ID] SELECT Students.Student_ID from Students;"
      
   DoCmd.RunSQL strSql

Open in new window


Thanks and appreciate it
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 37737913
No; I said repeat lines 6-8 not just line 6.

Dim strSql As String
   strSql = "INSERT INTO Billing(Student_ID] SELECT Students.Student_ID from Students;"
  DoCmd.RunSQL strSql

   strSql = "INSERT INTO Grading(Student_ID] SELECT Students.Student_ID from Students;"
  DoCmd.RunSQL strSql

   strSql = "INSERT INTO [Financial Aid](Student_ID] SELECT Students.Student_ID from Students;"
  DoCmd.RunSQL strSql


   strSql = "INSERT INTO Sports(Student_ID] SELECT Students.Student_ID from Students;"
    DoCmd.RunSQL strSql

Your error suggests that there is something wrong with the last query.
But you need to test with the above code first and see what happens.
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37738469
Sorry I changed the code as you suggested but I am still get the same error.

Thanks
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37738532
Ok I got it working but I have something come up, when I click on the save button , it shows an alert box saying you are about append 3 rows which is fine but the message comes for each and every table. How make that message come just for once.

Thanks and appreciate your help
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 500 total points
ID: 37738662
The warning message applies to an individual query.  I think it would be misleading to turn off messages for 3 of the queries and leave them on for one .  You might as well stop them altogether.  Although be aware that this means that errors will not get reported.

One way to do that is to add the line below after the dim statement..

Docmd.Setwarnings False

and add the next line after the last Runsql command

Docmd.Setwarnings True
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37739201
Ok thanks for the tip, can I ask you something else sir, Instead of inserting all the time, is there any way to modify the code in such way that if Student ID contains a value just upadate it rather then inserting and if its blank insert it.

Thanks
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37741368
Yes, in principle, but I don't see what theree is to update.  The only field you are inserting is the id.
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37743255
I understand that sir, whats happening now is every time I am hit the button, it keeps inserting all the Student Id's instead of just the new ones. so basically in all the these tables I have the student Id repeated

Thanks and appreciate it
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 500 total points
ID: 37743394
Then you need to modify your queries to include a Where clause like...

  strSql = "INSERT INTO Billing(Student_ID] SELECT Students.Student_ID from Students where Students.Student_ID =" & me.Student_ID  

This assumes Students.Student_ID is a number field.

If it is text then you would need this version...

  strSql = "INSERT INTO Billing(Student_ID] SELECT Students.Student_ID from Students where Students.Student_ID ='" & me.Student_ID   & "'"
0
 
LVL 1

Author Comment

by:niceoneishere
ID: 37766339
Thanks worked out perfectly. appreciate it
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now