防止全局变量被重置 [英] Preventing Global variables from being reset

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

问题描述

大家好,


我目前需要在整个应用程序中存储3个变量用作全局变量。我需要记录用户名,他们的员工组和他们正在做的任务。例如,用户名被输入到表格中的字段中,以指示最后一个人在该特定记录上工作。我已经创建了下面的代码,它似乎工作正常,除非应用程序偶尔会出错,此时全局变量会丢失它们的值。有没有办法防止全局变量被重置,直到数据库关闭?我在网上研究过,但还没找到我想要的东西。此外,只是好奇,但在多用户环境中,这些全局变量显然必须是特定机器的本地变量,这样如果两个人同时登录,则他们都有自己的名称存储在他们自己的全局变量中。这是真的?如果是这样,它们是否存储在每台本地机器的内存中?


下面的代码在我的Globals模块中

展开 | 选择 | Wrap | 行号

解决方案

嗨咆哮。如果遇到未捕获的代码错误,全局变量值本质上是不稳定的,因为在这些情况下VBA代码被重置并且它们的值丢失。当然,所有VBA代码都应该有适当的错误处理程序来防止这种中断。


此外,在不同PC上运行的Access实例之间不共享全局变量 - 它们纯粹是当前的本地实例,因为它们是在活动PC的内存中设置和读取的。


最简单的方法是在Access表中存储需要引用的共享值。诸如用户名之类的本地值可以保持全局,但是如果要将某些值写入引用表,则可以将它们全部写为一样简单。


-Stewart


我可能错了,但我相信只要访问遇到错误而没有任何形式的错误处理,所有全局变量都会被重置。如果你想避免这个问题,你将不得不在你的所有潜艇中实现错误处理例程。


哎呀太慢了。我真的要努力提高打字速度。 ;)

Hi All,

I currently need to store 3 variables to be used as global variables throughout my application. I need to record the username, their employee group and the task they are doing. The username, for example, is entered into a field on a table to indicate who last worked on that particular record. I have created the code below and it seems to work fine except when the application occasionally errors out at which point the global variables lose their values. Is there a way to prevent the global variables from being reset until the database is closed? I''ve researched online but haven''t found what I''m looking for yet. Also, just curious, but in a multi-user environment these global variables must obviously be local to a particular machine so that if two people log in at the same time they both have their own name stored in their own global variable. Is this true? If so, are these stored within the memory of each local machine?

The code below is in my Globals module

Expand|Select|Wrap|Line Numbers

解决方案

Hi ramprat. Global variable values are inherently volatile if faced with untrapped code errors, as in these circumstances the VBA code is reset and their values are lost. Of course, all VBA code should have appropriate error handlers to prevent such interruptions.

Further, globals are not shared between instances of Access running on different PCs - they are purely local to the current instance as they are set and read within the active PC''s memory.

Simplest thing to do is to store the shared values you need to refer to in an Access table instead. Local values such as username could remain global, but if you are writing some values to a reference table it may be as simple to write them all.

-Stewart


I could be wrong on this, but I believe that as soon as access encounters an errors without any form of error handling all the global variables get reset. If you want to avoid this issue you are going to have to implement error handling routines in all of your subs.


Oops too slow again. I realy have to work on my typing speed. ;)


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

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