Stripping Variable values (1 Viewer)

Zydeceltico

Registered User.
Local time
Today, 02:27
Joined
Dec 5, 2017
Messages
843
I was saying much the same thing but I prefer to make my functions more generic.

So I might have a global/public variable defined in a standard module
E.g. Public lngClientID as Long.

Then in one or more forms set the value
E.g lngClientID = Me.ClientID

Then have a function GetClientID =lngClientID so I can use it in queries.

That’s precisely how I was thinking of using the public variables I mentioned. I didn’t know about the function aspect (thank you) - but now I do.

Now I need to study up on how to use functions in queries.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 02:27
Joined
May 21, 2018
Messages
8,527
Code:
Public Function GetInspId() as Long
  if currentProject.allforms("SomeFormName").isloaded then
     GetInspId = forms("SomeFormName").InspectionEventID.value
  end if
end function

To be clear I never use form references in a query. Most difficult thing in the world to debug. However, I would use the above in a query

Select * from sometable where InspectionEventID_FK = getInspctID()

the nice thing about that is that you can open the query even if the form is not loaded without getting an error.
 

Zydeceltico

Registered User.
Local time
Today, 02:27
Joined
Dec 5, 2017
Messages
843
Code:
the nice thing about that is that you can open the query even if the form is not loaded without getting an error.[/QUOTE]

That would be a nice change of pace. :rolleyes:
 

Zydeceltico

Registered User.
Local time
Today, 02:27
Joined
Dec 5, 2017
Messages
843
Code:
Public Function GetInspId() as Long
  if currentProject.allforms("SomeFormName").isloaded then
     GetInspId = forms("SomeFormName").InspectionEventID.value
  end if
end function

So - - - -I already have a Public Variable that I am using declared like this:

Dim strFormName As String

I use it to return the name of the form I need to call.

If I wanted to use that inside of the function you've suggested above would the syntax look like this:

GetInspID = Forms(strFormName).InspectionEventID.Value

OR like this:

GetInspID = Forms!strFormName.InspectionEventID.Value

or something else entirely?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
If I wanted to use that inside of the function you've suggested above would the syntax look like this:

GetInspID = Forms(strFormName).InspectionEventID.Value

OR like this:

GetInspID = Forms!strFormName.InspectionEventID.Value

The first one can use a string variable. The second one won't work as the form name is an integral part of expression.

BTW You can leave off Value as it is the default property of the control.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
… can't tell if it's understood that a variable declared as Public in a form module isn't available to any other form or report.

Public variables are available to other forms and reports. They are exposed as members of the object. Like any other member they just need the reference qualified.

Code:
Forms!formname.publicvariablename
 

Zydeceltico

Registered User.
Local time
Today, 02:27
Joined
Dec 5, 2017
Messages
843
The first one can use a string variable. The second one won't work as the form name is an integral part of expression.

BTW You can leave off Value as it is the default property of the control.

So in the first one the variable name inside the () is the correct syntax? - - - for a string?

What would it be if the variable were a long integer?

Is the syntax different for an integer?
 
Last edited:

Micron

AWF VIP
Local time
Today, 02:27
Joined
Oct 20, 2018
Messages
3,478
Public variables are available to other forms and reports. They are exposed as members of the object. Like any other member they just need the reference qualified.
Code:
Forms!formname.publicvariablename
Must be a version thing then. I put
Option Compare Database
Option Explicit
Dim pubPublic As Variant

Didn't work.

(Galaxiom:Sorry I accidentally edited the post instead of replying and have tried to put it back.)
 
Last edited by a moderator:

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
The general module gets loaded the first time that anything in it gets referenced - even if the first thing is a function or subroutine. At the point of module "load" you will have automatic instantiation of all variables, public or private, in that module at their default values. Which means the numerics become 0, the strings become "", the objects become Nothing, and the the variants become Null.

Variants are instantiated as Empty rather than Null.
Code:
Dim x As Variant
 
? IsNull(x) ' returns False
? IsEmpty(x) ' returns True
 

Micron

AWF VIP
Local time
Today, 02:27
Joined
Oct 20, 2018
Messages
3,478
Not 100% sure, but I think variables are what are "instantiated" (Dim'd) and when they are assigned a value or object they are "initialized". I also think that some would argue that to instantiate is to create a class because of the jargon of other programming languages, so who knows for sure.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
Must be a version thing then. I put
Option Compare Database
Option Explicit
Dim pubPublic As Variant
The default scope of a module level variable in an object module is Private.

To make it public use:
Public pubPublic As Variant

The default is the other way in a Standard Module where module level variables are Public by default but can be declared Private if desired.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
Not 100% sure, but I think variables are what are "instantiated" (Dim'd) and when they are assigned a value or object they are "initialized". I also think that some would argue that to instantiate is to create a class because of the jargon of other programming languages, so who knows for sure.

I think Initialized means that they are allocated memory, so that is when the module loads. eg A Variant is initialized as Empty.

"Instantiated" is normally used when an "instance" of a class is created. (Note the similarity of the words.)
 

Micron

AWF VIP
Local time
Today, 02:27
Joined
Oct 20, 2018
Messages
3,478
re the variable thing - I see what I missed. Never tried that in all my years as I've always considered it to be less than desirable practice. As for the other,
I think Initialized means that they are allocated memory,
what I'm reading here leads me to think otherwise, at least for Access. He seems to make the distinction between "declaring" and "initializing".
http://www.allenbrowne.com/vba-NothingEmpty.html

I guess it doesn't really matter as I suppose I'd understand the intent regardless of which word somebody wrote. Certainly have no qualms about the variant thing.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:27
Joined
Feb 28, 2001
Messages
27,156
Interesting. I could have sworn that they would be Null. But that intrigued me so much that I decided to look up some things.

In Object Browser, if you search appropriately, you find a definition group called VBVarType which is an enumeration of possible values of the datatype of a Variant. You figure that for the initial value of something that hasn't otherwise been set up with a specific default type or value, you would have a ZERO (numeric) in the slot that defines its type, whatever that zero means. And for VBVarType, that code translates to vbEmpty. I.e. the Empty type. (I guess analogous to C's VOID type?)

So I looked up IsEmpty(x) and determined that the ONLY time it is supposed to return TRUE is if the variant has never been initialized. Anyone who has been using IsEmpty() to test for an empty string has been using the wrong test. For a string variable that contains "" (the empty string), IsEmpty should NOT return TRUE. And it doesn't.

Thanks for point that out, G.
 

Micron

AWF VIP
Local time
Today, 02:27
Joined
Oct 20, 2018
Messages
3,478
Interesting. I've never used IsEmpty on a string variable, always believing it was for object variables only. As AB says, an uninitialized variable that is of the variant type is equal to an empty string and also 0. A value that is both "" and 0 is Empty.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 16:27
Joined
Jan 20, 2009
Messages
12,851
re the variable thing - I see what I missed. Never tried that in all my years as I've always considered it to be less than desirable practice.

Agreed. If you going to have a member of an object exposed then far better done using a Property where the nature of allowed values can be controlled.
 

Users who are viewing this thread

Top Bottom