2017-10-17 89 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

流程与您展示的相同,那么问题是什么?