Public Sub EnsureLoad(ByRef F As Variant, ByVal bUnloadInvisible As Boolean)
' Trap and memory errors while loading a form or array element ' If an error occurs, ask the user to close a program before continuing ' Also unload any INVISIBLE forms if bUnloadInvisible is true Dim bContinue As Boolean Dim iFormCount As Integer Dim I As Integer bContinue = True Do On Error Resume Next Load F Select Case Err.Number Case 0 bContinue = False Case 7 MsgBox "<YOUR VB APP NAME needs more memory in order to _ continue running." + vbCrLf + vbCrLf + _ "Close some of your other applications then click OK to try to continue", _ vbCritical, "Memory Allocation Error" Case Else MsgBox "<YOUR VB APP Name encountered an unexpected error while loading _ a window." + vbCrLf + vbCrLf + _ "Error Number #" + CStr(Err.Number) + vbCrLf + _ Err.Description + vbCrLf + vbCrLf + _ "Close some of your other applications then click OK to try to continue", _ vbCritical, "Memory Allocation Error" End Select If bContinue And bUnloadInvisible Then ' try to free (unload) forms that are not visible iFormCount = Forms.Count - 1 For I = iFormCount To 0 Step -1 If Forms(I).Visible = False Then On Error Resume Next Unload Forms(I) Set Forms(I) = Nothing End If Next I End If Loop Until bContinue = False End Sub I have written a fairly large program for my company in VB 4 (32 bit). EXE over 2 Meg. Lately I was getting a lot of memory errors duing forms loading. The following code does not solve this, but keeps my app from crashing. I found his useful as should others. |