Is there a solution to getting sub forms fade in/out (1 Viewer)

AC_Alberta

New member
Local time
Today, 00:28
Joined
Mar 31, 2025
Messages
9
Hi, I'm after some help in getting the above to work..
Have no problem with parent forms fading in and out using the following:

Public FF1, FF2, FF3 As Long

FF1 = GetWindowLong(MyForm1.hWnd, GWL_EXSTYLE)
FF1 = FF1 Or WS_EX_LAYERED
SetWindowLong MyForm1.hWnd, GWL_EXSTYLE, FF1
SetWindowOpacity MyForm1.hWnd, 0, 0, LWA_ALPHA

MyForm1.Visible = True
Forms(Str1).Modal = True

For FF2 = 0 To 255 Step 10
SetWindowOpacity MyForm1.hWnd, 0, FF2, LWA_ALPHA
Sleep 2
DoEvents
Next FF2

FF1 = GetWindowLong(MyForm2.hWnd, GWL_EXSTYLE)
FF1 = FF1 Or WS_EX_LAYERED
SetWindowLong MyForm2.hWnd, GWL_EXSTYLE, FF1
SetWindowOpacity MyForm2.hWnd, 0, 0, LWA_ALPHA

For FF3 = 255 To 0 Step -10
SetWindowOpacity MyForm2.hWnd, 0, FF3, LWA_ALPHA
Sleep 2
DoEvents
Next FF3

However when it comes to two child forms overlayed and embedded in the parent form, with one visible = True and one one visible = False I get Error 438, Object doesn't support this property or method.
I can get them to work by toggling the visible property on each but I'm really after the fade effect.
Is there any other method or coding that will work for sub forms or do I have to suck it up?
Any suggestions would be appreciated.
PS - The coding above goes over my head, I just know it works.

Andy
 
you can only Fade (in/out) the main form and not the subforms.
 
If you want the fade effect, you would need to change the subforms to make them popup forms so they can be faded.
 
If you want the fade effect, you would need to change the subforms to make them popup forms so they can be faded.
Thanks Pat but both are popup and still getting the 438 error message. Beginning to think that arnelgp's post above maybe right and that it can't be done.
 
You said they were subforms. Subforms are forms which are embedded on another form in a subform control. So, which is it?
Access terminology isn't my strong suite.
They are subforms that you drag down from the design folder and plop into the main form. They are designated as 'child and whatever number'
I leave it as it is and call it sub1 and then use sourceobject to put the form in.
 
OK, now that we've cleared that up. You know they are subforms and you've been told that you can't fade a subform. Soooooooooo, why not try my suggestion?
 
OK, now that we've cleared that up. You know they are subforms and you've been told that you can't fade a subform. Soooooooooo, why not try my suggestion?
Thanks. Not the effect I was trying to achieve so I'll stick with populating the subform using the sourceobject method to swap forms
 
you can only Fade (in/out) the main form and not the subforms.
The reason for that is that your Fade code only works on objects (forms) with a Window Handle (hWnd). Subforms don't have that. They are painted on the form. Fading is not possible, unless you go to extremes.
 
you can use code, to resize the subform or even 'spin' it - move the left towards the middle by a factor - say 5% of the overall width and reduce the width by twice the factor. Once the width is zero, change the source object to your new form and then move the left back, and increase the width.

Same principle can be applied to top and height or combine both to shrink to nothing then expand back out.

Depend on how busy your form is but the other thing you can do is set all control color properties to say white then over a period of a second or so, gradually change these colors from white to black through the grey scale.
 
Last edited:
I use this code in the attached example:
Code:
Private Declare PtrSafe Function SetLayeredWindowAttributes Lib "user32" (ByVal Hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
...

Private Sub SetWindowTransparency(ByVal FormHwnd As LongPtr, ByVal TransparencyValue As Byte)

   Dim RetVal As LongPtr
   Dim bAlpha As Byte
   
   bAlpha = (255 - TransparencyValue)
   
   RetVal = GetWindowLong(Hwnd, (-20))
   RetVal = RetVal Or &H80000
   SetWindowLong FormHwnd, (-20), RetVal
                               
   SetLayeredWindowAttributes FormHwnd, 0, bAlpha, &H2

End Sub

@AC_Alberta: Can you add your "problem" to the example?
 

Attachments

Subforms don't, but the form inside them does, using that handle the OP can fade their form. But there is no sample code from them to test it.
there is a sample code from Chatgpt to get the handle of the subform, yet when tested to fade it using the handle found, it does not work.
 
I gave it a shot and created this module:
Code:
Option Explicit

Private Declare PtrSafe Function SetWindowLong _
    Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  
Private Declare PtrSafe Function GetWindowLong _
    Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
  
Private Declare PtrSafe Function SetLayeredWindowAttributes _
    Lib "user32" (ByVal hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE = -20
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2

Public Sub SetFormOpacity(FormObject As Form, opacity As Long)
    Dim currentWindowStyle As Long
    currentWindowStyle = GetWindowLong(FormObject.hwnd, GWL_EXSTYLE)

    Dim targetWindowStyle As Long
    targetWindowStyle = SetWindowLong(FormObject.hwnd, GWL_EXSTYLE, currentWindowStyle Or WS_EX_LAYERED)

    SetLayeredWindowAttributes FormObject.hwnd, 0, opacity, LWA_ALPHA
End Sub

That module helps setting the opacity of any form object, including those inside subforms. I achieved the fade in effect using the timer, but it has its issues. I didn't test using the Sleep API call, but it might make it more stable.

Please download the attached file to test its effects, I added two:
1. Fade in on timer (2 versions)
2. Fade using a slider
 

Attachments

if you have border on the subform, it won't get faded?
need to set the border of the subform same color as the detail backcolor

open Mainform
 

Attachments

Last edited:
save the "state" of the subform in a table, so next time you open main form, it is hidden or not.
 

Attachments

@arnelgp
You forgot to enclose the subform control name with square brackets?
I was gettin an error all the time.

Private Sub Command2_Click()
FadeForm [Table1 subform].Form, True
Me.[Table1 subform].BorderColor = lngBordercolor
End Sub
 
which one, the last db.
i changed the subform name to subForm.
 

Users who are viewing this thread

Back
Top Bottom