SQL Getting values from 3 different tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metalheadstorm
    New Member
    • Sep 2007
    • 84

    SQL Getting values from 3 different tables

    Ok this is starting to hurt my head :)

    I have a Invoice table that has Invoice_Id , Customer_Id and Product_Code

    Invoice_Id is the primary key
    Customer_Id is a foreign key
    Product_Code is a foreign key

    so im making a data report and i can get it to work with connecting an retrieving data from the invoice and customer table but when i try and get data from the product table it fails

    this is what i have atm

    InvoiceSQL = "SELECT * From Invoice INNER JOIN Customer ON Customer.Custom er_Id = Invoice.Custome r_Id WHERE Invoice.Invoice _ID=" & Txt_InvoiceID.T ext & " INNER JOIN Product ON Invoice.Product _Code = Product.Product _Code WHERE Invoice.Product _Code =" & Txt_InvoiceProd Code.Text & ""

    can anyone help ;)

    btw im using vb6 and MS Access
  • metalheadstorm
    New Member
    • Sep 2007
    • 84

    #2
    I was looking back and i dontthink i need the

    WHERE Invoice.Product _Code =" & Txt_InvoiceProd Code.Text & ""

    at the end

    Comment

    • metalheadstorm
      New Member
      • Sep 2007
      • 84

      #3
      Still need help though :) cant go any further without sorting this ^^

      sorry for being impatient

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by metalheadstorm
        Still need help though :) cant go any further without sorting this ^^

        sorry for being impatient
        Sorry, you're too impatient - I'm going to ban you for life... :D

        Seriously, I'm going to move this thread to the Access forum, as you'll find much more SQL expertise there.

        I believe it would help if you could copy and paste here the actual contents of the string passed to the SQL interpret (in other words, the srting built by the code you showed us).

        And also, could you give a more detailed explanation of what "fail" means in this context? For all we know it might have retrieved the wrong data, retrieved nothing, returned an error, blown up the PC...

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          OK
          You can't have 2 different where clauses in the same query like that.

          change it to something like this
          Code:
          SELECT * 
          FROM Invoice 
          INNER JOIN Customer ON Customer.Customer_Id = Invoice.Customer_Id 
          INNER JOIN Product ON Invoice.Product_Code = Product.Product_Code 
          WHERE Invoice.Invoice_ID=" & Txt_InvoiceID.Text & " 
                 and Invoice.Product_Code =" & Txt_InvoiceProdCode.Text & ""
          Last edited by Killer42; Feb 4 '08, 05:22 AM. Reason: Fix closing CODE tag

          Comment

          • metalheadstorm
            New Member
            • Sep 2007
            • 84

            #6
            Hmm i seem to get this error


            syntax error(missing Operator) in query expression 'Customer.Custo mer_Id = Invoice.Custome r_Id INNER JOIN Product ON Invoice.Product _Code = Product.Product _Code'.

            Comment

            • metalheadstorm
              New Member
              • Sep 2007
              • 84

              #7
              Anyone got any ideas ?: D

              Comment

              • Delerna
                Recognized Expert Top Contributor
                • Jan 2008
                • 1134

                #8
                not from that ??
                Are you sill using a dynamic sql string?
                Have you tried SELECT @SQLString
                and examined the contents to see if the query was constructed correctly?

                Comment

                • Scott Price
                  Recognized Expert Top Contributor
                  • Jul 2007
                  • 1384

                  #9
                  Metalheadstorm,

                  Can you give us the SQL you are using now? Also, what version of Access are you working with?

                  You seem to be confusing the purpose of the WHERE clause, judging from your first posted sql statement. The WHERE clause does not provide criteria for the join, it provides criteria for the selection of the data.

                  Regards,
                  Scott

                  Comment

                  • Scott Price
                    Recognized Expert Top Contributor
                    • Jul 2007
                    • 1384

                    #10
                    You're going to end up with something looking like this:

                    SELECT INVOICE.*, INVOICE.Invoice ID, *
                    FROM (Customer INNER JOIN Invoice ON Customer.Custom erID = Invoice.Custome rID) INNER JOIN Product ON Invoice.Product ID = Product.Product ID
                    WHERE (((Invoice.Invo iceID)=[Form1].[Text1]));


                    Regards,
                    Scott

                    Comment

                    • metalheadstorm
                      New Member
                      • Sep 2007
                      • 84

                      #11
                      Oooo sorry i didnt see ther was some more replies, thats why i havent posted back yet , i will try that scot thx and try and sort this problem out :P

                      Comment

                      Working...