Re: Simple Question on Variable Scope

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Dave (davefrick_at_newsgroup.nospam)
Date: 01/31/05


Date: Sun, 30 Jan 2005 20:02:08 -0800

Thanks guys

Dumb question but does the variable declaration have to precede the
procedure in the module?

IOW Do I have to declare module level variables before I write any
functions/procedures?

"Brendan Reynolds" <brenreyn at indigo dot ie> wrote in message
news:%23LwloLzBFHA.1452@TK2MSFTNGP11.phx.gbl...
>I can't reproduce this. I copied and pasted your code, the only thing I
>changed was the names of the controls, as follows ...
>
> Option Compare Database
> Option Explicit
>
> 'Private bln As Boolean
> Public bln As Boolean
>
> Private Sub cmdTest_Click()
>
> IsItEven (txtTest)
> Debug.Print "Calling Proc = " & bln
>
> End Sub
>
> 'Private Sub IsItEven(ByRef x As Integer)
> 'Private Sub IsItEven(ByVal x As Integer)
> Private Sub IsItEven(x As Integer)
>
> If x Mod 2 = 0 Then
> bln = True
> Else
> bln = False
> End If
>
> Debug.Print "Called Proc = " & bln
>
> End Sub
>
> Here are my results, first with the value "2" in the text box (x Mod 2 = 0
> evaluates to True) and then with the value "3" in the text box (x Mod 2 =
> 0 evaluates to False) ...
>
> Called Proc = True
> Calling Proc = True
> Called Proc = False
> Calling Proc = False
>
> By the way, a Boolean variable can never, under any circumstances, have
> the value Empty. Only a Variant can have that value.
>
> --
> Brendan Reynolds (MVP)
>
> "Dave" <davefrick@newsgroup.nospam> wrote in message
> news:u6fiw4yBFHA.2012@TK2MSFTNGP15.phx.gbl...
>>I need to call a sub procedure and have it assign a value to a variable
>>that will be available throughout a module.
>>
>> I can call a function and have it return a value to the calling
>> procedure. But I cannot call a procedure from another procedure, have it
>> change the value of a variable and then be able to reference this
>> variable in other procedures within the module.
>>
>> For example, in the code below, the value of "bln" is true in the called
>> proc but in the calling proc it is empty.
>>
>> What don't I understand?
>>
>> Dave
>> -------------------
>>
>> 'Private bln As Boolean
>> Public bln As Boolean
>>
>> Private Sub Command41_Click()
>>
>> IsItEven (txtX)
>> Debug.Print "Calling Proc = " & bln
>>
>> End Sub
>>
>>
>> 'Private Sub IsItEven(ByRef x As Integer)
>> 'Private Sub IsItEven(ByVal x As Integer)
>> Private Sub IsItEven(x As Integer)
>>
>> If x Mod 2 = 0 Then
>> bln = True
>> Else
>> bln = False
>> End If
>>
>> Debug.Print "Called Proc = " & bln
>>
>> End Sub
>>
>
>



Relevant Pages

  • Re: Simple Question on Variable Scope
    ... 'Private bln As Boolean ... 'Private Sub IsItEven ... Calling Proc = False ...
    (microsoft.public.access.formscoding)
  • Re: Simple Question on Variable Scope
    ... module that also Dim bln as a local variable. ... > proc but in the calling proc it is empty. ... > 'Private bln As Boolean ... > 'Private Sub IsItEven ...
    (microsoft.public.access.formscoding)
  • Re: Simple Question on Variable Scope
    ... >> 'Private bln As Boolean ... >> Private Sub cmdTest_Click ... >> 'Private Sub IsItEven ... >> Calling Proc = False ...
    (microsoft.public.access.formscoding)
  • Re: Dont save file if condition is met
    ... I tried to save the workbook and your message popped up and the workbook closed. ... Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) ...
    (microsoft.public.excel.programming)
  • Re: Creating a list of workbook users
    ... Private Sub Workbook_BeforeClose(Cancel As Boolean) ... With Worksheets("hidden sheet") ... also created a macro in my personal workbook that allows me to toggle ...
    (microsoft.public.excel.programming)