I've got a problem that's been having me pulling out hairs and losing sleep quite some time now.
The thing is this. I have a container form with a subform control. This control can take several sub forms as its source object. To make the information, that is generated on one sub form, available to a later sub form, I'm trying to store data in public variables and objects of the container form.
In other words, I'm using a form (i.e. the container form) as if it were a class. Which is what is giving me a headache.
To explain:
On subform A, there is a textbox of which I need to have the text available for subform B.
I therefore have on the container form:
Expand|Select|Wrap|Line Numbers
- Option Compare Database
- Option Explicit
- Public textboxtext As String
Expand|Select|Wrap|Line Numbers
- Private Sub cmdSave_Click()
- Me.Parent.textboxtext = Me.textbox1.Value
- End Sub
Expand|Select|Wrap|Line Numbers
- Private Sub cmdShowMsg_Click()
- Dim text As String
- text = Me.Parent.textboxtext
- Call MsgBox("The text on the previous subform was: " & text)
- End Sub
The problem arises when trying to do this with an OBJECT instead of a VARIABLE.
E.g.
Container form:
Expand|Select|Wrap|Line Numbers
- Option Compare Database
- Option Explicit
- Public rst As DAO.Recordset
Expand|Select|Wrap|Line Numbers
- Private Sub cmdSave_Click()
- Set Me.Parent.rst = CurrentDb.OpenRecordset("SELECT * FROM table1 WHERE ID = " & Me.textbox1.Value & ";", dbOpenSnapshot)
- End Sub
Expand|Select|Wrap|Line Numbers
- Private Sub cmdShowMsg_Click()
- Dim rsttemp As dao.Recordset
- Set rsttemp = Me.Parent.rst
- Call MsgBox("The name of the record is: " & rsttemp.Fields("Name"))
- End Sub
I've tried all different variants; passing the recordset through a function, or a sub, or a property set; using private objects, etc.
It seems that the reference to the recordset is lost as soon as the code jumps back to subform A, even before subform A is closed. ("<Out of context>" shows in watch window.)
Am I doing something wrong? Or is this a fundamental limitation of VBA? If so, is there a workaround?
When I write the code in a CLASS module instead of the FORM, it works just fine. Ah, and I'm using access 2007, btw.
Thank you for replying!