Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

save variable value for next program boot

Dear all,
I want to save a variable value as bootup parameter when next time my program boots. How can I do?

any suggestion appreciated
1 Solution
Hi David777

Probably the registry is the best place and most common solution for such things.

Ray at home
Hi David777,

In 16-bit Windows-based applications this type of information generally stores in INI (initialization file) but in Win32-based applications you should store initialization information in the registry.

I hope it will help you.

Look up savesetting and getsetting in the help. These are used to save to the reg. But they only save into a special section of the reg if you want total control you should look at another function to read/write the reg.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

How about just writing it out to as file..?
Éric MoreauSenior .Net ConsultantCommented:
ExcelRay is correct. See GetSettings and SaveSettings in the VB help. These are really easy to use.
copy and paste....

'when your app ends, it saves the value in the file
'replace DesiredValue with the variable name you wish

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    Dim V As Long
    Open App.Path & "\" & App.EXEName & ".cfg" For Random As #1 Len = 4
    V = DesiredValue
    Put #1, 1, V
    Close 1
End Sub

'when your app starts, it loads the value from the file

Private Sub Form_Load()

    Dim V As Long
    Open App.Path & "\" & App.EXEName & ".cfg" For Random As #1 Len = 4
    Get #1, 1, V
    DesiredValue = V
    Close 1
End Sub

and if you like to use more values put this in a module:

type ConfigFile

     Value1 as long
     Value2 as long
     Value3 as long
     Value4 as date
     Value5 as integer
     SomeText as string * 100

end type

'and replace the lines:
'    Dim V As Long
'    Open App.Path & "\" & App.EXEName & ".cfg" For Random 'As #1 Len = 4

'for the lines
'    Dim V as configfile
'    Open App.Path & "\" & App.EXEName & ".cfg" For Random As #1 Len = len(v)

'to retrieve the values query them inside the form_load like this:

'    somevar = v.value1
'    someOtherVar = v.value2

hope this helps.
Here's how we implemented it:

SaveSetting App.CompanyName, App.EXEName, RegistryEntryName, strDataToSave


strRegInfo = GetSetting(App.CompanyName, App.EXEName, RegistryEntryName, vbNullString)

If strRegInfo = vbNullString Then
  msgbox "not currently in registry"
  ' load strRegInfo somewhere to continue where you left off or otherwise restore a setting
end if

The above would work well for doing things like restoring the window location and size to wherever the user left it when the app closed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now