common code

Qamar

Registered User.
Local time
Yesterday, 23:05
Joined
Jun 28, 2004
Messages
42
I have a string of text which i shall use often in my code in different places. What is the best solution to adopt to set this text in one place and then reference to it, so that if i need to change it, i would only have to change it once?

TU
 
Depends on what you mean by "string of text". Potential candidates include a global constant, global variable and public function.
 
Hello and thank you for your reply.

My plan of this string of text is to be a path, reason being that when i run database on other pc, i would only have to change a certain path once, and not everywhere i used it.

TU
 
Then depending on how you use it, I'd narrow those choices to global constant and add local table storing it to the mix.
 
so you mean i should create a table with just 1 record?

So if i create something globally, for example Public varname As String, could i set the string on an onOpen or OnLoad event of a form to set it to my path?

TU
 
Yes, a table with one record. You could use a public variable that you'd populate when a form opened as you describe. That would require you to enter the string each time the application started. The table and global constant would retain the value when the application closed.
 
You could use a public variable that you'd populate when a form opened as you describe.

If i put something on the OnOpen event such as VarName = "path", what do you mean exactly by having to populate each time, as i thought that i would only have to do it once.

Moreover, if you think that your other suggestion is more efficient you mean i can create something as follows in a standard module:

Global Const PATHNAME = "path"

If yes, what do i need the table for?

Thank you for your time.
 
A global variable you assign the value to when opening a form will lose its value when the application is closed, so you would have to input it again when the application opened. A global constant will hold the value when the application is closed, but can't be changed by the user (outside the VBA editor). You don't need a table, it's simply another option. As I said, which I would use would depend on how you use the value.
 
Hi,

Thanks for your reply. I think the best solution would be your suggestion of using a 1 record table.

My very final last question because i think i really made you bored.

for example if i have something like:

Code:
Me.ControlName.Picture = "C:\Documents And Settings\User1\My Documents\Databases\DB1\" & filename (which i get from controls)

then i would be storing C:\Documents And Settings\User1\My Documents\Databases\DB1\ in the table, however, how can i call the part of path from the table to the above code?

Thank you
 
If this is how you normally use the value, I'd go with the global constant, which would look like:

Me.ControlName.Picture = VariableName & filename

If the table better suits your overall needs:

Me.ControlName.Picture = DLookup("FieldName", "TableName") & filename
 
Thank you very much for all your posts, you certainly set me on the right track :)
 

Users who are viewing this thread

Back
Top Bottom