简体   繁体   English

VBA访问循环代码不起作用

[英]VBA Access Loop Code not working

Can anyone help?? 有人可以帮忙吗?

This code isn't starting when it should and isn't stopping when it should and I can't figure out why. 这段代码没有在适当的时候开始,并且在适当的时候没有停止,我不知道为什么。

Private Sub Condition1_Click()

Dim Condition1 As Boolean

Do

DoCmd.OpenQuery "qGBX2b"
DoCmd.OpenQuery "qGBX2c"
DoCmd.OpenQuery "qGBX2d"
CurrentDb.Execute "ALTER TABLE GBX2Temp ALTER COLUMN Line COUNTER(1,1)"
DoCmd.Requery

Loop Until Condition1 = True

MsgBox "Done", vbDefaultButton1, "Done"
End Sub

It is run from a form, where a field is counting down and Condition1 is a field that changes to TRUE once it reaches a certain value. 它从一个表单开始运行,其中一个字段在递减,而Condition1是一个一旦达到某个值便变为TRUE的字段。

As the code is above, it runs once and does not loop whilst the value of Condition1 is FALSE. 如上面的代码所示,当Condition1的值为FALSE时,它将运行一次并且不会循环。 If I change the Loop criteria to “Loop Until Condition1 = False”, it runs whilst the value is FALSE, but does not stop when it changes to True. 如果将“循环”条件更改为“直到条件1 = False,直到循环”,它会在值为FALSE时运行,但当其变为True时不会停止。

Where am I going wrong? 我要去哪里错了?

You seem to be experiencing a problem with scope. 您似乎遇到范围问题。 When you issue the DIM statement, that variable name overrides all other field or control names that might exist unless you specifically designate the scope (such was with me.Condition1 ) 发出DIM语句时,该变量名将覆盖可能存在的所有其他字段或控件名,除非您专门指定范围(例如me.Condition1 )。

If you have a field or textbox named Condition1 , you can simply remove the dim statement and it will use the value of that control or field instead. 如果您有一个名为Condition1的字段或文本框,则只需删除dim语句,它将使用该控件或字段的值代替。 I recommend that you rename textboxes to something other than the field name... simply for the fact that it's completely unambiguous which value you are referring to. 我建议您将文本框重命名为字段名称以外的其他名称……仅是因为它所指的是完全明确的事实。 So if your Field name is Condition1 , then name your text boxes something like txtCondition1 . 因此,如果您的字段名称为Condition1 ,则将文本框命名为txtCondition1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM