?
Solved

Stickey Variables?

Posted on 2001-06-13
9
Medium Priority
?
171 Views
Last Modified: 2010-05-02
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

thanks!
0
Comment
Question by:phred_the_cat
9 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 200 total points
ID: 6187763
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

etc.

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

strName = GetSetting("NameofProg", "PersonalSettings", "Name")
0
 
LVL 4

Expert Comment

by:beckingh
ID: 6187772
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:

[Settings]
Value=MYVALUE!
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6187776
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")
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:beckingh
ID: 6187778
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.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6187794
>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.)
0
 
LVL 1

Expert Comment

by:alaplume
ID: 6188777
Try using SetSetting to save the registry.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6190035
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7136069
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7168407
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question