Victor Charles
asked on
How to sort data in a string?
Hello,
I have an ACCESS table with duplicate data in a string, for example:
ROW1: USA,BEL,N;D,CZE,FRA
ROW2: GBR,CZE,BEL,USA,NOR
ROW3: BEL,FRA,USA,DNK,CAN
How do you loop through the rows and sort the countries?
Thanks,
Victor
I have an ACCESS table with duplicate data in a string, for example:
ROW1: USA,BEL,N;D,CZE,FRA
ROW2: GBR,CZE,BEL,USA,NOR
ROW3: BEL,FRA,USA,DNK,CAN
How do you loop through the rows and sort the countries?
Thanks,
Victor
ASKER
Hello,
I found the example below from the link, but unfortunately I can't figure out how to integrate it with my existing project.
Further below is the code that I'm using to delete duplicate data in the row, can you help me include the example as part of my code?
For lLoop = 0 To UBound(MyArray)
For lLoop2 = lLoop To UBound(MyArray)
If UCase(MyArray(lLoop2)) < UCase(MyArray(lLoop)) Then
str1 = MyArray(lLoop)
str2 = MyArray(lLoop2)
MyArray(lLoop) = str2
MyArray(lLoop2) = str1
End If
Next lLoop2
Next lLoop
Existing Code:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=|DataDirectory|\aop t2002org.m db;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
Dim objConnection As New OleDb.OleDbConnection(Conn ectionStri ng)
objConnection.Open()
'Dim da As New OleDb.OleDbDataAdapter("Se lect NIIN, code3 from IDENTIFICATION_DATA", objConnection)
Dim da As New OleDb.OleDbDataAdapter("Se lect FIF, COUNTRYFUZEALLA from AOP5", objConnection)
Dim dt1 As New DataTable
da.Fill(dt1)
da.Dispose()
da = Nothing
'da = New OleDb.OleDbDataAdapter("Se lect NIIN, RN from REFERENCE_NUMBER_DATA", objConnection)
da = New OleDb.OleDbDataAdapter("Se lect FIF, COUNTRY from AOP5A", objConnection)
Dim dt2 As New DataTable
da.Fill(dt2)
Dim dv2 As DataView
dv2 = dt2.DefaultView
Dim cmd As OleDbCommand
Dim SQLStr As String
For Each dr As DataRow In dt1.Rows
'.RowFilter = "NIIN='" & dr.Item("NIIN") & "'"
dv2.RowFilter = "FIF='" & dr.Item("FIF") & "'"
If dv2.Count > 0 Then
SQLStr = "Update AOP5 Set COUNTRYFUZEALLA='"
' For Each drv2 As DataRowView In dv2
'SQLStr &= drv2.Item("COUNTRY") & ","
'Next
For Each drv2 As DataRowView In dv2
If SQLStr.Contains(drv2.Item( "COUNTRY") ) = False Then
SQLStr &= drv2.Item("COUNTRY") & ","
End If
Next
'strip off trailing comma
SQLStr = SQLStr.Substring(0, SQLStr.Length - 1)
'add closing apostrophe
SQLStr &= "'"
'add WHERE clause
SQLStr &= " Where FIF='" & dr.Item("FIF") & "'"
' MsgBox(SQLStr)
cmd = New OleDbCommand(SQLStr, objConnection)
'MsgBox(SQLStr)
cmd.ExecuteNonQuery()
cmd.Dispose()
cmd = Nothing
End If
Next
I found the example below from the link, but unfortunately I can't figure out how to integrate it with my existing project.
Further below is the code that I'm using to delete duplicate data in the row, can you help me include the example as part of my code?
For lLoop = 0 To UBound(MyArray)
For lLoop2 = lLoop To UBound(MyArray)
If UCase(MyArray(lLoop2)) < UCase(MyArray(lLoop)) Then
str1 = MyArray(lLoop)
str2 = MyArray(lLoop2)
MyArray(lLoop) = str2
MyArray(lLoop2) = str1
End If
Next lLoop2
Next lLoop
Existing Code:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OL
Dim objConnection As New OleDb.OleDbConnection(Conn
objConnection.Open()
'Dim da As New OleDb.OleDbDataAdapter("Se
Dim da As New OleDb.OleDbDataAdapter("Se
Dim dt1 As New DataTable
da.Fill(dt1)
da.Dispose()
da = Nothing
'da = New OleDb.OleDbDataAdapter("Se
da = New OleDb.OleDbDataAdapter("Se
Dim dt2 As New DataTable
da.Fill(dt2)
Dim dv2 As DataView
dv2 = dt2.DefaultView
Dim cmd As OleDbCommand
Dim SQLStr As String
For Each dr As DataRow In dt1.Rows
'.RowFilter = "NIIN='" & dr.Item("NIIN") & "'"
dv2.RowFilter = "FIF='" & dr.Item("FIF") & "'"
If dv2.Count > 0 Then
SQLStr = "Update AOP5 Set COUNTRYFUZEALLA='"
' For Each drv2 As DataRowView In dv2
'SQLStr &= drv2.Item("COUNTRY") & ","
'Next
For Each drv2 As DataRowView In dv2
If SQLStr.Contains(drv2.Item(
SQLStr &= drv2.Item("COUNTRY") & ","
End If
Next
'strip off trailing comma
SQLStr = SQLStr.Substring(0, SQLStr.Length - 1)
'add closing apostrophe
SQLStr &= "'"
'add WHERE clause
SQLStr &= " Where FIF='" & dr.Item("FIF") & "'"
' MsgBox(SQLStr)
cmd = New OleDbCommand(SQLStr, objConnection)
'MsgBox(SQLStr)
cmd.ExecuteNonQuery()
cmd.Dispose()
cmd = Nothing
End If
Next
do you need to sort the data in each row or sort the rows of a single column?
for instance, if your row is:
BEL,FRA,USA,DNK,CAN
after sorting:
BEL,CAN,DNK,FRA,USA
is that what u mean?
for instance, if your row is:
BEL,FRA,USA,DNK,CAN
after sorting:
BEL,CAN,DNK,FRA,USA
is that what u mean?
can we assume data is splitted by comma separator?
ASKER
Yes after sorting I would like to see:
BEL,CAN,DNK,FRA,USA
The countries will always be separate by a " , ".
Thanks.
BEL,CAN,DNK,FRA,USA
The countries will always be separate by a " , ".
Thanks.
which line in your code contain the string of countries which need to be sorted?
ASKER
I believe this is that part of the code
.
For Each drv2 As DataRowView In dv2
If SQLStr.Contains(drv2.Item( "COUNTRY") ) = False Then
SQLStr &= drv2.Item("COUNTRY") & ","
End If
Next
.
For Each drv2 As DataRowView In dv2
If SQLStr.Contains(drv2.Item(
SQLStr &= drv2.Item("COUNTRY") & ","
End If
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It Worked.
THANK YOU!
Victor
THANK YOU!
Victor
Glad to help :-)
http://www.ozgrid.com/VBA/sort-array.htm