Access Query中的全局变量 [英] Global Variables in Access Query

查看:150
本文介绍了Access Query中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在访问查询中使用全局变量作为标准。

在我第一次尝试这样做时我写了一个看起来像这样的函数


公共函数GloVar(ByVal VarName)字符串


GloVar = VarName


结束函数


它没用。

它计算变量名的文本而不是变量的值。 Additionaly访问强制VarName在引号中。如果我输入GloVar(JobNo),它会自动更改为GloVar(JobNo)。


我读了大约一年前的帖子,建议对每个变量使用If语句。即


如果VarName =" JobNo"然后

GloVar = JobNo


我(作为原始海报)希望有一个更通用的功能,可以检索输入的任何变量的值为每个变量引入一个if语句。


任何帮助都将不胜感激。


Megan

I would like to use a Global variable as a criteria in an access query.
in my first attempt to do so I wrote a Function that looks like this

Public Function GloVar(ByVal VarName) As String

GloVar = VarName

End Function

It Didn''t Work.
It evaluates to the text of the variable name not the value of the variable. Additionaly access forces the VarName to be in quotes. If I Type GloVar(JobNo) it automatically changes to GloVar("JobNo").

I read a post from about a year ago that suggested using an If statement for each variable. i.e.

If VarName = "JobNo" Then
GloVar = JobNo

I (as the original poster) was hoping for a more universal function that would retrieve the value of whatever variable was entered without have to introduce an if statement for every variable.

Any help will be appreciated.

Megan

推荐答案

我认为您希望函数(GloVar)返回变量的值(内容),其名称(在源代码中)与VarName中传递的字符串相匹配吗?

基本上这是不可能的,因为编译器在代码运行时没有运行,因此无法访问源代码。

奇怪的是,VBA提供了一种方式(Eval()函数)你可以通过这种方式间接调用函数,但不能访问变量。


有趣的问题。
Am I right in thinking that you want a function (GloVar) to return the value (contents) of a variable, whose name (in the source code) matches the string passed in VarName?
Essentially this is impossible as the compiler is not running when the code is, so there is no access to the source code.
Strangely, VBA DOES provide a way (Eval() function) whereby you can call a function ''indirectly'' in this way, but not to access a variable.

Interesting question.


你明白了很好,我想做什么。


特别是我想要完成的事情是这样的。


当通过创建新工作表单创建工作时全局变量JobNo被分配了作业号。传递给后续表格,即创建采购订单。


我希望能够将该变量用作查询中的条件。我试图在我的数据库中消除一些庞大的查询和代码,这似乎是最简单的解决方案,因为全局变量可以在作业创建过程中打开和关闭表单时保持其值,而不需要表级别的标记。


我目前正在使用看起来像这样的函数


Public Function GloVar(ByVal VarName)As String



Select Case VarName


Case" CurEmp"

GloVar = CurEmp


案例CurEquip

GloVar = CurEquip


等等...每个变量


结束选择

结束功能

此方法要求将每个变量作为案例输入到函数中。我希望有一些更普遍的东西可以用于任何变量。


我很感激输入。


Megan
You understand pretty well what I want to do.

Specifically what I want to accomplish is this.

When a job is created via the "Create New Job Form" the global variable JobNo is assigned the Job Number. which is passed to subsequent forms i.e. "Create a purchase order".

I would like to be able to use the variable as criteria in a query. I am attempting to eleminate some of the bulky queries and code in my database and this seems like the simplest solution since the global variable can maintain its value while forms open and close during the job creation process and without the need for flags at the table level.

I am currently using a function that looks like this

Public Function GloVar(ByVal VarName) As String


Select Case VarName

Case "CurEmp"
GloVar = CurEmp

Case "CurEquip"
GloVar = CurEquip

Etc... for each variable

End Select
End Function

This method requires each variable be entered as a case into the function. I was hoping for something a little more universal that could be used with any variable.

I appreciate the input.

Megan


你可以做你正在做的事情已经或者替代地,为每个变量做一个函数。

正如我在上一篇文章中所解释的那样,VBA不支持你想做的事情(我解释说的原因)发布它没有意义。)
You can do what you''re doing already, or alternatively, do a function for each variable.
As I explained in the previous post though, what you''d like to do is not supported in VBA (for the reasons I explained in said post it wouldn''t make sense).


这篇关于Access Query中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆