Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dialog boxes and file i/o

Posted on 1997-03-18
7
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 180 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

719 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