CodeItBetter Programming Another VB Programming Blog

How to Use Open, Save dialog boxes of common dialog control. Notify user if a file already exists.

Posted on September 12, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'Controls - How to Use Open, Save dialog boxes of common dialog control. Notify user if a file already exists.
'Assume form1 is a form having following controls. Set the properties as given below :-
'
'Control Type            Name Property       Caption
'------------            -------------       -------
'1) Command Button       cmdOpen             &Open
'2) Command Button       cmdSave             &Save
'3) RichtextBox          rtbText
'4) Common Dialog        dlgCommon

'Now place the following code in the click event of cmdSave
Private Sub cmdSave_click()
    Dim chs As String
    With dlgcommon
        .cancelerror = False
        .ShowSave
    End With
    If Dir(dlgcommon.FileName) = "" Then
        rtbtext.savefile dlgcommon.FileName
    Else
        chs = MsgBox(dlgcommon.FileName & " already exist do you want to overwrite it", vbYesNo)
        If chs = vbYes Then rtbtext.savefile dlgcommon.FileName
    End If
End Sub
 
'Place the following code in cmdOpen's click event
Private Sub cmdOpen_click()
    Dim chs As String
    With dlgcommon
        .cancelerror = False
        .showopen
    End With
    If dlgcommon.FileName = "" Then Exit Sub
    If Dir(dlgcommon.FileName) = "" Then
        MsgBox dlgcommon.FileName & " does not exist"
        Exit Sub
    End If
    rtbtext.loadfile dlgcommon.FileName
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.