It might already be posted but i was just wondering what the open and save code was that you would put into a VB5 form. I would appreciate it if someone informed me. Thanks.

arlene.deluna.568 commented: i love being iT +0

Open and save what rockstar ?

VB calls it an "frmNewform" but i probably would want to open\save it as a txt document

not understood actually. plz clarify in detail

I'm just going to attach the exe so you can see what I'm talking about.

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

Unless i didn't do it right, it didnt work for me. one thing i noticed in the code though was to pick something with version 6 at the end. I only have version 5. So i tried five instead and like I said it didn't work.

Unless i didn't do it right, it didnt work for me. one thing i noticed in the code though was to pick something with version 6 at the end. I only have version 5. So i tried five instead and like I said it didn't work.

This code runs smoothly in VB6 without having a single piece of problem.
Looks like you are using VB5. Isn't it? Now could you plz tell what exactly was happened when you tried that code? Can u post the output here or your application(if possible)???

If you wish to continue in using the above code you have to upgrade your application to version 6.

Are you interested in upgrading to VB6? If yes, then you can download the service pack files from here: http://www.microsoft.com/downloads/details.aspx?familyid=7b9ba261-7a9c-43e7-9117-f673077ffb3c&displaylang=en

Let me know if this works for you.

This code runs smoothly in VB6 without having a single piece of problem.
Looks like you are using VB5. Isn't it? Now could you plz tell what exactly was happened when you tried that code? Can u post the output here or your application(if possible)???

First off, I do have VB5. Second, my problem is that when i put your code in and changed it to agree with my program it said some variables were undefined. I could try to update, but variables being undefined was my only problem.

I'll try to attach it again. The files are separate this time

First off, I do have VB5. Second, my problem is that when i put your code in and changed it to agree with my program it said some variables were undefined. I could try to update, but variables being undefined was my only problem.

I'll try to attach it again. The files are separate this time

Ok i've checked your application. The errors you were getting was in your program. It was missing the object names like in my sample coding which i posted here have a textbox with object name "Text1". now what happened(if i was not wrong) you just copied the code within your form and forgot to change the name of textbox. In your form(frmtemplate.frm) there is no textbox control with name "Text1". Only you have is the "txtuserarea" located in your frmtemplate.frm . Now i've fixed this in your coding and you will be happy by hearing that everything is running smoothly without any single piece of error.

OK??

Try the attached one and plz don't forget to post your feedback.

Good Luck

regards
Shouvik

I marked it as solved but it said that the files were invalid or corrupt when i tried to download them. I assume that it works so thank you very much.

well that's a problem of daniweb itself. the same problem had been faced by many other members also. but don't worry, if u didn't get the file properly just give me your e-mail id and i'll send the zipfile to you.

OK.......

regards
Shouvik

Hello rockstar,

i've send the zip file to your e-mail id.
check that and send me your feedback.

for your consideration plz use a well-reputed compressing software(e.g, WINZIP) to extract files from the zip archive. Use the latest version of winzip. you can freely download it from :-
http://www.winzip.com/index.htm

good luck

with regards
Shouvik

It didn't work in my VB. It said, "'Retained' is an invalid key." Could you maybe just send me the already made exe because that is all the code I wanted to put in it anyway. I might be able to open the exe better. Like i said, I only have VB5. So just go ahead and make it into an exe for me. Thanks.

ok man i've sent the exe to your e-mail id.
check that.
hope this time it will work for you.

regards
Shouvik

I feel bad that i keep coming and saying it's not working, but it still isn't working. I got another error about the CommonDialog. I'll attach the screen shot for you.

Hi Rockstar,

Two files attached

1) Origfile.zip contains project file and form you need as source for VB5.

2) Rockstar.zip contains install set that you can use to install program on a pc, whether has vb5 or not.

Denis

I feel bad that i keep coming and saying it's not working, but it still isn't working. I got another error about the CommonDialog. I'll attach the screen shot for you.

that's a version confliction problem. not a problem generating from the program at all.
Ok i'm attaching the comdlg32.ocx used in this code. copy this file to your system directory and register it using the following command :-

regsvr32 %systemroot%\<name of your system directory>\comdlg32.ocx

if u r using win9x then ur system dir becomes "system" without quotes.
if u r using winxp or higher ur system dir becomes "system32" without quotes.

try this.
hope this time it will surely work for you.

regards
Shouvik

How do you code a save command button for your vb6 project, so that when it is run and the user clicks the save command button, it opens the save as dialogue box?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.