Results 1 to 4 of 4

Thread: Using INNER JOIN on multiple table columns for sql query in VB

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    52

    Using INNER JOIN on multiple table columns for sql query in VB

    Hello!
    I am trying to find common elements between 24 tables in access using VB 6.0. Currently I have developed the code for finding common elements between two columns in two tables only, you can see my pasted code.

    The elements in the column "CustomerNo" have integer values, and the sql string returns the common elements found from the CustomerNo columns of two tables. This is the code:


    Code:
    Dim sql(0 To 11) As String
    Dim month(0 To 11) As String
    Dim lngPosition As Long
    
    Dim mtype As Variant
    mtype = Array("Month1", "Month2", "Month3", "Month4", "Month5", "Month6", "Month7", "Month8", "Month9", "Month10", "Month11", "Month12", "Month13", "Month14", "Month15", "Month16", "Month17", "Month18", "Month19", "Month20", "Month21", "Month22", "Month23", "Month24")
    Dim mno As Variant
    mno = Array("Month_1_and_2", "Month_3_and_4", "Month_5_and_6", "Month_7_and_8", "Month_9_and_10", "Month_11_and_12", "Month_13_and_14", "Month_15_and_16", "Month_17_and_18", "Month_19_and_20", "Month_21_and_22", "Month_23_and_24")
    
    'INNER JOIN 24 records to obtain 12 records
    For lngPosition = LBound(month) To UBound(month)
    
    sql(lngPosition) = "SELECT DISTINCT " & mtype(lngPosition) & ".CustomerNo " _
    & "FROM " & mtype(lngPosition) & " INNER JOIN " & mtype(lngPosition + 1) & "" _
    & " ON " & mtype(lngPosition) & ".CustomerNo = " & mtype(lngPosition + 1) & ".CustomerNo"
    Set rs = cn.Execute(sql(lngPosition))
    .........................
    .....................
    .................
    Currently I am using this string, to find common elements amongst two table columns "CustomerNo", and the result from this is used with the third table column and so on for all 24 columns (uses a looping mechanism) to find the common values in all 24 table columns. The code is tidious and very slow indeed and not very effective.

    If I want to find the common for the CustomerNo in between 24 tables? Is it possible to use this sql string or looping in my case is better? The 24 tables have the CustomerNo column, however the number of rows are different in each table. My only concern is using 24 Tables with INNER JOIN separately, would be very complex.. Any ideas on how to solve this?

    regards,
    JD
    Last edited by awyeah; Apr 20th, 2008 at 10:17 PM. Reason: Changing content..

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