Using a SELECT in an INSERT query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bard777
    New Member
    • Jan 2008
    • 23

    #1

    Using a SELECT in an INSERT query

    I have a form kicking off an INSERT query. I need to check another table for a matching value and use a field from that table as the value for one of the fields in my insert.

    Table 1 (insert from form)

    PLN_NUM
    SLN_NUM

    Table 2

    PLN_NUM
    SLN_NUM

    I need to do something like:
    Code:
    INSERT INTO [Table 1] (PLN_NUM, SLN_NUM) VALUES (fld_PLN_NUM, (SELECT SLN_NUM FROM [Table 2] WHERE PLN_NUM = fld_PLN_NUM))
    Can somebody help with how to make this insert?

    Thanks
    Last edited by NeoPa; Aug 5 '08, 08:16 PM. Reason: Please use the [CODE] tags provided
  • bard777
    New Member
    • Jan 2008
    • 23

    #2
    I got it to work using the following:
    Code:
    INSERT INTO Table 1 (PLN_NUM, SLN_NUM)
    SELECT fld_pln_num, SLN_NUM
    FROM [Table 2]
    WHERE [Table 2].PLN_NUM = fld_pln_num
    fld_pln_num represents a textbox on the form that executes the insert.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Thanks for posting your solution. It even makes the question clearer.

      Comment

      Working...