我无法在visual basic 2012中创建我的记录编号,我到目前为止的代码是覆盖以前保存的文件吗?
我的模块
Imports System.IO
Module AssModule
Structure employee
' Public Name As String
' Public ID As String
<VBFixedString(30)> Dim name As String ' vb fixed string
<VBFixedString(5)> Dim id As String ' vb fixed string
End Structure
Structure transaction
<VBFixedString(5)> Dim id As String ' vb fixed string
Dim time As Date
<VBFixedString(3)> Dim type As String ' vb fixed string
End Structure
' Public fileSize As Integer = Len(index)
' Public Staff(100) As employee ' staff as new name for eployeee structure
Public myEmployee As employee
Public r As Integer = 5
Public fileNumber As Integer = FreeFile()
Public mySizerecordnumber As Integer = Len(myEmployee)
Public recordNumber As Integer
Public index As Byte
Public Sub setRecordNumber()
Dim n As Byte = 0
FileOpen(fileNumber, "binData.dat", OpenMode.Random, OpenAccess.Read)
While Not EOF(fileNumber)
n = n + 1
FileGet(fileNumber, myEmployee, n)
End While
recordNumber = n
FileClose(fileNumber)
End Sub
End Module
添加员工按钮
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
For n = 0 To 3
FileOpen(fileNumber, "binData.dat", OpenMode.Random, OpenAccess.Write, , mySizerecordnumber)
myEmployee.id = txtID.Text
myEmployee.name = txtName.Text
recordNumber = recordNumber + 1
FilePut(fileNumber, myEmployee, recordNumber)
FileClose(fileNumber)
Next
Me.Close()
End Sub
答案 0 :(得分:1)
好的,让我们分解你的btnSave_Click
功能。
对于0到3,每次,你:打开文件,读取文件,将结果放入myEmployee,然后将其放回文件中并关闭。
您想要做的(我认为)是将新员工保存在文件末尾
您应该打开文件,移到最后,然后保存新记录:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
setRecordNumber()
FileOpen(fileNumber, "binData.dat", OpenMode.Random, OpenAccess.Write, , mySizerecordnumber)
myEmployee.id = txtID.Text
myEmployee.name = txtName.Text
FilePut(fileNumber, myEmployee, recordNumber)
FileClose(fileNumber)
Me.Close()
End Sub