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

Stickey Variables?

Ok..this is my last of the points...

Desining program that will be used by multiple users to automate word.  Most of the varibles will change from use to use (its a will program, where the attorney inputs data from client)

WOUld like to have method for the attorney to enter his personal info (Name, etc) so it will remain in the program for his use (i guess like a constant) each time he restarts the program.   But if another attny needs to use, he would be able to change this variable...(and agian it would remain constant until changed)

This is my first visual basic program, and any help is appreciated

1 Solution
Create a form for settings.  Add text fields and OK/Cancel buttons.  The OK button does this:

SaveSetting "NameofProg", "PersonalSettings", "Name", txtName
SaveSetting "NameofProg", "PersonalSettings", "Phone", txtPhone


Then in the main program, use GetSetting to retrieve the info into variables

strName = GetSetting("NameofProg", "PersonalSettings", "Name")
Many ways to do it.  The most accepted would be to store it in the registry.  Easiest is probably storing it in an INI file.

The INI file can be loaded at startup and written to at shutdown (or when a value changes).

Here's some sample code:

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

' *****************************************************************************************************
'  SUBROUTINE       :   GetMyProfileString
'  DESCRIPTION      :   Returns a value from an INI file.
'  INPUTS           :   ByVal lpApplicationName - Name of the application
'                   :   ByVal lpKeyName         - Name of the key to look up in the INI File
'                   :   ByVal lpDefault         - Default value if no value found
'                   :   ByRef lpReturnedString  - String returned from the ini file (OUTPUT actually)
'                   :   ByVal nSize             - Number of characters to retrieve (usually 255)
'                   :   ByVal lpFileName        - Filename of the ini file. (usually App.Path & "\" & name)
'  OUTPUTS          :   RetVal                  - String returned from the ini file
'  REVISIONS        :   1.0.0 CB 04/01/2001 Original version.
' *****************************************************************************************************
Public Function GetMyProfileString(ByVal lpApplicationName As String, ByVal lpKeyName As String, _
                  ByVal lpDefault As String, ByRef lpReturnedString As String, ByVal nSize As Integer, _
                   ByVal lpFileName As String) As String
    Dim rc As Integer
    lpReturnedString = String$(255, 32)
    rc = GetPrivateProfileString(lpApplicationName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
    lpReturnedString = Trim$(Left$(lpReturnedString, rc))
    GetMyProfileString = lpReturnedString
    Exit Function

End Function

Call the function like this:

Public Sub Main()
   Dim lsValue as string

   lsValue= GetMyProfileString("Settings", "Value", "", lsValue, 255, App.Path & "\MyINIFile.ini")

End Sub

Ini file looks like:

Remember to check for valid data before saving it or using it in the program.  The last field of GetSetting is a default that you can use to put in a default in case no setting has ever been saved.

strName = GetSetting("NameofProg", "PersonalSettings", "Name", "John Smith")
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

PaulHews just showed you how to store it in the registry...  which as i said is probably the more accepted way of doing it.

Either way'll work though.
>Either way'll work though.

Agreed.  If you ever need to edit the setting outside of the program an INI file is the way to go (using notepad is so much less threatening for your users than regedit.)
Try using SetSetting to save the registry.
One comment to add to Paul's suggestion. This in fact works better than you would imagine: the SaveSetting/GetSetting functions work with the HKEY_CURRENT_USER Hive of the registry, this means that if you have different users logging in to the machine then the values that you store will be stored for these specific users and will automatically be different depending on who is logged into the computer.
Hi phred_the_cat@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept PaulHews's comment(s) as an answer.

phred_the_cat@devx, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as answer

E-E Moderator
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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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