Results 1 to 7 of 7
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046

    Extract DISTINCT values from n tables (works) but now need to add source table (i.e., add Boolean)

    Hello:

    A while back, forum expert @orange assisted me with the development of a VBA routine which does accomplish the following:

    1. Scan through n number of tables (where table name has prefix = 'tbl_').
    2. Extract all DISTINCT values across all fields within n tables.
    2. For every field across all tables, create a new table (starting with prefix 'LK_) and append all DISTINCT values.

    This process works great (see attached file "LK Tables v01.accdb"). Allow me to summarize the specific steps for the above process:
    a. Upon opening "LK Tables v01.accdb", default forms opens.
    b. Click command button "Delete LK Tables". In this case, message box will indicated "5 LK tables were deleted."

    Note: At this time, DB will show 2 admin tables (Create_LK_Tables & Data_Dictionary) + 5 source tables (tbl_MMAC, tbl_NNSY, tbl_PHNSY, tbl_PNSY, tbl_POAIRS).
    c. Now, with the form still open, click on command button "Create LK Tables". Click on message box "Seems all LK tables were dropped..."
    d. Finally, message box will display that "5 LK tables were created & loaded.".

    As part of the process, the following 5 LK tables are created:
    - LK_BLS_ACCIDENT_TYPE
    - LK_CHEMICALS_INVOLVED


    - LK_EMERGENCY_ROOM
    - LK_GENDER
    - LK_GRADE

    Again, this process works great and the 5 LK tables contain *all distinct* values across the 5 source tables and their up to 5 fields each (please note that not all 5 tables include all 5 fields though).

    ---------------

    Now, here's what I need some help with. I would like to explore whether or not it is possible to modify the VBA (i.e., module "02 Generate_LK_Tables") so that a Boolean value (0 or -1) will be automatically be added where applicable.

    Note: The following must be kept in mind:
    - Some data values exist in one and only one table. For example, the value "Fall, slip, trip" (in table LK_BLS_ACCIDENT_TYPE) only exists in source table [tbl_POAIRS].
    - Alternatively, other data values may exist in multiple tables but not all tables. For instances, the value "10" (in table LK_BLS_ACCIDENT_TYPE) exists in 3 out of 5 source tables: tbl_MMAC, tbl_NNSY, tbl_PHNSY.

    I have attached an image that depicts how the modified VBA should generate, e.g., table "LK_BLS_ACCIDENT_TYPE". So, ultimately, I would be able to filter on "-1" to identify where each of the distinct values may have come from. Naturally, same principle applies to the other 4 fields. If table contains the field name, add a "-1" where value/source table intersect.

    How can this modification be accomplished?

    Thanks,
    Tom
    Attached Thumbnails Attached Thumbnails Current_vs_Desired_Output.jpg  
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    One approach is to create a record for each value/source table pair in each LK table then use each LK table as source for a CROSSTAB query to get the matrix output.

    Another is to add fields to LK tables for each source table.

    Instead of deleting and recreating tables, an option is to delete records, which is what I use. I would probably have one LK table with a Category field.

    Consider simple code example for first approach with a single LK table (assumes no memo type fields):
    Code:
    Public Sub GetData()
    Dim db As DAO.Database, rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM Data_Dictionary WHERE Field_Name <> 'Auto_ID'")
    CurrentDb.Execute "DELETE * FROM LK"
    Do While Not rs.EOF
        db.Execute "INSERT INTO LK(Data,Category,Source) SELECT DISTINCT " & rs!Field_Name & ",'" & rs!Field_Name & "','" & rs!Table_Name & "' FROM " & rs!Table_Name
        rs.MoveNext
    Loop
    End Sub
    And then a CROSSTAB for BLS_ACCIDENT_TYPE

    TRANSFORM Nz(Count("*"),0) AS X
    SELECT LK.Data
    FROM LK
    WHERE (((LK.Category)="BLS_ACCIDENT_TYPE"))
    GROUP BY LK.Data
    PIVOT LK.Source IN ("tbl_MMAC","tbl_NNSY","tbl_PHNSY","tbl_PNSY","tbl _POIRS");

    Now for the other approach code:
    Code:
    Public Sub GetData_M()Dim db As DAO.Database, rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM Data_Dictionary WHERE Field_Name <> 'Auto_ID'")
    CurrentDb.Execute "DELETE FROM LK_M"
    Do While Not rs.EOF
        db.Execute "INSERT INTO LK_M(Data,Category," & rs!Table_Name & ") SELECT DISTINCT " & rs!Field_Name & ",'" & rs!Field_Name & "',1 FROM " & rs!Table_Name
        rs.MoveNext
    Loop
    End Sub
    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.

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Tom,

    Here is an updated file that should do what you're after, I am adding the "source table" fields to the LK_ tables in a loop and updating it as we go.

    Cheers,
    Vlad
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    @Vlad --very nice --more direct /compact than my first thoughts. Well done!

  5. #5
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    @ Vlad -- this is BEAUTIFUL... the VBA modification is exactly what I was looking for.

    Quick update on the integration:
    1. Again, VBA sub routine works great w/ the sample data.
    2. Once integrated, I ran into some errors where the fieldnames were equal to "memo" (long text).
    3. I further reviewed the LK tables and determined that -- for this process -- I only need a subset of fieldnames (not the FFs aka free forms).
    4. Thus, based on #3, all LK tables are successfully processed and show the source tables... fantastic.

    Cheers,
    Tom

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Tom,
    The errors from the Memo fields are caused by the existing data itself (which could also happen in a short text): the SQL code is broken by the double-quotes (inches), I am reviewing it right now.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    Vlad -- yes, it would be great to have a routine which processes ALL LK tables (regardless of data type). Appreciate any additional advice that would prevent the error WARNING.

    Cheers,
    Tom

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

Similar Threads

  1. Replies: 53
    Last Post: 06-24-2021, 10:49 AM
  2. Replies: 26
    Last Post: 03-20-2021, 04:06 PM
  3. Replies: 3
    Last Post: 12-10-2015, 12:18 PM
  4. Replies: 10
    Last Post: 11-25-2014, 06:35 PM
  5. How to query boolean values from table
    By kevdmiller in forum Queries
    Replies: 2
    Last Post: 11-30-2006, 07:41 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