Results 1 to 5 of 5
  1. #1
    iamlathem is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3

    INSERT INTO external DB wich has a DB PAssword

    I have a database called Scheduling.mdb. In it, I have a simple form called frm_CARD. Its record source is a table called ORDERS.

    I also have another Database called Out.mde which has a database password - let's pretend the password is "1234". This database has a table called PATIENTS with fields that are identical to the fields in the ORDERS table (located in the Scheduling.mdb).



    On the CARD form (located in the scheduling.mdb) I have a buttom called SEND. When this button is clicked I need to INSERT all of the values that have been entered into the current record on that form INTO the PATIENTS table (located in the Out.mde).

    I simply cannot get any code to accomplish this task.

    For reference, I do have a query that accomplishes the same thing for multiple records and with far more fields than I need in this task. Here is the SQL for that query:

    INSERT INTO (C:\Out\Out.mde;PWD=1234) Patients ( [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, [Place #], [Cancelled?], Age, Height, Height_Inches, Weight, [Time], Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr], Prints_x, IV_Setup_x, Isotope_1, [Isotope_1_Lot#], Isotope_1_Dosage, Isotope_1_Inj_Time, Isotope_1_Inj_Site, Isotope_1_Time, Isotope_2, [Isotope_2_Lot#], Isotope_2_Dosage, Isotope_2_Inj_Time, Isotope_2_Inj_Site, Isotope_2_Time, [Tech Notes], Misc, Good, Acceptable, Unacceptable, Facility, [Sched Date], [Schedule #], [Taken By], Tech, ID, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10 )
    SELECT Orders.[Grouped_Invoice#], Orders.Patient_First_Name, Orders.Patient_Last_Name, Orders.[Place #], Orders.[Cancelled?], Orders.Age, Orders.Height, Orders.Height_Inches, Orders.Weight, Orders.Time, Orders.Exam, Orders.Diagnosis, Orders.[Room #], Orders.[Med_Rec_#], Orders.[For Dr], Orders.Prints_x, Orders.IV_Setup_x, Orders.Isotope_1, Orders.[Isotope_1_Lot#], Orders.Isotope_1_Dosage, Orders.Isotope_1_Inj_Time, Orders.Isotope_1_Inj_Site, Orders.Isotope_1_Time, Orders.Isotope_2, Orders.[Isotope_2_Lot#], Orders.Isotope_2_Dosage, Orders.Isotope_2_Inj_Time, Orders.Isotope_2_Inj_Site, Orders.Isotope_2_Time, Orders.[Tech Notes], Orders.Misc, Orders.Good, Orders.Acceptable, Orders.Unacceptable, Orders.Facility, Orders.[Sched Date], Orders.[Schedule #], Orders.[Taken By], Orders.Tech, Orders.ID, Orders.Item02, Orders.Item03, Orders.Item04, Orders.Item05, Orders.Item06, Orders.Item07, Orders.Item08, Orders.Item09, Orders.Item10
    FROM Orders
    WHERE (((Orders.Patient_Last_Name) Is Not Null) AND ((Orders.Facility) Is Not Null) AND ((Orders.[Sched Date]) Between Date() And (Date()+4)) AND ((Orders.[Schedule #])=1));

    How can I convert this SQL statement to a VBA Procedure? Obviously I need to change the WHERE clause so that only the current record gets transferred. I can do tht by using the ID field - something like: WHERE Orders.ID = Me.ID

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Options:

    1. make the query WHERE criteria dynamic with a reference to the form control:

    ((Orders.[Schedule #])=Forms!formname!ID));

    and call the query:

    DoCmd.SetWarnings = False
    DoCmd.OpenQuery ...
    DoCmd.SetWarnings = True

    2. eliminate the query object and put the entire SQL statement in VBA:

    CurrentDb.Execute "INSERT INTO (C:\Out\Out.mde;PWD=1234) Patients ( [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, " & _
    "[Place #], [Cancelled?], Age, Height, Height_Inches, Weight, [Time], Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr], Prints_x, IV_Setup_x, " & _
    "Isotope_1, [Isotope_1_Lot#], Isotope_1_Dosage, Isotope_1_Inj_Time, Isotope_1_Inj_Site, Isotope_1_Time, Isotope_2, [Isotope_2_Lot#], " & _
    "Isotope_2_Dosage, Isotope_2_Inj_Time, Isotope_2_Inj_Site, Isotope_2_Time, [Tech Notes], Misc, Good, Acceptable, Unacceptable, Facility, " & _
    "[Sched Date], [Schedule #], [Taken By], Tech, ID, Item02, Item03, Item04, Item05, Item06, Item07, Item08, Item09, Item10 ) " & _
    "SELECT [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, [Place #], [Cancelled?], OrdAge, Height, Height_Inches, Weight, " & _
    "Time, Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr], Prints_x, IV_Setup_x, " & _
    "Isotope_1, [Isotope_1_Lot#], Isotope_1_Dosage, Isotope_1_Inj_Time, Isotope_1_Inj_Site, Isotope_1_Time, Isotope_2, " & _
    "[Isotope_2_Lot#], Isotope_2_Dosage, Isotope_2_Inj_Time, Isotope_2_Inj_Site, Isotope_2_Time, [Tech Notes], Misc, Good, Acceptable, " & _
    "Unacceptable, Facility, [Sched Date], [Schedule #], [Taken By], Tech, ID, Item02, Item03, Item04, Item05, Item06, Item07, Item08, " & _
    "Item09, Item10 " & _
    "FROM Orders " & _
    "WHERE Patient_Last_Name Is Not Null AND Facility Is Not Null AND [Sched Date] Between Date() And Date()+4) AND [Schedule #])=" & Me.ID & "));"


    Sidenote: use of spaces and special characters/punctuation (underscore is exception) in names should be avoided.
    Last edited by June7; 12-05-2013 at 12:22 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    iamlathem is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    June 7,
    Thank you so much for trying to help. I don't understand why I need to call the query (or how to do so for that matter) if I am pasting the SQL directly into the VBA. Based on your advice and my very limited understanding, I tried doing the following but got a syntax error for the INSERT INTO statement.
    -------------------------------------
    DoCmd.SetWarnings = False

    CurrentDb.Execute "INSERT INTO (C:\Out\Out.mde;PWD=1234) Patients ( [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, " & _
    "[Place #], [Cancelled?], Age, Height, Height_Inches, Weight, [Time], Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr] " & _
    "SELECT [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, [Place #], [Cancelled?], OrdAge, Height, Height_Inches, Weight, " & _
    "Time, Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr] " & _
    "FROM Orders " & _
    "WHERE ((Orders.[Schedule #])=Forms!formname!ID));"

    DoCmd.SetWarnings = True
    ------------------------------------------------------

    Could you clarify?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    You don't have to call the query object if you use option 2.

    DoCmd.SetWarnings not needed with CurrentDb, won't hurt, just not needed.

    Missing a closing ): [For Dr]) " & _

    Concatenate variable. Reference to value from form is a variable. Look again at example 2 in post 2.

    "WHERE ((Orders.[Schedule #])=" & Me.ID & "));"

    Surely your form is not actually named "formname"?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    iamlathem is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    June7,
    Thank you so much! I got it to work, although I had to create a linked table and eliminate the "(C:\Out\Out.mde;PWD=1234)" from the code. This is not ideal but I can live with it. Also, in your example, "WHERE ((Orders.[Schedule #])=" & Me.ID & "));" there was one too many closing parentheses which I would not have seen if an error message hadn't popped up and specifically told me there was an extra one. Anyway, I really appreciate your help. In case you are interested I am pasting the final code that worked:

    CurrentDb.Execute "INSERT INTO Patients ([Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, " & _
    "[Place #], [Cancelled?], Age, Height, Height_Inches, Weight, [Time], Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr]) " & _
    "SELECT [Grouped_Invoice#], Patient_First_Name, Patient_Last_Name, [Place #], [Cancelled?], Age, Height, Height_Inches, Weight, " & _
    "Time, Exam, Diagnosis, [Room #], [Med_Rec_#], [For Dr] " & _
    "FROM Orders " & _
    "WHERE ((Orders.[ID])=" & Forms![Card].[ID] & ");"

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

Similar Threads

  1. Replies: 1
    Last Post: 05-29-2013, 03:08 PM
  2. Problems wich subreports
    By fabiobarreto10 in forum Reports
    Replies: 1
    Last Post: 08-07-2012, 05:12 AM
  3. Replies: 1
    Last Post: 07-24-2012, 06:10 PM
  4. Replies: 1
    Last Post: 06-22-2012, 08:05 AM
  5. Replies: 5
    Last Post: 04-01-2010, 03:48 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