Alex Alex - 5 months ago 14
Vb.net Question

Write to new lines of sequential file in Visual Basic 2010

Okay, so, I'm supposed to be able to access a file that already has values in it and write values to that file (namely questions).

This is my code so far:

Private Sub AddQuestion(sender As System.Object, e As System.EventArgs) Handles btnQuestions.Click
Dim pass, response, question As String
pass = "sample01"
response = InputBox("Please enter the administrator password.", "Password")
If response = pass Then
FileOpen(1, "W:\Visual Studio 2010\Projects\Culminating\assets\questions.txt", OpenMode.Output)
Do
question = InputBox("Enter new question.", "New Question")
If question = String.Empty Then
Exit Do
End If
Write(1, question)
WriteLine(1)
Loop
FileClose(1)
Else : MsgBox("Incorrect password. Please enter again.", MsgBoxStyle.Critical, "Incorrect Password")
End If
End Sub


This adds the questions to my file, however, if I exit out of the input box and try again, it will overwrite the old questions with the new ones.

Answer
FileOpen(1, "W:\Visual Studio 2010\Projects\Culminating\assets\questions.txt", OpenMode.Output)

OpenMode.Output opens the file for overwriting the contents. You want OpenMode.Append.

Comments