Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    327
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert With Values From Table and a Form (Access 2000)

    OK, I am back again, and this one is tricky (for me anyway). Basically, I am trying to insert a series of records into a table once a new product is added. What I need to do have my values come from a table AND a form. I am trying to get all the consultants from a table and insert there ID into a table. I am also getting values from a form. (Right Now it is just one, but will be more)

    Currently I have:

    sqlInsert = "INSERT INTO NewProductsList (ConsultantID, NewProductID) " _
    + "SELECT Consultants.ConsultantID, VALUES (TempNewProductID)" _
    + "FROM Consultants WHERE (Consultants.Type = 'Consultant' AND Consultants.Status = 'Active' AND Product.ProductID = " & TempNewProductID & ")"

    DoCmd.RunSQL sqlInsert

    Any ideas would be great

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Insert With Values From Table and a Form (Access 2000)

    There are several problems with your SQL statement:
    <UL><LI>You are mixing the SELECT and VALUES form of an append query; in this case, you should use the SELECT form.
    <LI>You have TempNewProductID within the quotes; that way, you won't use its value.
    <LI>You have Products.ProductID in the WHERE part, but Products is not specified in the FROM part.
    <LI>(Minor quibble) I prefer to use & to concatenate strings instead of +. It doesn't hurt here, but when you are concatenating numerical values in strings, the + can lead to confusion.[/list]Assuming that this is run from a form, and that TempNewProductID is the name of a control (text box) on the form, and that NewProductID is numeric, the following string may work (but not knowing the exact situation, I can't be sure):

    sqlInsert = "INSERT INTO NewProductsList (ConsultantID, NewProductID) " _
    & "SELECT Consultants.ConsultantID, " & TempNewProductID & _
    & " FROM Consultants WHERE (Consultants.Type = 'Consultant' AND Consultants.Status = 'Active')"

  3. #3
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Insert With Values From Table and a Form (Access 2000)

    What happens when you try and run this code?

    I would change your SQL statement to read:

    sqlInsert = "INSERT INTO NewProductsList (ConsultantID, NewProductID) " _
    + "SELECT Consultants.ConsultantID, " & Me!TempNewProductID & " " _
    + "FROM Consultants WHERE (Consultants.Type = 'Consultant' AND Consultants.Status = 'Active' AND Product.ProductID = " & Me!TempNewProductID & ")"

    Pat

  4. #4
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Calgary, Alberta
    Posts
    327
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Insert With Values From Table and a Form (Access 2000)

    Hi Guys,

    The example I gave was a mix of a 2 trys, so it looke dowrse then it was [img]/forums/images/smilies/sad.gif[/img]

    Thanks for your help once again Hans! Your Solution worked great. I will change my string concatenations to the & from now on.

    Pat, you solution worked good as well

    Thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •