Solved

combining string

Posted on 2002-04-04
6
167 Views
Last Modified: 2012-05-04
hi everybody,

i have a problem here.. about vb and database (MS Access)

okay, in my database, i have this kind of data
MyTable
id         PatternA           PatternB
1            a/1                b/1
1            b/2                b/2
1            dd/3              kl/3
1           dkaf/4              a/4
1            kjb/5              abcd/5
2           bb/1                 d/1
2            a/2                 def/2
2            bba/3                 c/3
...            ...               ....

okay, now i would like to put this thing in another table with another kind of arranging them, for example;

NewTable
id          PatA                          PatB
1       a/1b/2dd/3dkaf/4kjb/5    b/1b/2kl/3a/4abcd/5
2       bb/1a/2bba/3              d/1def/2c/3
..         ...                         ...

what i mean is, with the same id, all string in PatternA will be combine and put in PatA same as PatternB
i hope i explain it clearly.

got any idea?..

help in need;
FaRZ
0
Comment
Question by:FaRZ
6 Comments
 
LVL 6

Accepted Solution

by:
sharmon earned 50 total points
ID: 6920139
Here's one way...

Loop through your recordset and build the new rows to enter.

Assuming using ADO and you have already setup your connection, etc...

I wrote on the fly without anykind of test, hopefully you can make it work as you need...

Regards,
Shannon

rs.Open "SELECT ID, PatternA, PatternB FROM MyTable ORDER BY ID ASC"

Dim lngID As Long
Dim strPatA As String
Dim strPatB As String
Dim strSQL As String

lngID = -1

Do While Not rs.EOF

    If rs("ID").Value <> lngID Then

        If lngID <> -1 Then
           
            strSQL = "INSERT INTO MyNewTable (ID, PatA, PatB) VALUES ("
            strSQL = strSQL & lngID & ", '" & Replace(strPatA, "'", "''")
            strSQL = strSQL & "', '" & Replace(strPatB, "'", "''") & "')"
            cn.Execute strSQL
       
        End If

        strPatA = ""
        strPatB = ""
        lngID = rs("ID").Value
   
    End If
   
    strPatA = strPatA & rs("PatternA").Value
    strPatB = strPatB & rs("PatternB").Value

    rs.MoveNext
 
Loop

rs.Close
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6920149
For research: Just wonder if can retrieve the records wanted with a single SQL..

<listening..>
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6920171
ryancys,

I was thinking the same thing, but nothing popped in my head quickly since the layout he provided had no real way to assure the data in the original recordset was in an order that would make it maybe possible.  Although I'd like to see it that way...maybe someone else has some more ideas.

Shannon
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6921534
Please maintain your open questions:

Questions Asked 12
Last 10 Grades Given A A A A C C B  
Question Grading Record 7 Answers Graded / 7 Answers Received

Thanks,
Anthony
0
 

Author Comment

by:FaRZ
ID: 6922510
thanks sharmon..

it works and helps me a lottt..

FaRZ
0
 
LVL 6

Expert Comment

by:sharmon
ID: 6923057
Your welcome, glad I could help.

Shannon
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now