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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JOING TWO TABLES

Status
Not open for further replies.

juggers

IS-IT--Management
Jul 11, 2000
66
SA
Hi all,

I have 2 text files they have the Field [name] in common, I a using 2 DSNs 2 open because they have different structures.
--------------
set con = new ADODB.Connection
set cn = new ADODB.Connection
Set rec = New ADODB.Recordset
Set rs = New ADODB.Recordset
rec.Open " select * from names.txt , con, adOpenDynamic
rs.Open "select * from address.txt", cn, adOpenDynamic
----------------
The idea is to select all the names that are matching in both files add to them the addresses and put them into new text file. I was thinking of joining them threw and SQL coomand they call it inner join but I do not know what is the best solution. Any Ideas?

Regards

time is the father of truth
 
You can't join across recordsets. Can you drop the files into an Access or SQL database? Otherwise, you probably need to have two loops, one on the names and an inner loop on the addresses and write out an entry to the new file when they match up.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hi thanks for your reply,

I have created two tables in access I only have used recordsets what can to do now?

Thanks

time is the father of truth
 
>I a using 2 DSNs 2 open because they have different structures

Are you sure that you need two DSNs?
 
Well yes i need 2 DSNs how can you open 2 text files with different layout with one DSN?


time is the father of truth
 
Is it possible to loop through the Names recordset (rec) and then for each identifier, set the filter accordingly on the Addresses recordset (rs)??

Just a thought ...

Patrick
 
How? By letting the ODBC text driver figure out the layout for you ...

Create the following text file in C:\temp as text1.txt
Code:
1, 5
2, 2
3, 7
4, 4
5, 8

and this one as text2.txt in the same folder
Code:
27 Jan 2004, 0.5, 1
14 Dec 2005, 0.7, 1
11 May 2000, 0.6, 5

(notice the different layouts and data types)

Add a reference to ADO, then try the following code:
Code:
[blue]Option Explicit

Public Sub Commmand1_Click()
    Dim cn As Connection
    Dim recordset1 As Recordset
    
    Set cn = New Connection
    [green]' We'll tell the driver there's no header, and allow driver to figure out fields
    ' which means they'll be called F1 thru F<n>
    [/green]cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\;Extended Properties=""text;HDR=No;FMT=Delimited"""
    
    Set recordset1 = New Recordset
    recordset1.Open "SELECT text2.f1 as ADate, text1.f2*text2.f2 as crosstablecalc from text1.txt as text1 inner join text2.txt as text2 on text1.F1=text2.F3", cn
    Do Until recordset1.EOF
        Debug.Print Format(recordset1("ADate"), "long date"), recordset1("crosstablecalc")
        recordset1.MoveNext
    Loop
End Sub
[/blue]

 
No, that's not what I meant. Maybe it's not possible but ...

Using your text files as an example I was thinking of the following.

(assuming the first field in text1.txt can link to the third field in text2.txt)

Code:
Do while not recs.eof
    rs.filter (<FIELDNAME>) = (recs.fields(1))
    recs.movenext
loop

Will this work??

Patrick
 
strongm,

Just as a quick test I used you're code for the following.

Code:
    Dim cn      As Connection
    Dim rs1     As Recordset
    Dim rs2     As Recordset
    Dim sFilter As String
    ''''''''''''''''''''''''''''''''''
    Set cn = New Connection
    ' We'll tell the driver there's no header, and allow driver to figure out fields
    ' which means they'll be called F1 thru F<n>
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\;Extended Properties=""text;HDR=No;FMT=Delimited"""
    ''''''''''''''''''''''''''''''''''
    Set rs1 = New Recordset
    Set rs2 = New Recordset
    ''''''''''''''''''''''''''''''''''
    rs1.Open "SELECT * from text1.txt", cn, adOpenStatic
    rs2.Open "SELECT * from text2.txt", cn, adOpenStatic
    ''''''''''''''''''''''''''''''''''
    Do Until rs1.EOF
        sFilter = rs2.Fields(2).Name & " = " & rs1.Fields(1).Value
        rs2.Filter = sFilter
        
        Do While Not rs2.EOF
            Debug.Print rs2.Fields(0).Value & " :: " & rs2.Fields(1).Value & " :: " & rs2.Fields(2).Value
            rs2.MoveNext
        Loop
        
        rs1.MoveNext
    Loop
    ''''''''''''''''''''''''''''''''''

Am I missing something?? (I'm having quite a few "senior moments" lately so it's possible)

Patrick
 
Don't confuse me (Patrick) with the original poster (juggers).

I was just offering an opinion, that's all.
 
Guys you are complicating things too much

I have file1.txt with the following fileds:
name, age,sex
and file2.txt with the following fields:
name, address,telephone

All what I want with any method, is to get a third text file file3.txt with the following fields:
name,age,sex,address,telephone.

Any method will I tried loops it is working but I was told that this can be done with sql commands inside the coding



time is the father of truth
 
Although I would personally prefer using a method similar to strongm, another option would be to link your text files in an access database and join them that if (if what strongm is saying is too complicated for you). Then you can use a query to create a text file if you'd like...

Not saying I would do it this way, it's just another option.
 
> I was told that this can be done with sql commands inside the coding

Correct. And that I what I illustrated (well, apart from saving it as a third file), albeit using different fields from yours
 
Here, I've had a slow afternoon, so I've put an example together that more closely matches your requirements.

Create the following text file in c:\temp as names1.txt
Code:
Mike, 25, M
Dave, 30, M
Jerry, 42, M
Amanda, 22, F
Simon, 37, M
Sarah, 34, F
Jenny, 36, M

Then create addresses.txt in c:\temp as follows:
Code:
Mike, "10 London Road, Kingston, Surrey, KK1 44J", 12345678
Jenny, "Flat 1, 6 Mill Street, Effingham, Surrey", 45678990

And then this code:
Code:
[blue]Option Explicit

Public Sub Commmand1_Click()
    Dim cn As Connection
    Dim recordset1 As Recordset
    
    Set cn = New Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\;Extended Properties=""text;HDR=No;FMT=Delimited;"""
    
    Set recordset1 = New Recordset
    
    On Error Resume Next [green]' Ignore the error caused where mydemo.txt does not already exist
        [/green]recordset1.Open "drop table mydemo.txt", cn '
    On Error GoTo 0
    [green]' Creates new table as text file and creates the relevant schema.ini which is in turn
    ' used by INSERT INTO[/green]
    recordset1.Open "CREATE TABLE mydemo.txt (Name  char(50), Age  Integer, Sex  char(1), Address char(100), Tel char(12))", cn
    
    recordset1.Open "INSERT INTO mydemo.txt (Name,Age,Sex,Address,Tel) SELECT AName.f1 , aname.f2 , aname.f3 , address.f2 , address.f3  from names1.txt as AName inner join addresses.txt as address on AName.F1=address.f1", cn

End Sub[/blue]
 
hi STRONGM

I tried your code it is given an error on the last line
"No Value Given for one or more required parameters"




time is the father of truth
 
as well as My text files already exists and they are fixed length

time is the father of truth
 
1) The error message is misleading, and appears to be a quirk of some MDAC versions: Replace all the f<n> with F<n> as below (or use .Fields(<n>) instead of the .F<n>):
Code:
recordset1.Open "INSERT INTO mydemo.txt (Name,Age,Sex,Address,Tel) SELECT AName.F1 , AName.F2 , AName.F3 , address.F2 , address.F3  from names1.txt as AName inner join addresses.txt as address on AName.F1=address.F1", cn
2) Fixed length? Fine, just make the appropriate entries in schema.ini (whose existence I hinted at in the code sample, and details of which should be available at msdn.microsoft.com). You may also then have to make modifications to the INSERT INTO command, as your columns will now have names other than the ODBC-generated F1 thru F<n>
3) I'm only giving you an outline of what you need to do, not a full code solution to your specific problem as that should be down to you ...
 
thread222-1109585 contains some related detail that you might find helpful.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top