Ask Experts Questions for FREE Help !
Ask
    neo_lover2000's Avatar
    neo_lover2000 Posts: 12, Reputation: 1
    New Member
     
    #1

    Jun 25, 2009, 10:48 PM
    visual basic
    I made this code to save text file. But now I am wondering how to save a file if there is an existing file name. I would like to add a mnuItemSave for that. Is it work? How to code that?

    Private Sub mnuItemsave_Click()
    CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
    CommonDialog1.ShowSave 'display Save dialog
    If CommonDialog1.FileName <> "" Then
    Open CommonDialog1.FileName For Append As #1
    Print #1, txtFile.Text 'save string to file
    Close #1 'close file
    End If
    End Sub

    Private Sub mnuItemOpen_Click()
    Wrap$ = Chr$(13) + Chr$(10) 'create wrap character
    CommonDialog1.Filter = "Text files (*.TXT)|*.TXT|EXCEL files (*.xls)|*.xls|All Files (*.*)|*.*"
    CommonDialog1.ShowOpen 'display Open dialog box
    If CommonDialog1.FileName <> "" Then
    ' Form1.MousePointer = 11 'display hourglass
    Open CommonDialog1.FileName For Input As #1

    ' On Error GoTo TooBig: 'set error handler

    Do Until EOF(1) 'then read lines from file
    Line Input #1, LineOfText$
    AllText$ = AllText$ & LineOfText$ & Wrap$
    Loop
    lblFile.Caption = CommonDialog1.FileName
    txtFile.Text = AllText$ 'display file
    txtFile.Enabled = True
    mnuItemClose.Enabled = True
    mnuItemOpen.Enabled = False
    End If
    Close #1
    'CleanUp:
    ' Form1.MousePointer = 0 'reset mouse
    ' Close #1 'close file
    ' End If
    'Exit Sub
    'TooBig: 'error handler displays message
    ' MsgBox ("The specified file is too large.")
    ' Resume CleanUp: 'then jumps to CleanUp routine
    End Sub


    Private Sub mnuItemClose_Click()
    txtFile.Text = "" 'clear text box
    lblFile.Caption = "Load a text file with the Open command."
    mnuItemClose.Enabled = False 'dim Close command
    mnuItemOpen.Enabled = True 'enable Open command
    txtFile.Enabled = False 'disable text box
    End Sub

    Private Sub mnuItemExit_Click()
    End 'quit program
    End Sub

    thanks
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    Jun 26, 2009, 08:13 AM
    Quote Originally Posted by neo_lover2000 View Post
    i made this code to save text file. but now i am wondering how to save a file if there is an existing file name. i would like to add a mnuItemSave for that. is it work? how to code that?
    Let's see if I understand this. You want to save a file if the user has already specified a file name. Is this what you mean? In the following, when the user specifies a file name, it's saved as a module-level variable (not inside a subroutine), LastFileName. If the user clicks a menu item labeled "Save Existing" (mnuSaveExisting), it calls the second subroutine, below. That subroutine checks the LastFileName variable to make sure that it's not empty and then does the same thing that the mnuItemsave_click subroutine does -- except it doesn't call the CommonDialog1 object.

    Private LastFileName As String

    Private Sub mnuItemsave_Click()
    CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
    CommonDialog1.ShowSave 'display Save dialog
    If CommonDialog1.FileName <> "" Then
    LastFileName = CommonDialog1.FileName
    Open CommonDialog1.FileName For Append As #1
    Print #1, txtfile.Text 'save string to file
    Close #1 'close file
    End If
    End Sub

    Private Sub mnuExisting_Click()
    If LastFileName <> "" Then
    Open LastFileName For Append As #1
    Print #1, txtfile.Text 'save string to file
    Close #1 'close file
    End If
    End Sub

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Visual basic [ 1 Answers ]

I already finish a very simple calculator,actually it has only four buttons. at first its working fine but when add another feature its not working anymore. What I want to do is when press 1 then press + then press 1 and press + again it should add the first two number get ready to add another...

Visual basic [ 1 Answers ]

I want to to create a calculator in visual basic. How can I do this.there only that key preasent what I know that is (0-9, +, -, *, /,. = cleare)this code are preasent. So tell me the code which code I use to create this buttons.

Visual Basic [ 1 Answers ]

Actually I'm a new user(student) of Visual Basic.So I want to know how I am make a simple calculator in Visual Basic 6?

Visual basic [ 1 Answers ]

I am doing visual basic program. Could you help with the code how to login.

Visual Basic [ 1 Answers ]

I want to add icon in the button which I have used in my visual basic program. How can I do that. Please help.


View more questions Search