Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Creating a text file with a different name

kickercompkidkickercompkid Member Posts: 3
Hi, super new to this whole VB thing and I'm trying to figure out how to create a new text file each time my program starts that would go in numeric order. Like the first time you run it it creates 1.txt, the next time 2.txt and so on. The hard part is that in the program there are a bunch of text boxes that need to write to that file each time, but each time it runs it needs to write to the new named file it has just created. ANY help whatsoever would be great!! I'm using VB 6, if that makes any difference.

PLEASE HELP!!!

Comments

  • FirecoreFirecore Member Posts: 18
    : Hi, super new to this whole VB thing and I'm trying to figure out
    : how to create a new text file each time my program starts that would
    : go in numeric order. Like the first time you run it it creates
    : 1.txt, the next time 2.txt and so on. The hard part is that in the
    : program there are a bunch of text boxes that need to write to that
    : file each time, but each time it runs it needs to write to the new
    : named file it has just created. ANY help whatsoever would be
    : great!! I'm using VB 6, if that makes any difference.
    :
    : PLEASE HELP!!!

    First you would have to make a .txt which has a record of the number of txt files you have made. Using that you would then make the others.

    Something like

    Open "c:
    ecord.txt" For Output As #1 ' This would be the first bit of code
    Print #1, "1"
    Close #1

    ' Then, the next time, you would put

    Open "c:
    ecord.txt" For Input As #1
    Input #1, Val(data)
    Close #1

    Open "c:
    ecord.txt" For Output As #1
    data = data + 1
    Print #1, data
    Close #1

    You could combine all that, but I did it this way so you understand what I mean.
  • kickercompkidkickercompkid Member Posts: 3
    : : Hi, super new to this whole VB thing and I'm trying to figure out
    : : how to create a new text file each time my program starts that would
    : : go in numeric order. Like the first time you run it it creates
    : : 1.txt, the next time 2.txt and so on. The hard part is that in the
    : : program there are a bunch of text boxes that need to write to that
    : : file each time, but each time it runs it needs to write to the new
    : : named file it has just created. ANY help whatsoever would be
    : : great!! I'm using VB 6, if that makes any difference.
    : :
    : : PLEASE HELP!!!
    :
    : First you would have to make a .txt which has a record of the number
    : of txt files you have made. Using that you would then make the
    : others.
    :
    : Something like
    :
    : Open "c:
    ecord.txt" For Output As #1 ' This would be the first bit
    : of code
    : Print #1, "1"
    : Close #1
    :
    : ' Then, the next time, you would put
    :
    : Open "c:
    ecord.txt" For Input As #1
    : Input #1, Val(data)
    : Close #1
    :
    : Open "c:
    ecord.txt" For Output As #1
    : data = data + 1
    : Print #1, data
    : Close #1
    :
    : You could combine all that, but I did it this way so you understand
    : what I mean.
    :

    Ok that all makes sense to me, but how do I go about actually creating a new text file each time the program opens with a different name?
  • kickercompkidkickercompkid Member Posts: 3
    OK so maybe I didn't get any of that. Could you please explain it further, the concept makes sense but for a "special" person such as myself I'm having a hard time putting it into action.

    Thanks!
  • Phat NatPhat Nat Member Posts: 757
    You could also search the directory for any .TXT files and either record the highest number and add one.

    [code]
    Sub Form_Click ()
    GetLastFile "C:Texts*.Txt" ' Call GetLastFile
    End Sub

    Sub GetLastFile (SearchSpecs)

    Dim Count, HighestNum, I, FileName ' Declare variables.
    FileName = Dir(SearchSpecs, 0) ' Get first filename.
    HighestNum = 0

    Do While FileName <> ""
    Num = Left(FileName, InStr(FileName, ".") - 1)
    Count = Count + 1 ' Increment counter.
    If Num > HighestNum Then HighestNum = Num

    FileName = Dir ' Get another filename.
    Loop
    MsgBox "The Highest Filenumber is " & HighestNum & Chr(10) & "The Next Highest Available Number is " & HighestNum + 1
    End Sub
    [/code]

    Just make the SUB return the value of highestnum or highestnum + 1 and create that file.
Sign In or Register to comment.