Skip to content

Instantly share code, notes, and snippets.

@kevinoid
Last active October 5, 2020 13:44
Show Gist options
  • Save kevinoid/5885c2ec34d0ebfbe02dc7bb921ee9a6 to your computer and use it in GitHub Desktop.
Save kevinoid/5885c2ec34d0ebfbe02dc7bb921ee9a6 to your computer and use it in GitHub Desktop.
AutoHotkey script to dismiss (i.e. cancel) the Microsoft Access "Select Unique Record Identifier" dialog when linking a table or view without a primary key.
; AutoHotkey script to dismiss the "Select Unique Record Identifier" dialog
; when linking a table or view without a primary key.
;
; Written in 2020 by Kevin Locke <kevin@kevinlocke.name>
; Based on https://www.autohotkey.com/boards/viewtopic.php?p=295064#p295064
;
; To the extent possible under law, the author(s) have dedicated all copyright
; and related and neighboring rights to this software to the public domain
; worldwide. This software is distributed without any warranty.
;
; See https://creativecommons.org/publicdomain/zero/1.0/
#NoEnv
#Persistent
#Warn
SetBatchLines, -1 ; Run this script at max speed
WinGet, pidAccess, PID, ahk_exe MSACCESS.EXE
If (not pidAccess) {
MsgBox, 0x10, , Error: Unable to find Access window.
ExitApp, 1
}
DetectHiddenWindows, On ; Match created window before it is shown
; https://docs.microsoft.com/en-us/windows/win32/winauto/event-constants
EVENT_OBJECT_CREATE := 0x8000
; Note: Could use 0 for pidAccess to hook all processes (current and future)
hHook := SetWinEventHook( EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, 0, RegisterCallback("HookProc", "F"), pidAccess, 0, 0 )
OnExit( Func("FreeResource").Bind(hHook) )
Return
FreeResource(hHook) {
DllCall("UnhookWindowsHookEx", "Ptr", hHook)
}
HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
; https://docs.microsoft.com/en-us/windows/win32/winauto/object-identifiers
static OBJID_WINDOW := 0
If (idObject != OBJID_WINDOW)
Return
WinGetTitle, strTitle, ahk_id %hwnd%
If (strTitle = "Select Unique Record Identifier")
WinClose, ahk_id %hwnd%
}
; https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook
SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
return DllCall("SetWinEventHook"
, "UInt", eventMin
, "UInt", eventMax
, "Ptr", hmodWinEventProc
, "Ptr", lpfnWinEventProc
, "UInt", idProcess
, "UInt", idThread
, "UInt", dwFlags
, "Ptr")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment