• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

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

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
niceoneishere
Asked:
niceoneishere
  • 6
  • 5
3 Solutions
 
peter57rCommented:
Just repeat lines 6-8 changing the table name
0
 
niceoneishereAuthor Commented:
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
 
peter57rCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
niceoneishereAuthor Commented:
Sorry I changed the code as you suggested but I am still get the same error.

Thanks
0
 
niceoneishereAuthor Commented:
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
 
peter57rCommented:
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
 
niceoneishereAuthor Commented:
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
 
peter57rCommented:
Yes, in principle, but I don't see what theree is to update.  The only field you are inserting is the id.
0
 
niceoneishereAuthor Commented:
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
 
peter57rCommented:
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
 
niceoneishereAuthor Commented:
Thanks worked out perfectly. appreciate it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now