我用: - 赢7x64; - 访问 - 2016;
我尝试使用以下代码解决我的问题。 Form1中
Option Compare Database
Option Explicit
Public statusBool As Boolean
Public numProc As Integer
' `Button pressed`.
Private Sub btnStart_Click()
numProc = 0
statusBool = True
Call Process(statusBool, numProc)
End Sub
' Process
Public Sub Process(statusBool As Boolean, numProc As Integer)
If statusBool = True Then
Me.txtProcessFrm = "ProcessNum - " & numProc + 1
Call SleepFor(1000) '1 seconds delay
Call Process(statusBool, numProc)
End If
End Sub
'
Private Sub btnStart_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
statusBool = False
numProc = 0
Call Process(statusBool, numProc)
End Sub
Public Sub SleepFor(ByVal MilliSeconds As Long)
Sleep MilliSeconds
End Sub
模块1
Option Compare Database
Option Explicit
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
题 我的解决方案对于此任务是否正确,还是有更简单的方法来解决此问题?
Update_1
代码无法启动。
我收到错误Sub or Function not defined
。
Update_2
模块Module1
。
将Private
替换为Public
。
它是
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
它成了
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
问题。
当我单击按钮btnStart_Click
时,文件挂起
Update_3 修改过程(statusBool为布尔值,numProc为整数) 它成了。
' Process
Public Sub Process(statusBool As Boolean, numProc As Integer)
If statusBool = True Then
Do
Sleep 1000
DoEvents
Loop Until Me.txtProcessFrm = "ProcessNum - " & numProc + 1
Call Process(statusBool, numProc)
End If
End Sub
问题。 似乎暂停工作,但逻辑本身不起作用。 换句话说,文本字段未填充文本。 如果松开按钮,循环将继续工作。
单击按钮的事件链如下所示。
MouseDown → MouseUp → Click → DblClick → Click
在你的代码中,循环永远不会停止,因为你的statusBool将永远是真的,导致无限循环,这可能就是为什么即使你释放了mosue它也会挂起。
你可以试试这个鼠标=>鼠标向上:
Private Sub btnStart_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
statusBool = True
Call Process(0)
End Sub
Private Sub btnStart_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
statusBool = False
End Sub
Public Sub Process(numProc As Integer)
If statusBool Then
numProc = numProc + 1
Me.txtProcessFrm = "ProcessNum - " & numProc
Sleep 1000
DoEvents
Call Process(numProc)
End If
End Sub
更多这里:https://docs.microsoft.com/en-us/office/vba/api/access.commandbutton.click