I'm just going to attach the exe so you can see what I'm talking about.
try this code. for testing take a textbox(text1),three buttons(command1-3) where 1 is for "Open",2 is for "Save" and 3 is for "Save As". Add a control commondialog to ur project. To add this right click->toolbox and select components->scroll down and check on "Microsoft Common Dialog Control 6.0"->click ok. drag the control on ur form and change its object name to "cd1".
Option Explicit
Dim openfile As String
Private Sub Command1_Click() ''For Open
Dim fnum As Integer
Dim str As String
On Error GoTo err1
cd1.CancelError = False
cd1.Filter = "Text Files{*.txt}|*.txt"
cd1.Flags = cdlOFNFileMustExist + cdlOFNPathMustExist
cd1.InitDir = "C:\"
cd1.DialogTitle = "Select file to open..."
cd1.ShowOpen
If cd1.filename <> "" Then
fnum = FreeFile
Open cd1.filename For Input As #1
str = Input(LOF(fnum), #fnum)
Close #fnum
Text1.Text = str
Close #1
openfile = cd1.filename
Me.Caption = "File Opened - " & cd1.filename
End If
Exit Sub
err1:
Err.Clear
MsgBox "Unable to open the requested file...!", vbCritical, "Error opening file"
Me.Caption = "File not opened...!"
Exit Sub
End Sub
Private Sub Command2_Click() ''For Save
On Error GoTo err1
If Trim(openfile) = "" Then
cd1.CancelError = False
cd1.Filter = "Text Files{*.txt}|*.txt"
cd1.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist
cd1.InitDir = "C:\"
cd1.DialogTitle = "Save as"
cd1.ShowSave
If cd1.filename <> "" Then
Open cd1.filename For Output As #1
Print #1, Trim(Text1.Text)
Close #1
End If
Me.Caption = "File Saved - " & cd1.filename
Else
Open openfile For Output As #1
Print #1, Trim(Text1.Text)
Close #1
Me.Caption = "File Saved - " & openfile
End If
Exit Sub
err1:
Err.Clear
MsgBox "Unable to save current content to the disk...!", vbCritical, "Error saving file"
Me.Caption = "File not saved...!"
Exit Sub
End Sub
Private Sub Command3_Click() ''For Save As
On Error GoTo err1
cd1.CancelError = False
cd1.Filter = "Text Files{*.txt}|*.txt"
cd1.Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist
cd1.InitDir = "C:\"
cd1.DialogTitle = "Save as"
cd1.ShowSave
If cd1.filename <> "" Then
Open cd1.filename For Output As #1
Print #1, Trim(Text1.Text)
Close #1
End If
Me.Caption = "File Saved - " & cd1.filename
Exit Sub
err1:
Err.Clear
MsgBox "Unable to save current content to the disk...!", vbCritical, "Error saving file"
Me.Caption = "File not saved...!"
Exit Sub
End Sub
Private Sub Form_Load()
openfile = ""
End Sub
hope this one helps you.
plz don't forget to post ur feedback.
regards
Shouvik