WithEvents Form AccessObject ... conundrum

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
Hello all.
My goal at the moment is: To open Form1 as background, make it translucent, then use the openArgs to pass a name to open Form2 on top of it. When Form2 is closed Form1 to close automatically.
I achieved it easily with DoCmd.OpenForm but then I have to set WindowMode:=acDialog, which shows the form titlebar and ruins the design.
So I am trying to make it in another way. After some time I got to the idea to declare (in the module of Form1) a form object withEvents, assign Form2 to it, open it and when it is closed it will close Form1.
So far the problem is:
Case 1: I get a Type mismatch error on the line Set frm = ....
VBA Code:
Private WithEvents frm As Access.Form
...
Set frm = CurrentProject.AllForms(Me.OpenArgs)
frm.Close = "[Event Procedure]"
DoCmd.OpenForm frm.Name
...
Case 2: When I try this - The compiler just says Compile error: Object does not source automation events
VBA Code:
Private WithEvents frm As AccessObject
It does not accept Variant or Object at all on this line.

Any workarounds, or better ideas than mine are welcome?
Thanks in advance.
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
1,860
Not sure I get all of this. AFAIK, there is no such thing as a translucent form property. If there is, I'd sure like to learn how to do that. Maybe you mean invisible?
Second, you can open a form as popup and/or modal - no need to use acDialog. You need to ensure other properties are set in harmony in order to not not show a title bar. I think those are no control box, open as popup, maybe no close button, and a single space in the form caption should prevent the little form icon.
Next, if you want to assign a property on the fly (if that's what frm.Close = "[Event Procedure]" is) then it's usually like = "=[Event Procedure]" assuming that is the name of the function - which by thy way, it has to be a function.

Maybe I don't understand all of what you're doing. I don't see why you can't just command form1 to close in form2 close event rather than declaring forms as objects and such.
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
Not sure I get all of this. AFAIK, there is no such thing as a translucent form property. If there is, I'd sure like to learn how to do that. Maybe you mean invisible?
Second, you can open a form as popup and/or modal - no need to use acDialog. You need to ensure other properties are set in harmony in order to not not show a title bar. I think those are no control box, open as popup, maybe no close button, and a single space in the form caption should prevent the little form icon.
Next, if you want to assign a property on the fly (if that's what frm.Close = "[Event Procedure]" is) then it's usually like = "=[Event Procedure]" assuming that is the name of the function - which by thy way, it has to be a function.

Maybe I don't understand all of what you're doing. I don't see why you can't just command form1 to close in form2 close event rather than declaring forms as objects and such.
Well, I can't really explain everything but i will try to clarify:
Threre isn't a Translucent property but with some APIs from an Excel file (found some time ago on the net) and some adjustments for access it actually works pretty well. When used with combination with message box it's quite good.
I need to use acDialog to make the code in the caller Form1 to wait for Form2 to be closed. Then Form two shows as a dialog (with title bar - because of acDailog), but Form2 has no border by design - so I don't like it.
This is why I want to use a form object with Events and to use the close event on form2 to tell the code in Form1 that it can continue. That's what the following is supposed to be doing (in Form1):
VBA Code:
Private WithEvents frm As Access.Form
...
Private Sub Form_Open(Cancel As Integer)
...
                Set frm = CurrentProject.AllForms(Me.OpenArgs)
                frm.Close = "[Event Procedure]"
                DoCmd.OpenForm frm.Name
...
End Sub
...
Private Sub frm_Close()
    Set frm = Nothing
    DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
...
But it seems I cannot do it in this way. Actually the ultimate question for me here is: How to assign a closed form to a Form Object variable WithEvents?
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
I think I may have just come up with a working Idea, but will test it later on and share the result. Basically the idea is to use a second object (w/o events) to open the form, then assign the opened form to a form object with events. Something like this:
VBA Code:
Private WithEvents frm As Access.Form
Private obj
...
Private Sub Form_Open(Cancel As Integer)
...
                Set obj = CurrentProject.AllForms(Me.OpenArgs)
                DoCmd.OpenForm obj.Name
                Set frm = Forms(obj.Name)
                frm.Close = "[Event Procedure]"
...
End Sub
...
I already feel positive about it but will test it later and share the result.
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
:sick: the above one works almost well with one exception: Form2 remains behind Form1. 🥵
Any clues anyone?
...
Just checked - If I make form2 Modal, then it works as I want it to.
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
1,860
I guess you figured out that you don't need acDialog before I had a chance to counter your claim, which in fairness, I had already mentioned.
Second, you can open a form as popup and/or modal - no need to use acDialog
Glad to see you got it working, although I still think you've over complicated it - unless you need to do what you're doing because of this translucent thing, which I'm none the wiser about.
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,343
Glad to see you got it working, although I still think you've over complicated it
So am I, thank you, although it seems I have a tendency to do this ocasionally.:biggrin:
The translucent part is just a design feature, which turned out to be the less complicated part. I just wanted to achieve somthing similar to UAC prompts in Windows.
Second, you can open a form as popup and/or modal - no need to use acDialog
Just to explain something for whoever else reads this - the above statement is generally incorrect. Depending on the situation you can use one or the other, but there is a substantial difference between the two.
- The PopUp form property just brings the form on top, Modal - simply forbids you access to the forms behind it. But the code, the one that called the form, keeps on executing.
- When you open a form as acDialog, the form acts as a MsgBox or InputBox - the code that called it is waiting for the form to close before resuming its execution.
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
1,860
You are correct; I remembered it wrong. It is the acDialog call that suspends code and not the modal/popup property combination. Is this the sort of look you are after?
NoTitlebar.jpg

You might say the white space above is the title bar, but you can't see any close button, form icon or form caption.
 

Micron

Well-known Member
Joined
Jun 3, 2015
Messages
1,860
Well, I meant with respect to the title bar. Take away the navigation control and record selectors and you have the same thing but no sort of transparency.
Did you get your code to work? I found some but it made no difference in Access. There were some cautions about using API's for this sort of thing.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,907
Messages
5,489,650
Members
407,703
Latest member
Chibuzo

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top