Solved

Merging 2 random files

Posted on 1998-10-08
4
174 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

803 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