Code Samples for Businesses, Schools & Developers



First Published 30 Jan 2023

This article was triggered by a question at AccessForums.net: Clear the immediate window in VBA

There is no built-in menu item or VBA command to do this. However, various methods do exist

1.   Use SendKeys

      This is the simplest method. However, most developers avoid the use of SendKeys as working code can break with version updates

VBA.Interaction.SendKeys "^g ^a {DEL}"


      Or just

SendKeys "^g ^a {DEL}"



2.   Loop through and clear manually

      The immediate window holds a maximum of 200 lines of text so loop through and replace each line with an empty string

Sub ClearImmediate()

      Dim i As Integer
      For i = 0 To 200
            Debug.Print ""
      Next i

End Sub


      NOTE: This leaves the cursor 200 lines down in the immediate window

3.   Use VBA code
      Adapted from code by ProfoundlyOblivious which I found at Use VBA to Clear Immediate Window?

      Copy this to a standard module

Option Compare Database
Option Explicit

#If VBA7 Then
      Private Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
      Private Declare PtrSafe Function FindWindowExA Lib "user32" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, _
            ByVal lpsz2 As String) As LongPtr
      Private Declare PtrSafe Function PostMessageA Lib "user32" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
            ByVal lParam As LongPtr) As Long
      Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, _
            ByVal dwExtraInfo As LongPtr)
#Else Then
      Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
      Private Declare Function FindWindowExA Lib "user32" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
            ByVal lpsz2 As String) As Long
      Private Declare Function PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
            ByVal lParam As Long) As Long
      Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If

Private Const WM_ACTIVATE As Long = &H6
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_CONTROL = &H11

'=========================================

Public Sub ClearImmediateWindow()

'requires the reference VBA Extensibility

#If VBA7 Then
      Dim hwndVBE As LongPtr
      Dim hwndImmediate As LongPtr
#Else
      Dim hwndVBE As Long
      Dim hwndImmediate As Long
#End If

      Dim vbWin As VBIDE.Window
      Dim strCaption As String

      'get the caption for the Immediate window
      For Each vbWin In Application.VBE.Windows
            If vbWin.type = vbext_wt_Immediate Then
                  strCaption = vbWin.Caption 'e.g. Immediate in English, Direct in Dutch(etc)
            End If
      Next

      hwndVBE = FindWindowA("wndclass_desked_gsk", vbNullString)
      hwndImmediate = FindWindowExA(hwndVBE, ByVal 0&, "VbaWindow", strCaption)
      PostMessageA hwndImmediate, WM_ACTIVATE, 1, 0&

      keybd_event VK_CONTROL, 0, 0, 0
      keybd_event vbKeyA, 0, 0, 0
      keybd_event vbKeyA, 0, KEYEVENTF_KEYUP, 0
      keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0

      keybd_event vbKeyDelete, 0, 0, 0
      keybd_event vbKeyDelete, 0, KEYEVENTF_KEYUP, 0
End Sub



4.   Use an Access VBE add-in

      Some Access utilities include this functionality. For example:

      a)   IDBE Tools 2010     (free)
      b)   MZ Tools     (commercial)



5.   Feedback

      Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

      Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington                       Mendip Data Systems                       1 Feb 2023



Return to Code Samples Page




Return to Top