Thursday, July 30, 2009

How do I save in Visual Basic?

First off, I don't want people criticising me about my title, I made it like that to attract more people.





Anyways, my question is, how can I specify where I save a text file (notes.txt), by typeing in the path?


So instead of





C:\Documents and Settings\HP_Administrator\My Documents\My Pictures\notes.txt





I would like to change the file to this the location bellow...at runtime.





C:\Documents and Settings\HP_Administrator\My Documents\VB Files\notes.txt





This is my original code:





Private Sub cmdSave_Click()


msgsave = MsgBox("Are you sure you want to save? Any previous data will be deleted.", 4 + 48, "Save?")


If msgsave = 6 Then


Open "C:\Documents and Settings\HP_Administrator\My Documents\My Pictures\notes.txt" For Output As #1


Print #1, txtNotes.Text


Close #1


End If


End Sub





So I need a (possible simple) way to save my file to a new location anytime I want to while the program is running. If you have any questions just post them and I'll try to answer

How do I save in Visual Basic?
Here is some working code I wrote in VB5 (but it should work in v6). It opens a SAVEAS dialog box and defaults that dialog box to an INI type file. Then it writes specific info. to that file.





Private Sub Do_SaveConfig()


' Set CancelError is True


CommonDialog1.CancelError = True


On Error GoTo ErrHandler


' Set flags


CommonDialog1.Flags = cdlOFNHideReadOnly


' Set filters


CommonDialog1.Filter = "Config Files" %26amp; "(*.ini)|*.ini"


' Specify default filter


CommonDialog1.FilterIndex = 1


' Display the Open dialog box


CommonDialog1.ShowSave


' Display name of selected file





fn = CommonDialog1.FileName


If Dir(fn) %26lt;%26gt; "" Then


Kill fn


End If


fHnd = FreeFile()


Open fn For Output As #fHnd





Write #fHnd, "# This file may be discarded, safely."


Write #fHnd, "# Used by AREP Circuit Visual Calculator, only."


Write #fHnd,


Write #fHnd, "Opt_DACdec", Opt_DACdec.Value


Write #fHnd, "Opt_GainDACdec", Opt_GainDACdec.Value


Write #fHnd, "Opt_VoutV", Opt_VoutV.Value


Write #fHnd, "Opt_VstepV", Opt_VstepV.Value


Write #fHnd, "Txt_G1sense", Txt_G1sense.Text


Write #fHnd, "Txt_Gain1", Txt_Gain1.Text


Write #fHnd, "Txt_GainCode", Txt_GainCode.Text


Write #fHnd, "Txt_OffsetCode", Txt_OffsetCode.Text


Write #fHnd, "Txt_Rin", Txt_Rin.Text


Write #fHnd, "Txt_Rbias", Txt_Rbias.Text


Write #fHnd, "Txt_Roffset", Txt_Roffset.Text


Write #fHnd, "Txt_Rf", Txt_Rf.Text


Write #fHnd, "Txt_Rtrim2", Txt_Rtrim2.Text


Write #fHnd, "Txt_Vbias", Txt_Vbias.Text


Write #fHnd, "Txt_Vref", Txt_Vref.Text


Write #fHnd, "Txt_Vin", Txt_Vin.Text


Close





Exit Sub





ErrHandler:


'User pressed the Cancel button


If Err.Number = 32755 Then Exit Sub


MsgBox "Save error: " %26amp; Err.Number %26amp; vbCr %26amp; vbLf %26amp; _


Err.Description


Exit Sub





End Sub





.

liama-song

No comments:

Post a Comment