473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

setting public objects of parent form

Hey guys (m/f),

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
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public textboxtext As String
  5.  
On subform A:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.     Me.Parent.textboxtext = Me.textbox1.Value
  3. End Sub
  4.  
And on subform B:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdShowMsg_Click()
  2.     Dim text As String
  3.     text = Me.Parent.textboxtext
  4.     Call MsgBox("The text on the previous subform was: " & text)
  5. End Sub
  6.  
Now, this works like a charm.
The problem arises when trying to do this with an OBJECT instead of a VARIABLE.


E.g.
Container form:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public rst As DAO.Recordset
  5.  
On subform A:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.     Set Me.Parent.rst = CurrentDb.OpenRecordset("SELECT * FROM table1 WHERE ID = " & Me.textbox1.Value & ";", dbOpenSnapshot)
  3. End Sub
  4.  
And on subform B:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdShowMsg_Click()
  2.     Dim rsttemp As dao.Recordset
  3.     Set rsttemp = Me.Parent.rst
  4.     Call MsgBox("The name of the record is: " & rsttemp.Fields("Name"))
  5. End Sub
  6.  

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!
Jun 2 '09 #1
7 4143
FishVal
2,653 Expert 2GB
I couldn't reproduce it in Access2003.
Would you like to attach a sanitized copy of your db to the thread?

Regards,
Fish.
Jun 3 '09 #2
Fishval, thanks for replying. I'll post a sample db asap - just give me about 8 to 9 hrs to go to work and come back home :)
Jun 3 '09 #3
FishVal
2,653 Expert 2GB
No worries, post it when you like.

The only problem is that I don't have Access 2007. So it would be nice to get both original version (for any other willing to help and having Access 2007) and version downgraded to Access 2003 (for me and, I guess, for most of the forum staff).
Jun 3 '09 #4
ADezii
8,834 Expert 8TB
@SjaakBanaan
  1. Dim rsttemp As dao.Recordset within the confines of a Click() Event essentially makes rsttemp a Local Variable and is destroyed as since as the Click() Event is terminated.
  2. Dim text As String is never a good idea, Declaring a Variable with the same name as a Built-In Data Type.
  3. Public rst As DAO.Recordset - Declaring a Variable as Public within the context of a Form makes it a Property of the Form which may not be the approach to take. Try Declaring it Privately within the Form where it is now accessible throughout.
Jun 3 '09 #5
@ Fishval,

I have created a small db (1 table, 1 container form, 2 sub forms) from scratch, and the problem seems to not appear....

I will now try to cut pieces out of the the larger db that I have the problems with, to the point where it does work, to see where the problem lies.

Thanks for your help sofar!



@ADezii,

@ADezii
True; rsttemp is a local object that is destroyed once the click-event is terminated. Still, the recordset is retained when another object (i.e. the one on the container form) is still pointing to it.

@ADezii
Sorry, my bad. That was an unfortunate naming example.

@ADezii
I want it to be a property of the form, so that the sub forms can actually change it! In practice I will of course have the rst object be private, and only modifiable through a 'property set' (where data validation checks are in place), or through a public subroutine (again, with all data validation checks in place).

I do not see how I can pass information between subforms in any other way than by storing them on the container form. If you have suggestions... they're very welcome! :)

(But you are right in that all information that is relevant only to ONE subform should be kept on this form, and not be shared).

Thanks for your help as well!
Jun 3 '09 #6
Alright people,

I understand now. This is what happened.

On the parent form, there is the public recordset object 'rst'. Then, also on the parent form, there is a number of functions that retrieve data from this recordset, or from another recordset if specified. In code:

Expand|Select|Wrap|Line Numbers
  1. Public Function varCharacteristic(byval lngField as Long, Optional ByVal varID As Variant) As Variant
  2.         'Get a Field value.
  3.         Dim varResult As Variant
  4.         Dim rst As DAO.Recordset
  5.  
  6.         On Error GoTo ErrorHandler
  7.  
  8.         '1: Use the private recordset if no particular ID is specified.
  9.         '   (If a particular export option IS specified, it is only used in this procedure, and not selected as THE export option.)
  10.         If IsMissing(varID) Then
  11.             Set rst = p_rstApp
  12.         Else
  13.             Set rst = CurrentDb.OpenRecordset("SELECT * FROM table1 WHERE ID = " & CStr(CLng(varID)) & ";", dbOpenSnapshot)
  14.         End If
  15.  
  16.         '2: Retrieve field value.
  17.         With rst
  18.             If (.BOF And .EOF) _
  19.                 Then varResult = Null _
  20.                 Else varResult = .Fields(strField)                 'Causes (trapped) error if field doesn't exist.
  21.             Call .Close
  22.         End With
  23.  
  24.     Quit:
  25.         'cleanup
  26.         Set rst = Nothing
  27.  
  28.         varCharacteristic = varResult
  29.         Exit Function
  30.     ErrorHandler:
  31.         varResult = Null
  32.         Resume Quit
  33.     End Function
  34.  
Now, as I try to take care of all the objects and their memory use, I 'set' them all to 'nothing' before exiting the function (yes, not completely necessary. call me an autist.). This is not the problem, however. The problem lies in that I also 'close' all recordsets that I create within a function!

Expand|Select|Wrap|Line Numbers
  1.     '2: Retrieve field value.
  2.     With rst
  3.         If (.BOF And .EOF) _
  4.             Then varResult = Null _
  5.             Else varResult = .Fields(strField)                 'Causes (trapped) error if field doesn't exist.
  6.         Call .Close
  7.     End With
  8.  
This closes the private recordset... and makes it unavailable for later data retrieval!

Arhg! This has taken years off my life - but at least i'm a wiser man now.

Thanks again for trying to help me out!
Jun 3 '09 #7
NeoPa
32,556 Expert Mod 16PB
@SjaakBanaan
Never underestimate the value of being thorough. Even excessively thorough is a good thing.

It is a trait that marks out the very good programmers from the ordinary. Particularly important for database work.

Well done for getting the solution, and Welcome to Bytes!
Jun 4 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hollywood | last post by:
I'm using XML serialization to produce the following XML document: <TestDoc xmlns:srd="some-url"> <Additional> <Security> <srd:Login>login_id</srd:Login> <srd:Password>password</srd:Password>...
9
by: TD | last post by:
I am trying to add transactions to my code. The original code worked fine until I followed an example to setup transactions, now the code does strange things, but no error messages. Could...
3
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g....
1
by: Frank Yamrick | last post by:
I am trying to program an application that requires a large number of screnes that are very similar in the respect that all the bottons and labels interact with each other with the same...
0
by: meh | last post by:
Everytime I think I understand this it bites me....... I have a derived tree control on a MDI parent form. From a child form I want to be able to add a node (or modify). The tree (CurrTree) has...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
3
by: Mark Rae | last post by:
Hi, Just a general quickie on setting properties of user controls from the parent form. Let's say I have a user control called note.ascx which displays a datagrid. That datagrid is populated...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
1
by: Blau | last post by:
I'm still trying to learn c#, and while doing so I am trying to convert some C++ code to C#. Doing so has kind of challenged my understanding of the way values and references work in C# when i'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.