access全局变量_在MS Access中使用“全局”变量


As developers, we frequently need to store values for use throughout our application or to pass values from one object (form, report, module) to another.  There are a number of ways to do this within Access and I'll discuss several of the more common methods in this article.

作为开发人员,我们经常需要存储值以在整个应用程序中使用,或将值从一个对象(窗体,报表,模块)传递到另一个对象。 在Access中有许多方法可以做到这一点,我将在本文中讨论几种较常见的方法。

Caveat Emptor: Before delving into this discussion, I want to address a common misconception that "global variables" are bad.  I strongly disagree with this view and regularly use them in my database applications.  The key is that you must be cautious about how you use these variables and understand that changing the variable in one spot will affect other aspects of your application.  

告诫Emptor:在深入讨论之前,我想解决一个普遍的误解,即“全局变量”是不好的。 我强烈不同意这种观点,并定期在我的数据库应用程序中使用它们。 关键是您必须谨慎使用这些变量,并了解在一处更改变量会影响应用程序的其他方面。

For example: You use a global variable as a criteria in a recordset (say the RecordSource of a form).  While that form is open, you somehow navigate to another form or place in your code where the value of that variable is set to a new value.  When you return to the original form, the value of the variable has been reset, but the RecordSource of the form has not been re-queried, so the records that are displayed are not indicative of the current value of the variable.  For this reason, you must be cautious when using "global variables"

例如:您将全局变量用作记录集中的条件(例如表单的RecordSource)。 打开该表单后,您将以某种方式导航到代码中该变量的值设置为新值的另一种表单或位置。 当您返回到原始窗体时,该变量的值已被重置,但是尚未重新查询该窗体的RecordSource,因此显示的记录并不表示该变量的当前值。 因此,使用“全局变量”时必须谨慎

1.  Global Variables: I frequently encounter databases where the developer has chosen to use this technique and has simply defined one or more variables as global in a declaration statement.  This is by far the easiest way to define a variable which can be used throughout your application.  Simply define the variable in a code modules declaration section (as below).

1.全局变量:我经常遇到数据库,开发人员选择使用这种技术,并在声明语句中简单地将一个或多个变量定义为全局变量。 到目前为止,这是定义可在整个应用程序中使用的变量的最简单方法。 只需在代码模块声明部分中定义变量(如下所示)。

Global lngMyID as long 

This variable can then be referenced in all code modules throughout your application.  Although this value can be set and evaluated from within any code module or the VBA immediate window, it cannot be used directly in a query or as a value in a control source of a control on a form.  However, you can overcome this drawback by defining a set of procedures to set and get the value of these variables:

然后可以在整个应用程序的所有代码模块中引用此变量。 尽管可以从任何代码模块或VBA立即窗口中设置和评估该值,但不能将其直接用于查询或用作窗体上控件的控件源中的值。 但是,您可以通过定义一组过程来设置和获取这些变量的值来克服此缺点:

Public Sub SetMyID (VariableValue as long)
    lngMyID = VariableValue
End Sub
Public Function GetMyID() as Long
    GetMyID = lngMyID
End Function 

An additional drawback of this technique is that variables defined using this method will lose their values when an unhandled error is encountered in an application.  Regardless of how hard we try, most of us fail to include error handling in every procedure in our applications, and if an error occurs in one of these procedures, the variable will lose its value.  When this happens it can cause a myriad of problems that cascade through your application.  For this reaso

  • 0
  • 4
    觉得还不错? 一键收藏
  • 0


  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助




当前余额3.43前往充值 >
领取后你会自动成为博主和红包主的粉丝 规则
钱包余额 0