Welcome to Dream.In.Code
Become a VB Expert!

Join 137,233 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,723 people online right now. Registration is fast and FREE... Join Now!




Seq. Access File

 
Reply to this topicStart new topic

Seq. Access File, Having trouble

km22
15 Mar, 2007 - 11:14 AM
Post #1

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Hi, hoping some one can help with this -
I am trying to determine whether the names.txt file exists and if it does, the procedure should show the MessageBox.show method to ask if they want to replace the existing file. Well, I keep getting the dialog box that the file doesnt exist - but it does exist. Also, I am not sure if I have the procedure in the correct order, the ' write name to the txt file code is the part where I am not sure where this should be placed. I need to be able to write two names to the names.txt file.

I am including a copy of my code, in case anybody can help with this.
CODE

Private Sub writeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles writeButton.Click

        Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt\"
        Dim name As String
        Dim text As String
        Dim button As DialogResult

        ' write name file to txt file
        name = nameTextBox.Text

        My.Computer.FileSystem.WriteAllText(file, _
            name, True)
        My.Computer.FileSystem.WriteAllText(file, ControlChars.NewLine & ControlChars.NewLine, True)

        ' check for names.txt file

        If My.Computer.FileSystem.FileExists(file & "names.txt") Then
            text = My.Computer.FileSystem.ReadAllText(file & "names.txt")

        Else
            MessageBox.Show("File does not exist", "Names", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        If My.Computer.FileSystem.FileExists(file & "names.txt") Then
            button = MessageBox.Show("Erase the file?", _
            "Information", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Exclamation, _
            MessageBoxDefaultButton.Button2)
            If button = DialogResult.Yes Then
                My.Computer.FileSystem.WriteAllText(file & "names.txt", _
                String.Empty, False)
            End If
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seq. Access File
15 Mar, 2007 - 11:49 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You have specified the path and the filename already in your string. Also remove the last backslash at the end of the string, it is not needed.
CODE

Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt"

So you do not need to include the filename when checking if it exists. There are a couple of places where you need to make this change.
CODE

If My.Computer.FileSystem.FileExists(file) Then


Can you explain exactly what you want to do in this procedure?

Then I will better be able to tell you if things are in the correct order.
User is online!Profile CardPM
+Quote Post

km22
RE: Seq. Access File
15 Mar, 2007 - 06:30 PM
Post #3

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Still having trouble with the Seq. access file. I apologize for starting a new thread for the same topic, but I could not reply to the original post for some reason.

Now I am getting the dialog box that asks the user if they want to replace the existing file. Which was what I was trying to accomplish when the “writeButton” is clicked. (jayman9 Thank you for helping me to learn what I was doing wrong with this, now the file is showing that it exists)

Although when I answer “yes” on the dialog box to erase the existing file – it is not erasing.

What I am trying accomplish is:
1) When the writeButtons click event procedure is processed for the first time, it should be determined whether the names.txt file exists
2) If the file does exist, the user should be able to click on the yes button on the message box to replace the existing file, otherwise append to the existing file.

Test the application by:
using the “yes” button on the dialog box (this is still not working, when I do click yes to erase the files, the files do not erase)

end the application. Then type another name in the nameTextBox, click the writeButton. The application should ask if you want to replace the existing file. Click No.

end the application, use the file menu to open the names.txt file. The file should contain 2 names.

Start application again. Type another name in the nameTextBox, click the writeButton.
Click the Yes button to replace the existing file. Then the file should contain 1 name.
Revised code:
CODE

Private Sub writeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles writeButton.Click

        Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt"
        Dim name As String
        Dim text As String
        Dim button As DialogResult

        ' write name file to txt file
        name = nameTextBox.Text

        My.Computer.FileSystem.WriteAllText(file, _
            name, True)
        My.Computer.FileSystem.WriteAllText(file, ControlChars.NewLine & ControlChars.NewLine, True)

        ' check for names.txt file

        If My.Computer.FileSystem.FileExists(file) Then
            text = My.Computer.FileSystem.ReadAllText(file)

        Else
            MessageBox.Show("File does not exist", "Names", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        If My.Computer.FileSystem.FileExists(file & “names.txt”) Then
            button = MessageBox.Show("Erase the file?", _
            "Information", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Exclamation, _
            MessageBoxDefaultButton.Button2)
            If button = DialogResult.Yes Then
                My.Computer.FileSystem.WriteAllText(file & "names.txt", _
                String.Empty, False)
            End If
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seq. Access File
15 Mar, 2007 - 06:51 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Topics merged.



Same problem here as before. You already have the name of the file in the string so you don't need to include it in the IF statement or in the WriteAllText.
CODE

If My.Computer.FileSystem.FileExists(file) Then
button = MessageBox.Show("Erase the file?", _
"Information", MessageBoxButtons.YesNo, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button2)
If button = DialogResult.Yes Then
My.Computer.FileSystem.WriteAllText(file, _
String.Empty, False)
End If
End If

User is online!Profile CardPM
+Quote Post

km22
RE: Seq. Access File
16 Mar, 2007 - 07:47 AM
Post #5

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
I revised my code, and I am getting the result I was trying to achieve, except that I cannot figure out how to replace a name in the names.txt file when asked by the dialog box if I want to replace the existing file. For example if the name “Laura” was in the names.txt file I answered “yes” to the dialog to replace the existing file with “Steve.” Then “Steve” should be showing up in place of “Laura.”

This is the part of the code I am referring to and am not sure how it should be changed:

If button = DialogResult.Yes Then
My.Computer.FileSystem.WriteAllText(file, _
String.Empty, False)
End If
End If

Thank you for merging my posts, I kept getting a forbidden message for replying.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seq. Access File
16 Mar, 2007 - 08:34 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
When you are trying to replace, are you trying to delete all the information in the file and then insert the new information?

Or are you just trying to replace one name out of the many listed in the text file without overwriting the other names?
User is online!Profile CardPM
+Quote Post

km22
RE: Seq. Access File
16 Mar, 2007 - 09:33 AM
Post #7

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Thank you for your response. I am trying to do exactly what you mentioned in the first paragraph - I am trying to replace all the information, and then insert the new information.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Seq. Access File
16 Mar, 2007 - 09:53 AM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You do not need to wipe the file clean, so your use of String.Empty is not necessary.

By setting the last parameter to False in the WriteAllText method you are telling the method that you do NOT want to append the information. So what happens is it automatically wipes the file clean and overwrites it with the new information.
CODE

         My.Computer.FileSystem.WriteAllText(file, _
            String.Empty, False)

So all you need to do is replace String.Empty with the new information that you want to write to the file.
CODE

            If button = DialogResult.Yes Then
                My.Computer.FileSystem.WriteAllText(file, _
                name, False)
            End If


If you were to set the third parameter to True as you did earlier in the event. Then you are just adding new data to the file without deleting the old data.

User is online!Profile CardPM
+Quote Post

km22
RE: Seq. Access File
17 Mar, 2007 - 09:53 AM
Post #9

New D.I.C Head
*

Joined: 11 Mar, 2007
Posts: 32


My Contributions
Thank you, I figured it was something in the String.Empty, False. It makes sense now. Here is my completed code: (in case someone else is looking to accomplish something similar..)
CODE

Private Sub writeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles writeButton.Click

        Dim file As String = "C:\Documents and Settings\Owner\My Documents\Modified Employee List Solution\Employee List Project\bin\Debug\names.txt"
        Dim name As String
        Dim text As String
        Dim button As DialogResult

        ' write name file to txt file
        name = nameTextBox.Text

        My.Computer.FileSystem.WriteAllText(file, _
            name, True)
        My.Computer.FileSystem.WriteAllText(file, ControlChars.NewLine & ControlChars.NewLine, True)

        ' check for names.txt file

        If My.Computer.FileSystem.FileExists(file) Then
            text = My.Computer.FileSystem.ReadAllText(file)

        Else
            MessageBox.Show("File does not exist", "Names", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        If My.Computer.FileSystem.FileExists(file) Then
            button = MessageBox.Show("Erase the file?", _
            "Information", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Exclamation, _
            MessageBoxDefaultButton.Button2)
            If button = DialogResult.Yes Then
                My.Computer.FileSystem.WriteAllText(file, _
                name, False)
            End If
        End If

    End Sub

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:50PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month