ConcatRelated()函数上的字符数限制 [英] Character Limit On ConcatRelated() Function
问题描述
我一直在使用艾伦·布朗(Allen Browne)的ConcatRelated()
函数,但是正在意识到使用更大的数据集时,数据是不完整的.
I have been using Allen Browne's ConcatRelated()
function but am realizing with larger data sets the data is incomplete.
此函数是否有字符数限制?如果有,是否存在可以实现相同结果的/UDF函数?
Does this function have a character limit, and if it does - is there a different function /UDF that can achieve the same result?
这是函数:-并链接回原始线程 http://allenbrowne.com/func -concat.html
This is the function: - and link back to original thread http://allenbrowne.com/func-concat.html
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
修改
我正在使用VBA并运行Into
语句.如果先生成表然后再插入数据,效果会更好吗?
Edit
I am using VBA and running an Into
statement. Would it work better if I generated the table first and then insert the data?
当前结构
DoCmd.RunSQL "Select DISTINCT [ImportTable].DSFNum,
ConcatRelated('[Item]','[ImportTable]','[DSFNum] =' & [DSFNum]) AS Item,
ConcatRelated('[Count]','[ImportTable]','[DSFNum] =' & [DSFNum]) AS Count
INTO [OneRowTable]
FROM [ImportTable]"
推荐答案
所有相关信息都可以在ConcatRelated
函数的代码中找到:
All of the relevant information can be found in the code for the ConcatRelated
function:
- 注意:向记录集返回超过255个字符会触发此Access错误: http://allenbrowne.com/bug-16.html
- Notes: Returning more than 255 characters to a recordset triggers this Access bug: http://allenbrowne.com/bug-16.html
在该页面上可以找到,问题不在于功能,而是Access中的文本字段不能包含那么多字符的事实,可以通过使用诸如此类的内容来解决
As can be found on that page, the problem is not the function, but the fact that a text field in Access can't contain that many characters, and can be fixed by using something like
SELECT ID, MuchText FROM StructureOnly
UNION ALL
SELECT ID, ConcatRelated(something, table, where) AS MuchText FROM LotsaText;
MuchText
是备注字段
使用VBA和记录集的解决方法:
A workaround using VBA and recordsets:
Dim rsSelect As DAO.Recordset
Dim rsInsert As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rsSelect = db.OpenRecordset ("Select DISTINCT [ImportTable].DSFNum FROM [ImportTable]")
Set rsInsert = db.OpenRecordset("OneRowTable")
Do While Not rsSelect.EOF
rsInsert.AddNew
rsInsert!DSFNum = rsSelect!DSFNum
rsInsert!Item = ConcatRelated("[Item]","[ImportTable]","[DSFNum] =" & rsSelect!DSFNum)
rsInsert!Count = ConcatRelated("[Count]","[ImportTable]","[DSFNum] =" & rsSelect!DSFNum)
rsInsert.Update
rsSelect.MoveNext
Loop
这篇关于ConcatRelated()函数上的字符数限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!