3 Replies - 180 Views - Last Post: 17 October 2017 - 11:17 PM Rate Topic: -----

#1 ixes3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 17

How to get saved texts from saved file

Posted 16 October 2017 - 11:35 PM

I am making a number generator for my own purpose i already made it to work these features
- Saves the generated number

What features i want :

- When i close loads the last generated number from the notepad.

Please help me

here is the code:

Private Const FilePath As String = "C:\Users\sto0007404\Documents\Numbers.txt"
Private CurrentNumber As Long

Private Sub Command1_Click()
CurrentNumber = CurrentNumber + 1
txtRefNo.Text = "EM" & Format(CurrentNumber, String(4, "0"))
End Sub

Private Sub Form_Load()
    Dim TextFileData As String, MyArray() As String, i As Long


    ' Open file as binary
    Open "FilePath" For Binary As #1

    ' Read entire file's data in one go
    TextFileData = Space$(LOF(1))
    Get #1, , TextFileData

    ' Close File
    Close #1

    ' Split the data in separate lines
    MyArray() = Split(TextFileData, vbCrLf)

    For i = 0 To UBound(MyArray())
        ' Set CurrentNumber equal to the current max
        CurrentNumber = Val(Mid$(MyArray(i), 2))
    Next
    End Sub
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim i As Long

    ' delete the old file
    If Not LenB(Dir(FilePath)) = 0 Then Kill FilePath

    'open the file for writing
    Open FilePath For Output As #1

    For i = 1 To CurrentNumber
        Write #1, "EM" & Format(i, String(4, "0"))
    Next

    'close the file (if you dont do this, you wont be able to open it again!)
    Close #1
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: How to get saved texts from saved file

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: How to get saved texts from saved file

Posted 17 October 2017 - 06:06 AM

Quote

- When i close loads the last generated number from the notepad.

I don't understand this description. Do you mean "from the file" rather than "from the notepad"?

Do you mean "when I close the file"? In which case, you would read the value before you close the file.



If you want to read the last value - the last line - from the file then do so while reading the file.
But if you are writing values to the file, just stored the last value that you write, before or just after you write it.

Clarifying your question would likely encourage a clearer response.
Was This Post Helpful? 0
  • +
  • -

#3 ixes3377  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 17

Re: How to get saved texts from saved file

Posted 17 October 2017 - 08:54 PM

Dear Sir,

I mean
I can already run the programm but and can generate code, the generated codes are saved to Numbers.txt file and after i close the programm and reopen it again it doesnt start from the last number i generated ( last number which is on Numbers.txt file )
Ex: I generated till EM0030 but after i close the programm it starts over with EM0001 ( even if the Numbers.txt file show that its been generated till EM0030 )

This post has been edited by andrewsw: 17 October 2017 - 11:13 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: How to get saved texts from saved file

Posted 17 October 2017 - 11:17 PM

So you need to break down the steps you need:

When your program starts, check whether the file exists;
If it doesn't then the first number will be EM0001;
If it does, then open it and find the last line;
store this value and close the file 'til you need it.

Your code shows that you already know how to do most of this.

If, however, you will only be writing to the file then, before you start writing, discover what the last value was. (This seems unlikely though, as there wouldn't be much utility for the program.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1