每30秒运行一次VBA功能(从启动)(访问)

时间:2018-04-04 13:43:59

标签: vba ms-access access-vba

所以我的代码应该在我启动访问数据库时执行 (它从文件夹导入文件)

ShowDialog

我的想法是在启动时运行的宏然后打开函数并使用

Function import_files
'all the code (works without errors)
end function

corse的功能不起作用,因为它与访问不兼容......所以你可以像这样使用它:

Application.OnTime Now + TimeValue("00:00:30"), "import_files"

只是从那时起你无法运行任何东西......

是否有任何简单的简短解决方案

像这样基本的

Excel.Application.OnTime Now + TimeValue("00:00:30"), "import_files"

感谢您的帮助! (我在VBA中表现不佳:))

2 个答案:

答案 0 :(得分:3)

您可以考虑使用 Form的TimerInterval属性。您可以打开一个表单(隐藏)并让计时器触发代码。

示例
以下示例显示如何通过在按钮上显示和隐藏图标来在窗体上创建闪烁按钮。 表单的Load事件过程将表单的TimerInterval属性设置为1000,因此图标显示每秒切换一次。

Sub Form_Load() 
    Me.TimerInterval = 1000 
End Sub 

Sub Form_Timer() 
    Static intShowPicture As Integer 
    If intShowPicture Then 
        ' Show icon. 
        Me!btnPicture.Picture = "C:\Icons\Flash.ico" 
    Else 
        ' Don't show icon. 
        Me!btnPicture.Picture = "" 
    End If 
    intShowPicture = Not intShowPicture 
End Sub

要使用此示例代码:
注意:根据您使用的版本,您的屏幕会有所不同。
你可以谷歌:访问me.timerinterval例子。

  1. 单击选项|启用宏启用此内容|行。enter image description here enter image description here
  2. 在设计视图中创建新的空白表单和视图。enter image description here enter image description here
  3. 添加命令按钮|取消向导|删除标题|将其命名为btnPicture。enter image description here enter image description here enter image description here enter image description here

  4. 添加表格事件程序 A.取消选择按钮并选择表单属性 B.在属性表中单击表单的事件加载器 C.双击Code Builder D.选择全部,然后从此处粘贴代码。 enter image description here

  5. enter image description here



    <强>供参考:
    对于代码mmehta做了;你会把它放在一个模块中。
    他向您展示了如何延续您的思考过程 你建议:

    Function Import_files
        do every 30 seconds 
        run code
        if needed reset any variables 
    
            myVariableInteger = 0
            myVariableString = ""
            myVariableString= vbNullString
            myVariableInteger = Null
            Set myVariableObject = Nothing
    
        loop
    end function
    

    他详细说明:

    Function Import_files
      Do
        Pause(30)
        run code (You're code here or better yet put your code in a sub routine and call the routine.)
      Loop
    End Function
    
    1. 将此代码放入模块

      Option Compare Database
      Option Explicit
      
      Public Function Pause(NumberOfSeconds As Variant)
      On Error GoTo Error_GoTo
      
      Dim PauseTime As Variant
      Dim Start As Variant
      Dim Elapsed As Variant
      
      PauseTime = NumberOfSeconds
      Start = Timer
      Elapsed = 0
      Do While Timer < Start + PauseTime
          Elapsed = Elapsed + 1
          If Timer = 0 Then
              ' Crossing midnight
              PauseTime = PauseTime - Elapsed
              Start = 0
              Elapsed = 0
          End If
          DoEvents
      Loop
      
      Exit_GoTo:
          On Error GoTo 0
          Exit Function
      Error_GoTo:
          Debug.Print Err.Number, Err.Description, Erl
          GoTo Exit_GoTo
      End Function
      
      Sub msgUser()
          Dim x As Integer
          x = MsgBox("Click 'OK' to continue.", vbOKOnly, "Ready?")
      End Sub
      
    2. 将光标放在常规暂停中,然后按播放。enter image description here

    3. 观看msgBox每30秒弹出一次。

答案 1 :(得分:0)

尝试使用以下函数进行计时器。

Function Import_files
      Do
            Pause(30)
            run code
      Loop
end function

Public Function Pause(NumberOfSeconds As Variant)
    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim Start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Elapsed = 0
    Do While Timer < Start + PauseTime
        Elapsed = Elapsed + 1
        If Timer = 0 Then
            ' Crossing midnight
            PauseTime = PauseTime - Elapsed
            Start = 0
            Elapsed = 0
        End If
        DoEvents
    Loop

Exit_GoTo:
    On Error GoTo 0
    Exit Function
Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function