2017-10-17 88 views
0

我正在爲我自己的目的製作一個數字生成器。 我已經做它與使用這些功能: - 保存生成的數字如何從保存的文件中獲取保存的文本

我想要什麼功能: - 當我關閉它加載從記事本的最後生成的數字。

這裏是代碼:

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 
+1

歡迎使用計算器。您應該閱讀https://stackoverflow.com/help/how-to-ask。 – jac

+0

哪部分你有問題?什麼工作,特別是什麼不工作?它的行爲如何,你期望的是什麼? – Hrqls

+0

有什麼問題,請解釋一下。 – vbdevelp

回答

0

獨立的使用二進制模式...

打開文件爲#1 打印#1輸出, 「一些文本」 接近#1

打開文件輸入作爲#1 線路輸入#1,MYVARIABLE 靠近#1

msgbox myvariable

流程與您展示的相同,那麼問題是什麼?