按下按钮时如何使进程运行?

问题描述 投票:-1回答:1

我用: - 赢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)

题 我的解决方案对于此任务是否正确,还是有更简单的方法来解决此问题?

enter image description here

Update_1 代码无法启动。 我收到错误Sub or Function not definedenter image description here

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

问题。 似乎暂停工作,但逻辑本身不起作用。 换句话说,文本字段未填充文本。 如果松开按钮,循环将继续工作。

vba ms-access access-vba
1个回答
1
投票

单击按钮的事件链如下所示。

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

© www.soinside.com 2019 - 2024. All rights reserved.