14 Replies - 13626 Views - Last Post: 17 April 2012 - 10:50 AM Rate Topic: -----

#1 tbzserge   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 26-January 11

How to make increment number when each time data is added in text file

Posted 11 April 2012 - 11:08 PM

Hi there. I was trying something on the text file using VB6 which i going to explain in detail later.
Here is my text file look like

Testing Output: 1 Date: 12/4/2012 11:22am

Employee No:2323
GRN Number:4343
Part Number:545
PO Number:4545


Testing Output: 1 Date: 12/4/2012 11:25am

Employee No:323
GRN Number:433
Part Number:344
PO Number:3434

I was trying to make the testing output increment when each time the data is saved.It sounds simple but i seemed couldn't find any way work around it.

Below is what i have done
Private Sub cmd1_Click()

Dim list(4) As String
Dim item As Integer
Dim dDate As String
Dim i As Integer

dDate = Format$(Now, "d/M/yyyy h:mmAM/PM")

Open App.Path & "\testing.txt" For Append As #1

list(1) = "Employee No:" & txt1.Text
list(2) = "GRN Number:" & txt2.Text
list(3) = "Part Number:" & txt3.Text
list(4) = "PO Number:" & txt4.Text

i = i + 1
Print #1, vbCrLf
Print #1, "Testing Output: " & i; Spc(20); "Date: " & dDate & vbCrLf

For item = 1 To 4
Print #1, list(item)
Next item

MsgBox "Save Complete", vbOKOnly, "Message!"

Close #1

End Sub



I am well aware that the way i do is wrong.I'm still new and still learn on vb6.Any help would be much appreciated.Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to make increment number when each time data is added in text file

#2 Neku   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 269
  • Joined: 21-May 09

Re: How to make increment number when each time data is added in text file

Posted 12 April 2012 - 03:28 AM

that because you open it for Appened.
when you write into a file with Appened any data you put inside the file will be written after any data it may hold.
opening file for appened can be usefull for saving logs for example since you dont want to overwrite the data in the file.
but if you do want to overwrite the data you have to open the file for output.

change this line:

Open App.Path & "\testing.txt" For Append As #1


with this one:

Open App.Path & "\testing.txt" For Output As #1

Was This Post Helpful? 0
  • +
  • -

#3 tbzserge   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 26-January 11

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 03:00 AM

Hi.Thank for the reply.

I think you have misunderstood my meaning.I do not wish to overwrite my text file,that's why i am using Appened for it.I would like to add another this

Testing Output: 1 Date: 12/4/2012 11:25am

Employee No:323
GRN Number:433
Part Number:344
PO Number:3434

below the previous data. The testing output here should be increment become 2. Same goes if i have saved another testing output data, it will increment to 3...
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 08:17 AM

Aha. After some meditation, spells, incantations, prayers, candles, shamanic dances, and a few other secret rituals, I was able to divine what it is you are talking about. You need a static variable, my son. Change Dim i as Integer to Static i as Integer and your problems will be solved. :)

You get extra credit for posting back and asking why, once you see that it works.
Was This Post Helpful? 0
  • +
  • -

#5 Neku   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 269
  • Joined: 21-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 10:09 AM

do i get extra credits if i ask why before him? :D
Was This Post Helpful? 1
  • +
  • -

#6 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 10:56 AM

Sure. :) A static variable in VB6 is a local variable in terms of scope, but a module-level variable in terms of lifetime. So, it can only be seen inside of the procedure, but holds its value between procedure calls. His problem is that his variable i gets initialized to 0 each time he presses his command button, he increments it to 1, uses it in his print statement, and then next time he presses his button the same thing happens. He wants to increment i each time he presses the button without reinitializing the variable to 0 each time as well. That's what Static variables in VB do.

Most people just declare the variable in the General Declarations area, where it will do the same thing. Trouble is, other procedures can change it too, and if you don't want that Static variables are the way to go.
Was This Post Helpful? 1
  • +
  • -

#7 tbzserge   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 26-January 11

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 11:32 AM

Umm,actually i have tested it out,but it still didn't work for me. It still give me the same testing output (which is 1 ) for the variable i.

I first thought that maybe i need to check line by line and identify the largest i value in the text file.Then,do a increment on that value.

Or perhaps maybe i'm bad at explaining my requirement,is it?

Thanks for the reply. ^^
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 12:54 PM

Post your code showing the change to line 6, please.

This post has been edited by BobRodes: 13 April 2012 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#9 Neku   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 269
  • Joined: 21-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 12:55 PM

@bob
you teached me something new :D
thanks
Was This Post Helpful? 0
  • +
  • -

#10 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 12:56 PM

You're welcome.
Was This Post Helpful? 0
  • +
  • -

#11 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: How to make increment number when each time data is added in text file

Posted 13 April 2012 - 11:26 PM

You could look at creating a user-defined type to hold the data you need to store.

Take a look at this tutorial and the section on Storing/Reading a User Defined Type.

It writes the data as a sequential file. You can then read the file contents to determine the last record number, increment it and use it when you add your new record.
Was This Post Helpful? 0
  • +
  • -

#12 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 17 April 2012 - 06:14 AM

maj, perhaps I'm missing something, but I believe the OP needs to increment a local value each time he calls a sub.
Was This Post Helpful? 0
  • +
  • -

#13 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: How to make increment number when each time data is added in text file

Posted 17 April 2012 - 09:09 AM

I thought he was writing to a file and wanted to update a number (record ID perhaps) for each time he wrote a "record" to the file, So I thought writing and reading a UDT would allow him to do that....not ideal, but I couldn't think of another way at the time for doing it in a file.

This post has been edited by maj3091: 17 April 2012 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#14 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: How to make increment number when each time data is added in text file

Posted 17 April 2012 - 10:32 AM

Oh, ok. I'm looking at this bit of code here:
i = i + 1
Print #1, vbCrLf
Print #1, "Testing Output: " & i; Spc(20); "Date: " & dDate & vbCrLf
I'm getting that the OP was trying to increment i each time through the code, and didn't understand why i kept getting reinitialized each time through the event handler. Static variable fixes this. Of course, in that mysterious black-art world of requirements gathering, your guess is as good as mine, and the OP isn't saying anything.

Guess one of us solved his problem...
Was This Post Helpful? 0
  • +
  • -

#15 maj3091   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,998
  • Joined: 26-March 09

Re: How to make increment number when each time data is added in text file

Posted 17 April 2012 - 10:50 AM

View PostBobRodes, on 17 April 2012 - 05:32 PM, said:

Guess one of us solved his problem...


Well, until we get some response, we'll never know! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1