Solved

Stickey Variables?

Posted on 2001-06-13
9
161 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 50 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now