473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting public objects of parent form

6 New Member
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 4153
FishVal
2,653 Recognized Expert Specialist
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
SjaakBanaan
6 New Member
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 Recognized Expert Specialist
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 Recognized Expert Expert
@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
SjaakBanaan
6 New Member
@ 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
SjaakBanaan
6 New Member
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,564 Recognized Expert Moderator MVP
@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
2608
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> </Security> </Additional> </TestDoc>
9
2046
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 someone please review my before and after code and tell me the proper way to add transactions to my code? Thanks, TD
3
1931
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. <textarea name="NameOfTextArea" rows="5" cols="60">some text area</textarea>), as I want to then dynamically read back the controls' values Setting...
1
1978
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 relationship. If possible, it seems it would be easier to set up one standard page where the text is blank for each radiobutton, button, or label. The text...
0
955
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 Public WithEvents CurrTree As TreeView defined in the parent (MDI) and the same tree(CurrTree) is also defined in a module as a Public CurrTree...
8
1849
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
3
3905
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 according to an integer variable in the ViewState of the parent form. The code behind (still in v1.1) of the user control contains the following:
6
1477
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 creation of a class easier to read/understand. Anyway it seems to break the inheritance chain in the following code and I don't know why. ...
1
1955
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 tyring to duplicate C++ items that use pointers, such as a linked list. In the c++ code that i'm trying to convert, I have a linked list of...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.