Re: Variables and scope ? !!!!
- From: "Nick Calladine" <n i c k c a l l a d i n e @ n t lw orl d . c o m (remove all spaces)>
- Date: Tue, 20 Sep 2005 15:19:08 +0100
Hi Jezebel
Thanks for the reply it was very informative
I do understand your point and see how i should do something like this.. it
just my document a protected form and I have controlled the delete /
insertion routine to basically never remove / create afirst table.
So would you still suggest i should not define the table as global
variable.. and do it within each sub as when required... I know it hard
without seeing the project but i no professional vba programmer but would
like to learn good practices and this project is just a simple table
caculation but the data is always being scanned for validation rules and
also changes so i seemed to be using the with activedocument.tables(1) alot
in my code and it was if i ever had change the code i though a global
variable and name would make the code more readable.. and if i was doing a
large project (many many years from now ... lol) would this be a better way
of coding to be able change one variable and thus update my code...
The other thing i am doing at the moment is writing a lot of the code in non
module form and only adding functions to modules... should all code be
written in to a module or in the thisdocument area
At present my code is triggered from a toolbar...and user input is directly
in to a protected document ... should my click code be triggered this way...
Finally do have any good site or books you can recommend ... i getting a
copy of vba for dummies to go through any other suggestions for reading and
learning techniques
Many thanks
Nick
"Jezebel" <warcrimes@xxxxxxxxxxxxxx> wrote in message
news:OlkrJmbvFHA.2292@xxxxxxxxxxxxxxxxxxxxxxx
> First: constants and variables are different beasts. Constants are a
> shorthand way to write literals into your code, to improve readability and
> maintainability. You can use them only for simple data types (strings,
> longs, etc --- not objects). Eg ---
>
> Const PI as double = 3.14159
>
> then later in code you can use 'PI' in expressions ...
>
> Area = pRadius * pRadius * PI
>
>
> Second: scope is a separate issue. Your constants and variables can be
> global -- available to all code in the application, declared in the header
> of an ordinary module with 'Global' or 'Public'; or they are module
> level -- declared in the header of any code module, optionally with
> 'Private'; or they are procedure level, declared within a procedure.
>
>
> Third: to create a global reference to an object you need two pieces of
> code --
>
> a) Declare the variable, in the header of an ordinary module
>
> Public gtblEmployees as Word.Table
>
> b) Set the variable within a function
>
> Public Sub AutoOpen()
> on error resume next
> Set gtblEmployees = ActiveDocument.Tables(1)
> err.clear
> End Sub
>
>
> Note that the variable is cleared if you reset the code project, which
> happens automatically if you edit any code in the project.
>
>
> Fourth: Global variables are, in general, bad practice. They are a prime
> cause of bugs, and you hardly ever need them anyway. In this case for
> example, what happens if the table gets deleted? Or another table gets
> inserted before it? etc
>
>
>
>
> "Nick Calladine" <n i c k c a l l a d i n e @ n t lw orl d . c o m (remove
> all spaces)> wrote in message
> news:e5FPiVWvFHA.2312@xxxxxxxxxxxxxxxxxxxxxxx
>>I wonder if some one can clarify if it is possible
>>
>> I want to set a global or constant in word at the start of a document /
>> module in vba is this possible
>>
>> I understand to set a variable i use the "public" and if i require a set
>> value that i would use the "const"
>>
>> But how do set a variable to the whole document which refers to a table
>>
>> something like const tblEmployees As Tables = ActiveDocument.Tables(1)
>>
>> or this is poor coding or bad practice... ?
>>
>> if ppl have any tutorials or links which might help it all become clear
>>
>> many thanks
>>
>>
>>
>>
>>
>>
>
>
.
- Follow-Ups:
- Re: Variables and scope ? !!!!
- From: Jonathan West
- Re: Variables and scope ? !!!!
- References:
- Variables and scope ? !!!!
- From: Nick Calladine
- Re: Variables and scope ? !!!!
- From: Jezebel
- Variables and scope ? !!!!
- Prev by Date: Re: Error whilst fields are updating
- Next by Date: Re: Variables and scope ? !!!!
- Previous by thread: Re: Variables and scope ? !!!!
- Next by thread: Re: Variables and scope ? !!!!
- Index(es):
Relevant Pages
|
Loading