Results 1 to 13 of 13
  1. #1
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19

    Query: Combining 2 Tables with Table1.FieldA = Table2.FieldA

    Hi All,

    I have 2 tables,
    Table1:
    Code:
    Name Account
    Name1 Account1
    Name1 Account2
    Name2 Account1
    Name2 Account2
    Name3 Account1
    Name3 Account2


    Table2:

    Code:
    User
    User1
    User2
    User3
    I want to create Table3:

    Code:
      
    
    Name Account User
    Name1 Account1 User1
    Name1 Account2 User1
    Name2 Account1 User2
    Name2 Account2 User2
    Name3 Account1 User3
    Name3 Account2 User3

    I tried to use:


    Code:
    SELECT 
    Name,Account,Dlookup("[User]","Table2","User = '" & table1.[Name] & "'")
    FROM table1,Table2

    **Name matches User - both are text string


    Seems to work but my 60 rows data (based on table1) becomes 200,000+

    How can I a make a query that will return the specific row count as table1?

    Also if there is a better way to do this as the 60 rows table1 takes 4 to 5 mins to complete and when applied to the actual data which has over 5000 rows, I had to crash access as it is taking forever.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You need a join between the two tables. In design view, click/drag to create a join between the two fields.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    hi pblady,

    i tried:

    Code:
    SELECT
    table1.name,
    table1.account,
    table2.user
    FROM table2 INNER JOIN table1
    ON table1.name = table2.user;
    however, this returns as empty table.

    any guide on the correcting this join?

    Thanks!

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The INNER JOIN should work. If you want to provide db for analysis, follow instructions at bottom of my post.
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    I am trying to recreate the database with dummy data and the join seems to work fine, but reapplying it to the actual data doesn't produce the same result.

    do you have any idea what could cause this? all my data type are text.

    Thanks!

    ****

    Edit: the dummy data is now having problem as well, will post it now

  7. #7
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    here's the replication of the data.
    DB_Analysis.accdb

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Data doesn't make sense. Both tables have duplicate names in the linking fields. How should Access know which records are to be associated?
    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.

  9. #9
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    the names would be identical, I want to match the names in table2 to names in table1 to get to the accounts they match. is that attainable in access?

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Certainly can join the tables but will the output make sense? For instance:

    There are 5 records in table1 for Manager02, there are 2 records in table2 for Manager02. Joining these tables on the manager name fields results in 10 records for Manager02. Is that what you want?

    In table1 is the ManagerID the ID for ReportingManager?
    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.

  11. #11
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    Yes the ManagerID are for the ReportingManager. I think I get where the increase in result are coming from, as you noted access returns all possible matches, am I correct to assume this?

    so I filtered each account into queries and tried to use dlookup but it is returning increased results still. I googled around using the assumption above and found a code and tweaked it a bit:


    Code:
    Function IndexMatch(ByVal LookUpField As String, ByVal LookUpTable As String, ByVal ToMatchField As String, ByVal ToMatchTable As String) As String
    Dim t As Long
    With New CTimer
    .StartCounter
        Dim dboCurrent As Database
        Dim rstQuery As Recordset
        Dim strSQLsyntax As String
        Dim recordsetcount As Recordset
        Dim strFind As String
        Dim strfound As String
        
        Set dboCurrent = CurrentDb()
        strSQLsyntax = "SELECT * FROM " & LookUpTable
        
        Set recordsetcount = dboCurrent.OpenRecordset(ToMatchTable)
        Set rstQuery = dboCurrent.OpenRecordset(strSQLsyntax)
        
        'recordsetcount.MoveFirst'
        'Do While Not (recordsetcount.EOF)
            strFind = recordsetcount.Fields(ToMatchField).Value
                rstQuery.MoveFirst
                Do While Not (rstQuery.EOF)
                    If rstQuery.Fields(LookUpField).Value = strFind Then '
                        strfound = rstQuery.Fields("Account").Value
                        Exit Do
                    Else
                        rstQuery.MoveNext
                    End If
                Loop
                Debug.Print ' strFind & " " & strfound
            'recordsetcount.MoveNext
        'Loop
                   
        IndexMatch = strfound
        Set recordsetcount = Nothing
        Set rstQuery = Nothing
        Set dboCurrent = Nothing
    t = .TimeElapsed
    End With
    Debug.Print t
    Debug.Print (t / 1000) / 60
    End Function
    The code seems to be faster than the dlookup, however it is stuck on the first record. Would you be able to advise how to reference the records so the code would move along the record set.

  12. #12
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    figured the udf above.

    is anyone familiar with "sharepoint people or group", the accounts from the access table is the same as the domain\account of the managers but I am getting null violation upon linking the table to sharepoint. any idea?

    Thanks!

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Sorry, no experience with SharePoint.

    Don't know what data is in the queries/recordsets. Don't know what ToMatchField and LookupField fields are for. Don't see anything wrong with code logic.

    Step debug. Refer to link at bottom of my post for debugging guidelines.
    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.

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

Similar Threads

  1. Replies: 1
    Last Post: 05-26-2014, 03:02 AM
  2. TABLE1 VS TABLE2...Are all ID's from Table1 in Table2???
    By smoothlarryhughes in forum Queries
    Replies: 11
    Last Post: 10-26-2012, 11:28 AM
  3. Replies: 0
    Last Post: 03-25-2011, 03:35 PM
  4. Show only rows where fielda < fieldb
    By Schon731 in forum Queries
    Replies: 6
    Last Post: 12-02-2010, 06:26 PM
  5. Replies: 3
    Last Post: 07-08-2010, 01:47 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