Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This site is truly a marvel. Without a doubt the most comprehensive, friendly and just plain useful resource of its kind..."

Geography

Where in the world do Tek-Tips members come from?

INSERT into the table if the data does not exist

ramindya1234 (Programmer)
12 Jun 12 20:37
Database information:

I have three tables Patient , Scheduling and Tracking tables

Patient table has ( MRN, Name ) Where MRN is Primary Key.
Scheduling table has ( ID, MRN, StudyID ) WHERE ID ( Autonumber) is primary key.
Tracking table has ( ID, MRN, StudyID ) WHERE ID ( Autonumber) is primary key.

The relationship between Patient table and the Scheduling table is one to many relationship.Similarly, the relationship between Patient table and the Tracking is table is one to many relationship.
Because there can be any number of health studies for a patient.

Question:
The form Scheduling shows the MRN and StudyID and when I hit save the MRN and STUDYID saves in the Scheduling table but the same data should be inserted into Tracking table if the same combination does not exist.

For example: Scheduling table has the following data:
MRN STUDYID
1234 1
1234 2

and so when I open the form it will show MRN as 1234 and StudyID as 2 or 1 depending on my previous form selection of the study ID.

Assume if initially the Tracking table has only following data:
MRN STUDYID
1234 1

and if it doesnt have
MRN STUDYID
1234 2

Then when I save the form scheduling with MRN = 1234 and Studyid = 2

The Tracking table should also automatically have the following data:
MRN STUDYID
1234 1
1234 2

ISSUE/SOLUTION ?
Basically I like to write a VBA code behind the form Scheduling where it should save the data in the table Tracking if the data ( MRN and STUDYID) does not exist.

I got the approximate syntax but it does not work

strSQL = "SELECT dbo_tblScheduling.MRN, dbo_tblScheduling.STUDYID INTO dbo_tblTracking FROM dbo_tblScheduling WHERE not exists (select dbo_tblTracking.MRN, dbo_tblTracking.STUDYID FROM dbo_tblTracking Where dbo_tblTracking.MRN = dbo_tblScheduling.MRN AND dbo_tblTracking.STUDYID = dbo_tblScheduling.STUDYID)"

or

'INSERT INTO dbo_tblTracking (MRN, STUDYID) SELECT MRN, STUDYID FROM dbo_tblTracking WHERE not exists (select MRN, STUDYID FROM dbo_tblScheduling Where dbo_tblTracking.MRN = dbo_tblScheduling.MRN AND dbo_tblTracking.STUDYID = dbo_tblScheduling.STUDYID)

Can somebody provide me help? I am a beginner.

-Ram
PHV (MIS)
13 Jun 12 5:55
What about this ?
INSERT INTO dbo_tblTracking (MRN,STUDYID) SELECT dbo_tblScheduling.MRN,dbo_tblScheduling.STUDYID
FROM dbo_tblScheduling LEFT JOIN dbo_tblTracking ON dbo_tblTracking.MRN=dbo_tblScheduling.MRN AND dbo_tblTracking.STUDYID=dbo_tblScheduling.STUDYID
WHERE dbo_tblTracking.MRN IS NULL

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close