Solved

dialog boxes and file i/o

Posted on 1997-03-18
7
266 Views
Last Modified: 2008-03-17
File I/0

I’m a little confused about file saving and save as;  The below program opens up text files with out a problem.  I’ve managed to cal the save (and saveAs) dialog boxes.  I’d like to have the save function to overwrite the existing file using the previous file name, providing the file has not been saved before (how can I tell this?) .  How would I add a msgbox that would ask the user if he would like to save any changes befor exiting?



Private Sub Command1_Click()

End Sub

Private Sub cmdCloseFile_Click()
   
        Close
        cmdOpenFile.Enabled = True
        txtWorkPad.Text = ""
        txtFileNameOpen.Text = ""
End Sub

Private Sub cmdOpenFile_Click()

    On Error GoTo DialogError
   
    cmOpenFile.ShowOpen
    Open cmOpenFile.filename For Input As 1
    txtWorkPad.Text = Input(LOF(1), 1)
    txtFileNameOpen = cmOpenFile.filename
   
    cmdOpenFile.Enabled = False
   
DialogError:
    On Error GoTo 0
    Exit Sub
   
End Sub


Private Sub Form_Load()

' Set up the common dialog control to open a text file

    On Error GoTo DialogError

    With cmOpenFile
        .CancelError = True
        .Filter = "Text Files (*.txt)|*.txt"
        .FilterIndex = 1
        .DialogTitle = "Select a Text File"
    End With

DialogError:
    On Error GoTo 0
    Exit Sub

End Sub

Private Sub mnuEditReplace_Click()

   frmSearchString.Show 1

   
End Sub

Private Sub mnuFileClose_Click()

' Close all open files
        Close
' reset the menus and the caption bar

        mnuFileOpen.Enabled = True
        txtWorkPad.Text = ""
        frmWordPad.Caption = "WordPad"
        mnuFileClose.Enabled = False
        mnuFileSave.Enabled = False


End Sub


Private Sub mnuFileExit_Click()
    End
End Sub


Private Sub mnuFileOpen_Click()

    On Error GoTo DialogError

' Common dialog has been set up in form load event
        cmOpenFile.ShowOpen
' take filename selected by user and open it up
        Open cmOpenFile.filename For Input As 1
' read the whole file into the text box
        txtWorkPad.Text = Input(LOF(1), 1)
       
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
' don't allow another file to be opened
        mnuFileOpen.Enabled = False
' however we can save or close the file
        mnuFileClose.Enabled = True
        mnuFileSave.Enabled = True
        mnuFileSaveAs.Enabled = True
       
DialogError:
        On Error GoTo 0
        Exit Sub
   
    End Sub






Private Sub mnuFileSave_Click()

    On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save Your Text File"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
        Open cmOpenFile.filename For Output As 1
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close
   
End Sub

Private Sub mnuFileSaveAs_Click()
On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save As"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close

End Sub
0
Comment
Question by:johndill
7 Comments
 

Expert Comment

by:Sargon
ID: 1424617
All this for 50 points?
0
 

Author Comment

by:johndill
ID: 1424618
Well as you can see I wrote most of the code myself.  All I need help with is the mechanics of Save ans Save As.  I've already crested every thing else.  What say you to 70?
0
 

Expert Comment

by:limacher
ID: 1424619
At no point in your code example, have you used FLAGS.

I've had a brief view of the code, it's a little to much for one question and 70 points.

Look in the Help File for the FLAGS property of the Common Dialog Control. This will assist in with most the code.

PS.......If you are opening and Closing a text file, then why not use the RichTextFormat control. It's easier, faster and more code efficiant. Note that it 32bit only.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:johndill
ID: 1424620

0
 
LVL 2

Expert Comment

by:dirkmartin
ID: 1424621
I'd need 150 points for this one.
0
 

Author Comment

by:johndill
ID: 1424622
Thanks I'll try the flags.  I can only offer 90 at this point as my account is tapped
0
 
LVL 2

Accepted Solution

by:
weekee earned 90 total points
ID: 1424623
Basically you are trying to detect whether a user has save a file or. My approach is not very classic but it si simple. Simply creat a variable called saveFlag as boolean. Every time i open a new file  and the text is modified i set the flag to false. This is done by setting the flag to false in the keypress event of the textbox where the text is displayed. So what you need to do is compared whether this flag is False before exiting. If it is false, prompt the user. Once the file is save, set the flag to true...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now