Solved

Help with retreiving multiple countries to copy to another table

Posted on 2011-02-14
5
255 Views
Last Modified: 2012-05-11
Hello,

I'm using the code below to retreive countries from COUNTRY column in AOP5 and copy it to Column CA2 in AOP6A, problem is I need to retreive multiple countries in the following format: BEL,CAN,FRA,USA and copy it to CA2 in AOP6A. Is there a way to retreive multiple countries for matching IDs between both table before I copy them to CA2?
For example to code below only retreives one country:
Set CA2='" & dTable.Rows(i).Item(1) & "'
I need to set CA2 = BEL,CAN,FRA etc..

Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
        Dim dbcon As New OleDbConnection(ConnectionString)
        dbcon.Open()
        Dim dbcmd As New OleDbCommand
        dbcmd.Connection = dbcon
        dbcmd.CommandText = "Select SN, COUNTRY From AOP5"
        Dim dTable As New DataTable
        Dim dbadp As New OleDbDataAdapter(dbcmd)
        dbadp.Fill(dTable)

        For i As Integer = 0 To dTable.Rows.Count - 1
              dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(1) & "' Where SN='" & dTable.Rows(i).Item(0) & "'"
            dbcmd.ExecuteNonQuery()
        Next
        dTable.Dispose()
        dbcmd.Dispose()
        dbcon.Close()
        dbcon.Dispose()

AOP5 Table
SN        COUNTRY
10411    BEL
10411    CAN
10411    FRA

AOP6A Should include (AOP6A AND AOP5 are linked by the SN Field)
SN         CA2
10411     BEL,CAN,FRA

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 34892838
Hi,

You can try to build your Update string for each SN, and after that run your UPDATE statement, try the attached code, it should works, and will give you an idea of how to do it.

Hope it helps
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
Dim dbcon As New OleDbConnection(ConnectionString)

dbcon.Open()

Dim dbcmd As New OleDbCommand
dbcmd.Connection = dbcon

'TO GET THE SN LIST
dbcmd.CommandText = "Select Distinct SN From AOP5"
Dim dSNTable as New DataTable
Dim dbadpSN As New OleDbDataAdapter(dbcmd)
dbadp.Fill(dSNTable)

Dim strUpdate As String 

'FOR EACH SN CREATE AN STRING WITH THE COUNTRIES
For y As Integer = 0 To dSNTable.Rows.Count -1

	'GET THE COUNTRIES FOR THE SN
	Dim dTable As New DataTable
	dbcmd.CommandText = "Select COUNTRY From AOP5 WHERE SN = " & dSNTable.Rows(y).Item(0)
	Dim dbadp As New OleDbDataAdapter(dbcmd)
	dbadp.Fill(dTable)
		
	strUpdate = ""

	'CREATE THE STRING WITH ALL THE COUNTRIES FOR THE SN LIKE BEL,CAN,FRA ETC.
	For i As Integer = 0 To dTable.Rows.Count - 1
		strUpdate &= dTable.Rows(i).Item(0) & ","
	Next

	'REMOVE THE LAST COMMA IF THERE WAS COUNTRIES FOR THE SN
	if strUpdate.Length > 0 Then
		strUpdate = strUpdate.Substring(0, strUpdate.Length - 1)
	end if

	'UPDATE AOP6A TABLE
	dbcmd.CommandText = "Update AOP6A Set CA2='" & strUpdate & "' Where SN='" & dSNTable.Rows(y).Item(0) & "'"
	dbcmd.ExecuteNonQuery()

Next

dTable.Dispose()
dbcmd.Dispose()
dbcon.Close()
dbcon.Dispose()

Open in new window

0
 

Author Comment

by:vcharles
ID: 34944070
Hello,

I'm sorry for the late reply, it works but I'm getting duplicates in the same string/row. For example: BEL, FRA.FRA,USA,DNK,DNK. Is there a way to avoid duplicate countries in the string?

Thankis,

Victor
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34944125
Use DISTINCT for Country also.
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
Dim dbcon As New OleDbConnection(ConnectionString)

dbcon.Open()

Dim dbcmd As New OleDbCommand
dbcmd.Connection = dbcon

'TO GET THE SN LIST
dbcmd.CommandText = "Select Distinct SN From AOP5"
Dim dSNTable as New DataTable
Dim dbadpSN As New OleDbDataAdapter(dbcmd)
dbadp.Fill(dSNTable)

Dim strUpdate As String 

'FOR EACH SN CREATE AN STRING WITH THE COUNTRIES
For y As Integer = 0 To dSNTable.Rows.Count -1

	'GET THE COUNTRIES FOR THE SN
	Dim dTable As New DataTable
	dbcmd.CommandText = "Select DISTINCT COUNTRY From AOP5 WHERE SN = " & dSNTable.Rows(y).Item(0)
	Dim dbadp As New OleDbDataAdapter(dbcmd)
	dbadp.Fill(dTable)
		
	strUpdate = ""

	'CREATE THE STRING WITH ALL THE COUNTRIES FOR THE SN LIKE BEL,CAN,FRA ETC.
	For i As Integer = 0 To dTable.Rows.Count - 1
		strUpdate &= dTable.Rows(i).Item(0) & ","
	Next

	'REMOVE THE LAST COMMA IF THERE WAS COUNTRIES FOR THE SN
	if strUpdate.Length > 0 Then
		strUpdate = strUpdate.Substring(0, strUpdate.Length - 1)
	end if

	'UPDATE AOP6A TABLE
	dbcmd.CommandText = "Update AOP6A Set CA2='" & strUpdate & "' Where SN='" & dSNTable.Rows(y).Item(0) & "'"
	dbcmd.ExecuteNonQuery()

Next

dTable.Dispose()
dbcmd.Dispose()
dbcon.Close()
dbcon.Dispose()

Open in new window

0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 34944790
Hi,

Changet this line
dbcmd.CommandText = "Select COUNTRY From AOP5 WHERE SN = " & dSNTable.Rows(y).Item(0)

for this

dbcmd.CommandText = "Select DISTINCT COUNTRY From AOP5 WHERE SN = " & dSNTable.Rows(y).Item(0)

0
 

Author Closing Comment

by:vcharles
ID: 34944817
Thank You!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question