Solved

Merging 2 random files

Posted on 1998-10-08
4
172 Views
Last Modified: 2010-04-30
I know I asked this question before but I need some clarity with my code.
Below is my code, which dosn't work.
I basically am taking the firstfile and copying it as the finalfile, then I open the secondfile and try toget each record and put it at the end of the newly created finalfile.
But for some reason it is not working.
What am I doing wrong?
Dim finalcurrentrecord As Long
Dim finallastrecord As Long

lblmerge.Visible = True
firstfile$ = txtfirst.Text
secondfile$ = txtsecond.Text
finalfile$ = txtfinal.Text
Close
FileCopy firstfile$, finalfile$
recordlen = Len(primequote)
    filenum = 1
    Open finalfile$ For Random As filenum Len = recordlen
    finallastrecord = FileLen(finalfile$) / recordlen
    finalcurrentrecord = finallastrecord
   
recordlen = Len(primequote)
    filenum = 2
    Open secondfile$ For Random As filenum Len = recordlen
    currentrecord = 1
    lastrecord = FileLen(secondfile$) / recordlen
    If lastrecord = 0 Then
    lastrecord = 1
    End If
    currentrecord = 1
For recnum = 1 To lastrecord
Get #2, currentrecord, primequote
Put #1, finalcurrentrecord, primequote
recnum = recnum + 1
currentrecord = currentrecord + 1
finalcurrentrecord = finalcurrentrecord + 1
Next
END OF MY CODE

0
Comment
Question by:dtucker
  • 2
4 Comments
 
LVL 3

Expert Comment

by:vmano
ID: 1438891
change the line finalcurrentrecord = finallastrecord to this:
finalcurrentrecord = finallastrecord + 1 and try. rest all looks fine for me.
0
 

Author Comment

by:dtucker
ID: 1438892
Ok I made the change and all but the first record in the secondfile gets merged into the finalfile.
Any thoughts...

0
 
LVL 18

Accepted Solution

by:
deighton earned 0 total points
ID: 1438893

Heres a function for you to try - Good Luck

Public Sub MergeFiles(ByVal sFile1 As String, ByVal sFile2 As String, ByVal sFile3 As String, ByVal iLen As Long)

'APPEND FILE1 TO FILE 2 TO GIVE FILE3 - RECORDLENGTH = ILEN

    Dim iFile2 As Long, ifile3 As Long
    Dim iLength1 As Long
    Dim iStart As Long
   
    Dim sBuf As String

    FileCopy sFile1, sFile3
   
    ifile3 = FreeFile
   
    Open sFile3 For Binary As ifile3
   
    iLength1 = LOF(ifile3)
   
    iStart = iLength1 + 1
   
    iFile2 = FreeFile
    Open sFile2 For Binary As iFile2
   
    For c = 1 To LOF(iFile2) \ iLen
           
        sBuf = String(iLen, " ")
                   
        Get #iFile2, , sBuf
       
        Put #ifile3, iStart, sBuf
       
        iStart = iStart + iLen
       
    Next
   
    Close #iFile2, #ifile3
   
       
End Sub


0
 
LVL 3

Expert Comment

by:vmano
ID: 1438894
try this: Get #2, , primequote in the loop and comment out all the code for currentrecord. but i am not sure why it's happening.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

17 Experts available now in Live!

Get 1:1 Help Now