query duplicate problem (1 Viewer)

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
I have a database which holds information about repairs. Each repair is called a task and among other things data is held about things(s) that caused the failure and what need to be replaced. Usually it's one fault but it could be many.Replacements are usually more than 1. Bottom line, in a simplistic view, I have 3 tables task, fails and replacements. There is one to many relationship between tasks and fails and also a one to many relationship between tasks and replacements.
If there is one fail and many replacements my query is good enough as it will produce

task1 fail1 replace1
task1 fail1 replace2
task1 fail1 replace3

On the report I can hide duplicates to get the desired (. = space!!)

task1 fail1 replace1
...............replace2
...............replace3

Problem occurs when I get more than 1 fail. Query returns

task1 fail1 replace1
task1 fail1 replace2
task1 fail1 replace3
task1 fail2 replace1
task1 fail2 replace2
task1 fail2 replace3

Hiding duplicates I get

task1 fail1 replace1
...............replace2
...............replace3
.........fail2 replace1
...............replace2
...............replace3

whereas I'd like to get

task1 fail1/fail2 replace1
.....................replace2
.....................replace3


or something similar.
Any help much appreciated
 

billmeye

Access Aficionado
Local time
Today, 01:53
Joined
Feb 20, 2010
Messages
542
What you would like to do is concatenate the fails into a single list and that can be done using VBA. If you give me the primary key for the task table and foreign key for the fails table I could give you an example function.

Edit: I see Paul is already got you on that. Good Luck.
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
thanks to both - I'll take a look and report back!
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
this didn't work.
i'm wondering if it's because i actually have a many to many relationship with tasks and fails.
So I use the task ID to access the task/fails table which in turn uses the fail ID to access the fails table!
Is there a similar function for this or can I use the same one and it's because I'm not passing the correct parameters to it?
 

billmeye

Access Aficionado
Local time
Today, 01:53
Joined
Feb 20, 2010
Messages
542
Actually, you would no longer include the fails table in your query. The function takes care of that by passing the PKID.
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
Not sure what you're saying there.
I presume PKID is primary key ID?
So what do I actually pass to the function?
 

billmeye

Access Aficionado
Local time
Today, 01:53
Joined
Feb 20, 2010
Messages
542
Code:
Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant

Not knowing your tables, you are passing the function the name of the field you want concatenated, the name of the table containing the field and the ID# of the foreign key you are using to relate to the Task table.
Code:
FailList:ConcatRelated("fldFail", "tblFails", "TaskID = " & [TaskID])
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
So my 3 tables are basically
task -> fields taskid task
task fails -> taskfailsid taskid failsid
fails -> failid fails

I want to concatenate a list of fails from the fails table

So if I code in my query
name: Concatrelated("fail","fails","[failid] = " & [task fails].[failid])

I get only one fail returned

If I code

name: Concatrelated("[failid]","[task fails]","[taskid] = " & [tasks].[tasksid])

I get 2 ids returned but I need their description (fails from the fails table)!
 

billmeye

Access Aficionado
Local time
Today, 01:53
Joined
Feb 20, 2010
Messages
542
You'll need to customize the formula (thus no longer making it universal) to return your values. Here is the customized version with changes in red:
Code:
Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
    'Purpose:   Generate a concatenated string of related records.
    'Return:    String variant, or Null if no matches.
    'Arguments: strField = name of field to get results from and concatenate.
    '           strTable = name of a table or query.
    '           strWhere = WHERE clause to choose the right values.
    '           strOrderBy = ORDER BY clause, for sorting the values.
    '           strSeparator = characters to use between the concatenated values.
    'Notes:     1. Use square brackets around field/table names with spaces or odd characters.
    '           2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
    '           3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
    '           4. Returning more than 255 characters to a recordset triggers this Access bug:
    '               http://allenbrowne.com/bug-16.html
    Dim rs As DAO.Recordset         'Related records
    Dim rsMV As DAO.Recordset       'Multi-valued field recordset
    Dim strSql As String            'SQL statement
    Dim strOut As String            'Output string to concatenate to.
    Dim lngLen As Long              'Length of string.
    Dim bIsMultiValue As Boolean    'Flag if strField is a multi-valued field.
    
    'Initialize to Null
    ConcatRelated = Null
    
    'Build SQL string, and get the records.
   [COLOR="Red"] strSql = "SELECT * FROM " & strTable[/COLOR]
    If strWhere <> vbNullString Then
        strSql = strSql & " WHERE " & strWhere
    End If
    If strOrderBy <> vbNullString Then
        strSql = strSql & " ORDER BY " & strOrderBy
    End If
    Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
    'Determine if the requested field is multi-valued (Type is above 100.)
    bIsMultiValue = (rs(0).Type > 100)
    
    'Loop through the matching records
    Do While Not rs.EOF
        If bIsMultiValue Then
            'For multi-valued field, loop through the values
            Set rsMV = rs(0).Value
            Do While Not rsMV.EOF
                If Not IsNull(rsMV(0)) Then
                    strOut = strOut & rsMV(0) & strSeparator
                End If
                rsMV.MoveNext
            Loop
            Set rsMV = Nothing
        ElseIf Not IsNull(rs(0)) Then
            [COLOR="red"]strOut = strOut & Dlookup("Fail","Fails","[failid]=" & rs!failid) & strSeparator[/COLOR]
        End If
        rs.MoveNext
    Loop
    rs.Close
    
    'Return the string without the trailing separator.
    lngLen = Len(strOut) - Len(strSeparator)
    If lngLen > 0 Then
        ConcatRelated = Left(strOut, lngLen)
    End If

Exit_Handler:
    'Clean up
    Set rsMV = Nothing
    Set rs = Nothing
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
    Resume Exit_Handler
End Function
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
what should i be putting in my query
Concatrelated("fail","fails","[failid] = " & [task fails].[failid])
or
Concatrelated("[failid]","[task fails]","[taskid] = " & [tasks].[tasksid])

both return error 3265, item not found in this collection
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
No need!! Didn't copy the code in red properly - think I'm there now. Thanks a million.
 

trkl

Registered User.
Local time
Today, 09:53
Joined
Jun 30, 2013
Messages
31
Right click on a field then select properties and then in duplicate values there are ooptios of Yes and No.select no.and close property sheet by clicking close button.now you wont see duplicates in a field.
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
Right the concatenation code now works, thanks to billmeye but I'm still getting too many records!
From my original example I'm now getting in my query
task1 fail1/fail2 replace1
task1 fail1/fail2 replace2
task1 fail1/fail2 replace3
task1 fail1/fail2 replace1
task1 fail1/fail2 replace2
task1 fail1/fail2 replace3

My report looks better when I hide duplicate fails viz

task1 fail1/fail2 replace1
.....................replace2
.....................replace3
.....................replace1
.....................replace2
.....................replace3

I can't hide duplicate for replaces there may genuinely be more than 1 of the same.
 

ryetee

Registered User.
Local time
Today, 06:53
Joined
Jul 30, 2013
Messages
952
If I sort and then hide the duplicate replacements I get blank lines.
I'd like to get
task1 fail1/fail2 replace1
.....................replace2
.....................replace3
But I'm getting
task1 fail1/fail2 replace1

.....................replace2

.....................replace3
 

Users who are viewing this thread

Top Bottom