Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field Merge Solution

Status
Not open for further replies.

merciless32

Technical User
Mar 26, 2002
1
US
If you have a table with data that looks like this:

ID Data
A111 D1
A111 D2
A111 D3

But you want to combine the data to show:

ID Data
A111 D1|D2|D3...

Here is the script! This is worth it's weight in gold!!! First, create an empty copy of the table that you want to convert.

Here's the script with notes. Just copy and paste it and make your changes!
---------

'Make sure to check your table's field variables for Zero length rules, primary keys, data type, etc. This will cause a lot of headaches otherwise.

Function Update()
On Error GoTo ErrorHandler
'Set database, recordsets, and strings. You can use these names or modify them so that you will better understand them. Just make them relevant to what you are doing.
Dim dbs As Database
Dim rsOrigin, rsDestination As Recordset
Dim strFinalData, strData, strSelect As String
Dim strID, strFinalID As String
Dim bSuccess As Boolean

'Sets tables and variables. Create 2 tables, one for the original data, (Origin_Table) and one for the combined data, (Destination_Table). Change these table to reflect yours.
Set dbs = CurrentDb
strSelect = "SELECT * FROM Origin_Table ORDER BY ID;"
Set rsOrigin = dbs.OpenRecordset(strSelect)
Set rsDestination = CurrentDb.OpenRecordset("Destination_Table")

'In my orignal data table there are 2 fields, "ID", and "Data".
strID = rsOrigin.Fields("ID")
strData = rsOrigin.Fields("Data")
strFinalData = rsOrigin.Fields("Data")

If Not rsOrigin.EOF() Then
rsOrigin.MoveNext
End If

'Conversion Actions
Do While Not rsOrigin.EOF()

If strObserver = rsOrigin.Fields("ID") Then

'This is where the field merge takes place. Just replace the "|" with whatever character you want to display
strData = rsOrigin.Fields("Data")
strFinalID = rsOrigin.Fields("ID")
strFinalData = strFinalData & "|" & strData

Else
'This adds records to Destination Table. There are two fields in this table called "User_ID" and "Questions".
rsDestination.AddNew
rsDestination.Fields("User_ID") = strFinalID
rsDestination.Fields("Questions") = strFinalData
rsDestination.Update
strID = rsOrigin.Fields("ID")
strData = rsOrigin.Fields("Data")
strFinalID = rsOrigin.Fields("ID")
strFinalData = rsOrigin.Fields("Data")
End If

rsOrigin.MoveNext

Loop

bSuccess = True
'Final actions and messages. Use whatever message you want in place of this one.
If bSuccess = True Then
MsgBox "All Records Updated Sucessfully!"
End If
Exit Function

'Error message
ErrorHandler:
MsgBox "Unhandled error #" + CStr(Err.Number) + "-" + Err.Description
End Function

-------------
Enjoy!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top