Compile Error: Sub or Function Not Defined (1 Viewer)

GigTu

New member
Local time
Today, 15:14
Joined
Aug 9, 2018
Messages
4
Hello. When I debug I get a pop up saying the sub or function is not defined. It also highlights the line item (apiShowWindow) that seems to be creating the issue. I cannot see anything wrong with the code. Can you please help me figure out how to correct this?
Code:
Function fSetAccessWindow(nCmdShow As Long)
     
    Dim loX As Long
    Dim loForm As Form
    On Error Resume Next
    Set loForm = Screen.ActiveForm
     
    If Err <> 0 Then
        loX = [COLOR=Red]apiShowWindow[/COLOR](hWndAccessApp, nCmdShow)
        Err.Clear
    End If
     
    If nCmdShow = SW_SHOWMINIMIZED And loForm.Modal = True Then
        MsgBox "Cannot minimize Access with " _
        & (loForm.Caption + " ") _
        & "form on screen"
    ElseIf nCmdShow = SW_HIDE And loForm.PopUp <> True Then
        MsgBox "Cannot hide Access with " _
        & (loForm.Caption + " ") _
        & "form on screen"
    Else
        loX = apiShowWindow(hWndAccessApp, nCmdShow)
    End If
    fSetAccessWindow = (loX <> 0)
End Function
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:14
Joined
Jan 23, 2006
Messages
15,364
Where is apiShowWindow defined?

Did you write this code?

With access vba "&" is the concatenation operator, you are using "+" which makes me ask if you are familiar with vba?

Update: I did see the function and related, required code here.

You may also find this thread and dialog interesting.
 
Last edited:

GigTu

New member
Local time
Today, 15:14
Joined
Aug 9, 2018
Messages
4
I did not write it. I will go read your links. Thanks.
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 09:14
Joined
Jan 20, 2009
Messages
12,849
With access vba "&" is the concatenation operator, you are using "+"

+ will also concatenate if used with non numeric values, but unlike &, any Null will propagate resulting in a Null output if either expression is Null.

Code:
"Cannot minimize Access with " _
        & (loForm.Caption + " ") _
        & "form on screen"

I suspect this code may have been an attempt to use Null propagation to skip the extra space if the form has no caption property. Otherwise why not include the space in the quoted string?

However this would not work because an empty Caption property is a NullString rather than a Null. The following would work with Null propagation but there are more obvious ways to achieve it.


Code:
"Cannot minimize Access with " _
        & (Iif(loForm.Caption = vbNullString, Null, loForm.Caption) + " ") _
        & "form on screen"
 

isladogs

MVP / VIP
Local time
Today, 22:14
Joined
Jan 14, 2017
Messages
18,186
Here is some similar code by the same author that works for me.
Copy everything into a standard module

Code:
Option Compare Database
Option Explicit

'************ Code Start **********
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3

'###############################################
#If VBA7 Then
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#ElseIf Win64 Then 'need datatype LongPtr
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As LongPtr, ByVal nCmdShow As LongPtr) As LongPtr
#Else '32-bit Office
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
#End If
'###############################################

Function SetAccessWindow(nCmdShow As Long)

    'Usage Examples
    'Maximize window:
    ' ?SetAccessWindow(SW_SHOWMAXIMIZED)
    'Minimize window:
    ' ?SetAccessWindow(SW_SHOWMINIMIZED)
    'Hide window:
    ' ?SetAccessWindow(SW_HIDE)
    'Normal window:
    ' ?SetAccessWindow(SW_SHOWNORMAL)
    
    Dim loX As Long
   ' Dim loForm As Form
    On Error Resume Next
    
    loX = apiShowWindow(hWndAccessApp, nCmdShow)
    SetAccessWindow = (loX <> 0)

End Function

To use in your apps, just use one line of code such as SetAccessWindow(SW_SHOWMINIMIZED)
 

GigTu

New member
Local time
Today, 15:14
Joined
Aug 9, 2018
Messages
4
ridders,

By taking it out of the objects folder and writing in into a module, it works. However, now the component following it no longer works. Basically, that sets the background, and next the login in screen should pop up. However, I receive the following error: "Compile error: Variable not defined." for the part in red. Is that an easy fix? It all worked before, so I am not sure what changed to make it suddenly start throwing errors.

I am new to SQL so please bear with me and thank you in advance for your patience.

Code:
Private Sub Form_Load()
DoCmd.Maximize
[COLOR=Red]stDocName =[/COLOR] "frmLogin"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub
 

GigTu

New member
Local time
Today, 15:14
Joined
Aug 9, 2018
Messages
4
Nevermind. I removed Option Explicit and it now works fine. Thank you!!!! :)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:14
Joined
May 21, 2018
Messages
8,463
Code:
Option Explicit and it now works fine
That is really such a bad idea. Simply dimension your variables
Code:
Private Sub Form_Load()
   [B]Dim stDocName as string[/B]
   DoCmd.Maximize
   stDocName = "frmLogin"
   DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub
 

isladogs

MVP / VIP
Local time
Today, 22:14
Joined
Jan 14, 2017
Messages
18,186
Agree with MajP.
Using Option Explicit is important in helping ensure apps run correctly by requiring all variables to be explicitly declared.

As an alternative to what MajP wrote, this will also work.

Code:
Private Sub Form_Load()
DoCmd.Maximize
DoCmd.OpenForm "frmLogin"
End Sub

This approach means the stDocName variable isn't needed.
I've also omitted stLinkCriteria as it's not being used in this case
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:14
Joined
Feb 28, 2001
Messages
27,001
By taking it out of the objects folder and writing in into a module, it works. However, now the component following it no longer works.

This sounds like something else is improperly terminated ABOVE the place from which you moved the code. I.e. something that appeared before this code in its original place was breaking your subroutine, so when you moved it to a module, that "something" broke the next thing in sequence.
 

Users who are viewing this thread

Top Bottom