Results 1 to 2 of 2
  1. #1
    ramindya is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Posts
    20

    Insert if not exists to another table.

    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

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How do I Insert Values w/ Not Exists sub select?
    By toddbailey in forum Queries
    Replies: 1
    Last Post: 05-01-2012, 12:05 PM
  2. if exists UPDATE else INSERT
    By lloyddobler in forum Programming
    Replies: 18
    Last Post: 01-04-2012, 11:35 PM
  3. If table exists then
    By SorenIX in forum Programming
    Replies: 2
    Last Post: 06-26-2011, 08:42 AM
  4. SQL - find out whether a table exists
    By yurako in forum Programming
    Replies: 2
    Last Post: 01-20-2010, 09:27 AM
  5. Create Table If Not Exists
    By jp2access in forum Queries
    Replies: 7
    Last Post: 07-14-2009, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums