Solved

Merging 2 random files

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

860 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