Snowflake68
Registered User.
- Local time
- Today, 04:25
- Joined
- May 28, 2014
- Messages
- 464
I am using on of Allen Browne's functions (see below) to concatenate related records into a string. However I am occasionally and randomly getting an error 'Cannot open anymore databases' and the title in the error message suggests that it is this function.
One suggestion is that the function is in a continuous loop which would explain why that I am unable to exit out of the error and keep getting the same message over and over again and then the only way I can exit out is to use the Task Manager to end the task and close Access.
I am using a query to filter the records for a specific quote reference called 'qry_Concatenate_SPFBGetValues'
and then this one queries the above query to concatenate the related values. this all seems to work fine.
I then have a text box on a form that does a dlookup to that query
All of the above works but just occasionally I am getting this error and I cant replicate the error as it is random and I don't know what is triggering it.
Hope someone can help.
thanks for looking
One suggestion is that the function is in a continuous loop which would explain why that I am unable to exit out of the error and keep getting the same message over and over again and then the only way I can exit out is to use the Task Manager to end the task and close Access.
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.
strSQL = "SELECT " & strField & " FROM " & strTable
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
strOut = strOut & rs(0) & strSeparator
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
I am using a query to filter the records for a specific quote reference called 'qry_Concatenate_SPFBGetValues'
Code:
SELECT tblMainProductDetail.Reference, tblMainProductDetail.QRef, tblMainProductDetail.SpecialPrintFinishBACK.Value AS SPFBValues, tbl_SpecialPrintFinish.SpecialPrintFinish AS SPFB
FROM tblMainProductDetail INNER JOIN tbl_SpecialPrintFinish ON tblMainProductDetail.SpecialPrintFinishBACK.Value = tbl_SpecialPrintFinish.CodeBACK;
and then this one queries the above query to concatenate the related values. this all seems to work fine.
Code:
SELECT [qry_Concatenate_SPFBGetValues].[QRef] AS Ref, ConcatRelated("[SPFB]","qry_Concatenate_SPFBGetValues","[QRef] = """ & [QRef] & """") AS SpecialPrintFinish
FROM qry_Concatenate_SPFBGetValues
GROUP BY [qry_Concatenate_SPFBGetValues].[QRef], ConcatRelated("[SPFB]","qry_Concatenate_SPFBGetValues","[QRef] = """ & [QRef] & """"), ConcatRelated("[SPFBValues]","qry_Concatenate_SPFBGetValues","[QRef] = """ & [QRef] & """")
HAVING ((([qry_Concatenate_SPFBGetValues].[QRef])=[Forms]![frmMain].[txtCurrentRecord]));
I then have a text box on a form that does a dlookup to that query
Code:
=DLookUp("[SpecialPrintFinish]","qry_Concatenate_SPFF")
All of the above works but just occasionally I am getting this error and I cant replicate the error as it is random and I don't know what is triggering it.
Hope someone can help.
thanks for looking