954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Open\Save code

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.

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Open and save what rockstar ?

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

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

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

not understood actually. plz clarify in detail

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

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

Attachments Notebook.zip (35.57KB)
rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
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

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

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.

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
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.

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 
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

Attachments Notebook.zip (6.35KB)
rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

[QUOTE=choudhuryshouvi;502870]

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

/QUOTE]

This link is an upgrade for VB 6 not to upgrade VB5 to Vb6. To get VB6 then you have to buy a new licence.

Denis

DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 

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

Attachments NB_Revised.zip (6.25KB)
choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

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.

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

My email is [email]kmt0384@mtco.com[/email]. Thank you very much for all everyone has done to help me.

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

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.

rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

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.

Attachments error.gif 5.64KB
rockstar0384
Newbie Poster
9 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

Attachments rockstar.zip (1465.26KB) OrigFiles.zip (2.05KB)
DenisOxon
Posting Whiz
355 posts since Jan 2007
Reputation Points: 22
Solved Threads: 19
 
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%\\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

Attachments ocx.zip (61.87KB)
choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You