vba 引用全局变量 - MS Access

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3266584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:38:59  来源:igfitidea点击:

Referencing global variable - MS Access

ms-accessvbams-access-2007

提问by noelmcg

Im trying to pass the name of a global variable to a sub routine and would like to know how to reference it. For example I could do the below with a control:

我试图将全局变量的名称传递给子例程,并想知道如何引用它。例如,我可以使用控件执行以下操作:

Private Sub passCtrlName(ctlName as String)
Me.Controls(ctlName) = "Whatever"
End Sub

Edit:

编辑:

For Example

例如

Public imGlobVar As String
Public Sub passGlobVar(frm as Form, ctlName as String, globVar as String)
frm.Controls(ctlName) = globVar
End sub

And call it as Private Sub imaButton_Click() imGlobVar = "something" Call passGlobVar(Me , txtBox1, imGlobVar) End sub

并将其称为 Private Sub imaButton_Click() imGlobVar = "something" Call passGlobVar(Me , txtBox1, imGlobVar) End sub

2nd Edit:

第二次编辑:

It seems that I could most definitely be barking up the wrong tree here, so I will explain what I'm trying to achieve.

似乎我很可能在这里吠错了树,所以我将解释我想要实现的目标。

I have a form that has textboxes for the users (risk) address, with a checkbox at the top that lets the user select that this address is the same as the 'contact' details already on the system, and the textboxes are locked.

我有一个包含用户(风险)地址文本框的表单,顶部有一个复选框,让用户选择该地址与系统上已有的“联系人”详细信息相同,并且文本框已锁定。

Populating the textboxes is fine and works. What I use the global variables for is to improve usability (albeit slightly). The user can add new details, and if they hit the checkbox 'make same as contact' the details that they have entered are stored in the global variables, one for each control.

填充文本框很好并且有效。我使用全局变量的目的是为了提高可用性(尽管是轻微的)。用户可以添加新的详细信息,如果他们点击复选框“与联系人相同”,他们输入的详细信息将存储在全局变量中,每个控件一个。

If the user has made a mistake by hitting the checkbox, they haven't lost these value, and by unchecking the box the entered values are returned. I hoped to create a sub routine where I could pass the name of the global variable and control and calling this routine, as opposed to writing it out for each control.

如果用户通过点击复选框犯了错误,他们并没有丢失这些值,并且通过取消选中该框,输入的值将被返回。我希望创建一个子例程,我可以在其中传递全局变量和控件的名称并调用此例程,而不是为每个控件写出它。

I have a feeling that I could be using the wrong technique to achieve my goals. But in answer to my original question, it appears that you can not pass global variables to sub routines in the manner that I wished.

我有一种感觉,我可能使用了错误的技术来实现我的目标。但是在回答我最初的问题时,您似乎无法以我希望的方式将全局变量传递给子例程。

采纳答案by BenV

If you're asking if you can dynamically reference global variables using a string containing the variable name the answer is no. You could use a single global array and pass the index, which would allow you to dynamically reference an element of the array.

如果您问是否可以使用包含变量名称的字符串动态引用全局变量,答案是否定的。您可以使用单个全局数组并传递索引,这将允许您动态引用数组的元素。

[Edit]
In response to the clarification in the question: You could just save the value of each control to its Tagproperty when the user checks the checkbox. Then, if the user unchecks the checkbox, you can just loop over your controls and assign the value from the Tagback to the Valueof the control.

[编辑]
为了回应问题中的澄清:Tag当用户选中复选框时,您可以将每个控件的值保存到其属性中。然后,如果用户取消选中该复选框,您可以循环遍历您的控件并将值从控件的Tag背面分配给Value控件。

回答by Fionnuala

You do not need to pass global variables, you can simply refer to them by name. Note that global variables are reset if an unhandled error occurs.

您不需要传递全局变量,只需按名称引用它们即可。请注意,如果发生未处理的错误,全局变量将被重置。

In http://msdn.microsoft.com/en-us/library/dd897495(office.12).aspxyou will find a section on Scope and Lifetime of Variables and Constants.

http://msdn.microsoft.com/en-us/library/dd897495(office.12).aspx 中,您将找到有关变量和常量的范围和生命周期的部分。

In a module:

在一个模块中:

Option Explicit 
Public glbVarName As String
Const MyConstant=123

Sub InitVar
   glbVarName="Something"
End Sub

Any other module, includeing a form's class module:

任何其他模块,包括表单的类模块:

Sub SomeSub
    MsgBox glbVarName
    SomeVar=2+MyConstant
End Sub

回答by HansUp

You could store the values from your controls in a Dictionary object, using the control names as the dictionary keys. Then you can retrieve the value for each control based on the control's name.

您可以将控件的值存储在 Dictionary 对象中,使用控件名称作为字典键。然后您可以根据控件的名称检索每个控件的值。

Option Compare Database
Option Explicit

Const cstrMyControls As String = "Text0,Text2,Text4,Text6"
Dim objDict As Object

Private Sub chkToggle_Click()
    If Me.chkToggle = True Then
        Call SaveValues
    Else
        Call RestoreValues
    End If
End Sub

Private Sub SaveValues()
    Dim varControls As Variant
    Dim i As Long

    Set objDict = Nothing 'discard previous saved values '
    Set objDict = CreateObject("Scripting.Dictionary")
    varControls = Split(cstrMyControls, ",")
    For i = 0 To UBound(varControls)
        objDict.Add varControls(i), Me.Controls(varControls(i)).Value
    Next i
End Sub

Private Sub RestoreValues()
    Dim varControls As Variant
    Dim i As Long
    If objDict Is Nothing Then
        'MsgBox "No values to restore." '
    Else
        varControls = objDict.keys()
        For i = 0 To UBound(varControls)
            Me.Controls(varControls(i)).Value = objDict(varControls(i))
        Next i
    End If
End Sub

回答by Norbert Wróblewski

I use additional field in table - name cancel - of course boolean - when i'm not sure if contents of fields will be valid I set it true. If this field will be true by the end - then I clean up (it may be all record or some fileds of course). Very easy.

我在表中使用附加字段 - 名称取消 - 当然是布尔值 - 当我不确定字段内容是否有效时,我将其设置为 true。如果该字段到最后为真 - 那么我会清理(当然可能是所有记录或某些文件)。好简单。