View Full Version : common code


Qamar
07-14-2008, 06:59 AM
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

pbaldy
07-14-2008, 07:05 AM
Depends on what you mean by "string of text". Potential candidates include a global constant, global variable and public function.

Qamar
07-14-2008, 07:07 AM
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

pbaldy
07-14-2008, 07:15 AM
Then depending on how you use it, I'd narrow those choices to global constant and add local table storing it to the mix.

Qamar
07-14-2008, 07:19 AM
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

pbaldy
07-14-2008, 07:28 AM
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.

Qamar
07-14-2008, 07:35 AM
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.

pbaldy
07-14-2008, 07:44 AM
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.

Qamar
07-14-2008, 08:14 AM
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:

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

pbaldy
07-14-2008, 08:25 AM
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

Qamar
07-14-2008, 08:28 AM
Thank you very much for all your posts, you certainly set me on the right track :)