Problem: VBA Code in MS Access 2007 (or 2010) creates recordset.
The "Comment" field exceeds 255 characters.
The CopyFromRecordset method does not work for Memo fields (i.e. larger than 255 characters).
Objective: User CustomerID in a custom function and concat all the memo fields for a CustomerID into one Excel field. (note: Wells ID is actually Customer ID)
What I need help with is: Should I use the copyfromrecordset and populate the Excel worksheet, then loop through each cell to retrieve the long comment string?
The last column is the CustomerID, it needs to be replaced with the long comment string. On the Excel sheet, the column name is known, the start row and the end row. Any code examples would be helpful.
Line 600 CopyFromRecordset puts the correct fields into Excel, with the last field being the CustomerID. The last field is the Funcition parameter that can run the function and return the very long string.
The ConcatComments takes the WellID (a.k.a. CustomerID) and returns a long string of comments with dates and delimiters into one string variable
The "Comment" field exceeds 255 characters.
The CopyFromRecordset method does not work for Memo fields (i.e. larger than 255 characters).
Objective: User CustomerID in a custom function and concat all the memo fields for a CustomerID into one Excel field. (note: Wells ID is actually Customer ID)
What I need help with is: Should I use the copyfromrecordset and populate the Excel worksheet, then loop through each cell to retrieve the long comment string?
The last column is the CustomerID, it needs to be replaced with the long comment string. On the Excel sheet, the column name is known, the start row and the end row. Any code examples would be helpful.
Line 600 CopyFromRecordset puts the correct fields into Excel, with the last field being the CustomerID. The last field is the Funcition parameter that can run the function and return the very long string.
Code:
strSQLComments = "SELECT Wells_Areas.Area, Wells.Well_Name AS [Well Name], Last(Wells_Status1.Status1) AS [Well Status], Last(Comments.Date) AS LastOfDate1, Last(Comments.[User ID]) AS [LastOfUser ID], Last(Wells.ID_Wells) AS LastOfID_Wells "
strSQLComments = strSQLComments & " FROM ((States INNER JOIN Wells_Areas ON States.ID_State = Wells_Areas.ID_State) INNER JOIN (Wells_Status1 INNER JOIN Wells ON Wells_Status1.ID_WellStatus1 = Wells.ID_WellsStatus1) ON Wells_Areas.ID_Area = Wells.ID_Area) INNER JOIN Comments ON Wells.ID_Wells = Comments.ID_Wells "
strSQLComments = strSQLComments & " GROUP BY Wells_Areas.Area, Wells.Well_Name, Well_Name_Sorted([Well_Name]), Wells.ID_Area "
strSQLComments = strSQLComments & " HAVING (((Last(Wells.Activity))='A') AND ((Wells.ID_Area) " & ID_Area & ")) "
strSQLComments = strSQLComments & " ORDER BY Wells_Areas.Area, Well_Name_Sorted([Well_Name]), Last(Comments.Date) DESC; "
Debug.Print "strSQLComments = " & strSQLComments
540 Call LogUsage("Comments Report", "Open Dynaset", "Line") ' Log success
550 ObjXL.Visible = False
'Debug.Print " sql string = " & strSQLComments ' for test purposes
560 Set rsDataSundries = CurrentDb.OpenRecordset(strSQLComments, dbOpenSnapshot, dbReadOnly) ' suggestion was this could be faster but it is not
570 intRowPos = 6 'Sets starting Row to 6 for data returned to Excel
590 ObjXL.DisplayAlerts = False ' Turn off Display Alerts
600 ObjXL.Worksheets(intWorksheetNum).Cells(intRowPos, 1).CopyFromRecordset rsDataSundries
620 intMaxRecordCount = rsDataSundries.RecordCount - 1 ' - use for max rows returned in formatting later
The ConcatComments takes the WellID (a.k.a. CustomerID) and returns a long string of comments with dates and delimiters into one string variable
Code:
Function ConcatComments(intPrimaryKeyID As Integer) As String ' Wells Primary ID passed in
Dim dbCommentsCodes As DAO.Database
Dim rsCommentsCodes As DAO.Recordset
Dim CommentString As String ' Output String
Dim strCriteria As String, strsCommentsCodesQL As String
' Change SQL statement to accept intPrimaryKeyID as shown below
10 strsCommentsCodesQL = "SELECT CStr([Date]) & ' : ' & [Comments] & ' |' AS Comment " & _
"FROM Wells INNER JOIN Comments ON Wells.ID_Wells = Comments.ID_Wells " & _
"WHERE (((Wells.ID_Wells) = " & intPrimaryKeyID & ")) " & _
"ORDER BY Comments.Date DESC , Wells.ID_Wells;"
20 On Error GoTo Err_ConcatComments
30 CommentString = ""
40 Set dbCommentsCodes = CurrentDb
50 Set rsCommentsCodes = dbCommentsCodes.OpenRecordset(strsCommentsCodesQL, dbOpenSnapshot) ' snapshots run faster
60 With rsCommentsCodes
70 If .RecordCount <> 0 Then
80 Do While Not rsCommentsCodes.EOF
90 CommentString = CommentString & rsCommentsCodes("Comment") & " " ' <-- Field to concat and delimiter
100 .MoveNext
110 Loop
120 End If
130 End With
Exit_ConcatComments:
'ConcatComments = Null
150 If Not rsCommentsCodes Is Nothing Then
160 rsCommentsCodes.Close
170 Set rsCommentsCodes = Nothing
180 End If
190 Set dbCommentsCodes = Nothing
200 Exit Function
Err_ConcatComments:
210 Resume Exit_ConcatComments
End Function