PDA

View Full Version : Declare variable to use through out module:



Trevor
03-23-2008, 09:39 PM
I Would like to declair a string , the same string will be used in different events within the same module:
for example:

Private some1 onclick ()
string 1 = [Some procedure]
End Sub

Private Some2 ()
String 1 > Me.{Textbox] Then
[Do Somthing]
End sub

I have tried directly under option explicit "Dim string1 as string", and also on directly under option explicit "Private string1 as string", "Public string1 as string"
and everything I have tried string1 in private some2() is a null value
I have tested this with texboxes to varify that there ina value onclick event and their is, I could use texbox's to pass the value between the different events but would like tn be able to pass string values.
Thanks for helping

CreganTur
03-24-2008, 05:27 AM
I Would like to declair a string , the same string will be used in different events within the same module:
for example:

Private some1 onclick ()
string 1 = [Some procedure]
End Sub

Private Some2 ()
String 1 > Me.{Textbox] Then
[Do Somthing]
End sub

I have tried directly under option explicit "Dim string1 as string", and also on directly under option explicit "Private string1 as string", "Public string1 as string"
and everything I have tried string1 in private some2() is a null value
I have tested this with texboxes to varify that there ina value onclick event and their is, I could use texbox's to pass the value between the different events but would like tn be able to pass string values.
Thanks for helping

The issue seems to be the fact that you're delcaring it as Private. You need to change it to Public.



Remarks
Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project (javascript:hhobj_27.Click()) in which they reside.

Try:


Public some1 onclick ()
string 1 = [Some procedure]
End Sub

Public Some2 ()
String 1 > Me.{Textbox] Then
[Do Somthing]
End sub

akn112
03-24-2008, 07:35 AM
It shouldnt matter if the sub is public or private but only the variable.


option explicit
dim myString as string

private sub optOption_click()
myString= 'Some procedure
end sub

private sub optOption2_click()
if myString> txtString.value then
msgbox myString
end if
end sub


should work. I would recommend checking to see what your "procedure" is at the time of the click. I think that's where your problem may lie. To do that, either put "msgbox myString" after you set the value of myString or put a line break at the time it's setting the value and use the immediate window to test the value of the "procedure"

Trevor
03-25-2008, 12:10 PM
I tried declaring as public and it didn't work,

Public sub Ok Onclick()
StrStatus = DCount("[Status]", "PSwrdAttemptTbl", "Status = '" & StStatus & "'")
[More code]
End sub
Public rec()
if strstus > 3 Then msgbox "count" & Chr$(32) & strstatus
EndSub

in the rec sub strstus always come up as 0

ben.oates
03-26-2008, 03:08 AM
Try converting it to an integer for the comparison.
If CInt(strstus) > 3 Then ...
If that doesn't work, put a breakpoint on that line, right click on strstus and add a watch and tell us what the value is.

Also, I don't know if this is a true representation of your code... but you're using two different variable names in there. StrStatus <> strstus.

DarkSprout
03-26-2008, 06:46 AM
Any variable created in a Form, Report or Class will only ever be Private
Create a New Module called 'mdl_PublicVariables', and within it...


Option Compare Database
Option Explicit

Public String1 As String ' No variable can have a space in its name
Public Const conYellow = 8454143 ' Public Constant

Trevor
03-26-2008, 07:45 AM
ben.Oats, yes there are 2 diff variable names ststatus is the result of a dlookup for the value (name) in the status field of table
strstatus is the result of a dcount for the # of times that status name appears in a different table

Trevor
03-26-2008, 07:49 AM
thanks Darksprout, I don't think I want to got that far for what is otherwise only a page or 2 of code, it would be easyer just to state the expression Used earlier in the code, I'm just trying to parse and pass variable value between differnt event without re typing the same come( excluding calls)

ben.oates
03-26-2008, 08:15 AM
Could you check the value of strstus in a watch? Did you try CInt(strstus) ?