Results 1 to 15 of 15

Thread: Pulling Data from 2 tables

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Pulling Data from 2 tables

    hey there everyone, i need some code to pull data from two tables in Access97.

    My situation is this. i have to gather some info about a certain machine and i have info in 2 different tables. i need to be able to pull the data from the 2 tables, update it if need be and then add the new info back to the two tables. i have tried a copule different appraoches to this but have been reletivly unsucessful. thanx for any help.

  2. #2
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    You need to be a little less vague...

    But generally

    Select * FROM T1 Inner Join T2 ON T1.CommonField = T2.CommonField

  3. #3
    Addicted Member
    Join Date
    Dec 2001
    Posts
    158
    He'd need some type of matching key field for that right though?

    Depending on what he's doing it could end up being a Union or even he could open two record sets at the same time... ?

  4. #4
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    Yeah, the matching key field is the T1.Field = T2.Field

    Assuming that the 2 tables he wants data from are related in some way.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    the tables are related in a field. they share a common machine number that is how i am getting info on a certain machine from each table. my problem is updating each table so that if info for one is changed or updated then the other one needs to be as well. i haven't worked with this stuff very much so can some one tell me about the UNion and Join things? thanx for all the help

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i have tried to use the SQL statements and i think that i have them down but i need to know how to put the values that you get from the statements to a VB front end. if you would like i can post code. i did it the way i thought it would be but i got an error. please some one help

  7. #7
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    Post the structure of your table, what you're trying to achieve and how you're going about it.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    Post the structure of your table, what you're trying to achieve and how you're going about it.
    do u need the stucture of both tables that i am trying to pull data from or just the one that i am trying to make?

  9. #9
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Post the structures to both tables, your Sql statements that don't work and what the error(s) are.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i have to take some info from both the tables to make a new one, but the new one has to be able to have more than one entry bc it is a table that ensures that a yearly safety test be done on each machine so there will be more than one entry in the resulting table for each machine.
    my table structures are as follows

    Testble
    RNum-PK(Autonumber)
    IDNum-FK
    NeedACheck
    Supervisor
    Date

    tblMachines
    id-PK
    machine_name
    deptID
    NeedsCheck

    ------------------------------------------------
    my sql statments
    SELECT Testble.IDNum, tblMachines.machine_desc, tblMachines.NeedsCheck, Testble.NeedACheck, Testble.date tblMachines.deptID, Testble.Supervisor
    FROM tblMachines, Testble
    WHERE tblMachines.id = Testble.IDNum

    that is what i think it would be i had some other SQL statements but i deleted them so i can't give the error.
    Attached Files Attached Files

  11. #11
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    SELECT * FROM tblMachines INNER JOIN testble ON tblMachines.ID = testble.IDNUM

  12. #12
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    From personal experience I wouldn't use an inner join, as it only returns records with matching ids. This might sound good, but if you start pulling statistics, it can leads to discrepancies.

    I prefer to use the left join, and where clause filtering on not is null if required. At least you know you have everything in the left table with the left join

    Of course, for you - it'd be best if you can run both inner and left joins to compare and make the decision yourself.

    Also see second top thread, and you other posting on joins (I replied there too)


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    this may sound like a dumb question but......how do you know which is the left and which is the right table? is it the order in which thay appear in your sql statement.

  14. #14
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Code:
    table1 left join table2 on table1.id=table2.id
    table1 is the left side table

    It gets tricky after that lol.
    Code:
    (table1 left join table2 on table1.id=table2.id)
    left join table3 on table1.id=table3.id
    or trickier....


    I have to go - finishing 'work'
    Good Luck
    Experiment

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    thanx a whole bunch sir

Posting Permissions

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



Click Here to Expand Forum to Full Width