使用AHK关闭Access应用程序时未设置对象变量(错误91)

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

我有一个名为“Form1”的Access窗体,并且在窗体中有几个子窗体,如Form_Load,Form_Unload等。

我想使用AutoHotKey脚本来控制窗体的打开和关闭。

表格开头代码:

acc:= ComObjCreate("Access.Application")
acc.OpenCurrentDatabase("\\...\target_database.accdb")
acc.Visible := True
acc.UserControl := True
acc.DoCmd.OpenForm("Form1")

表格结束代码:

acc.quit

错误91

在尝试关闭应用程序时弹出。而错误似乎与Form_Unload子有关。它只发生在我使用AutoHotKey时,我能够手动关闭Access而没有任何问题。

这是Form_Unload的代码:

Private Sub Form_Unload(Cancel As Integer)
' update the data in txt, signal and datetime
Dim fso As New FileSystemObject
'the file we're going to write to
Dim ts As TextStream
'open this file to write to it
Set ts = fso.CreateTextFile("\\cp-apps01\ExtruderDataCollector\TESTING\log.txt", True)
For Each Key In Module1.dict_pre_Signal.Keys
    ts.WriteLine (Key & " " & Module1.dict_pre_Signal.Item(Key) & " " _
        & DateValue(CStr(Module1.dict_pre_Time.Item(Key))) & " " _
        & TimeValue(CStr(Module1.dict_pre_Time.Item(Key))))
Next Key
ts.Close
Set fso = Nothing
End Sub

在关闭访问应用程序之前,有没有办法可以关闭表单或将表单更改为设计视图?

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

关闭表格怎么样?

acc.DoCmd.OpenForm("Form1")
acc.DoCmd.Close(acForm, "Form1", acSaveNo)
acc.Quit

或者使用常量的数值:

acc.DoCmd.OpenForm("Form1")
acc.DoCmd.Close(2, "Form1", 2)
acc.Quit
© www.soinside.com 2019 - 2024. All rights reserved.