473,503 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

referencing a variable from a subform

9 New Member
Another question to people smarter than me! LOL

I have a form with a variable - Dim cboOriginator as TextBox - that holds the info about a textbox i click on. when i click on it, i have a calendar set up to "become visible" and show either the date in the field i just clicked on or todays date if null. all of this worked fine when i had the date fields in the same table as the rest of the data; but now i have this info in a separate table and have it shown in a subform.

my problem is now that i have this subform to view these dates, i can no longer reference the variable on the main form to set it. how can i set the variable on the main for, when i click on the date textbox on the subform? my code is below.

Code for mousedown event on the date textbox

Expand|Select|Wrap|Line Numbers
  1. Private Sub ActionDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2. 'Set ActionDate textbox to variable
  3. Set cboOriginator = ActionDate
  4. 'Show calendar
  5. Forms![KitView]!Calendar.Visible = True
  6. "set focus on calendar
  7. Forms![KitView]!Calendar.SetFocus
  8. 'If textbox had a date in it, set calendar to that date, else set to todays date
  9. If Not IsNull(cboOriginator) Then
  10.    Forms![KitView]!Calendar.Value = ActionDate.Value
  11. Else
  12.    Forms![KitView]!Calendar.Value = Date
  13. End If
  14. End Sub
Code for onclick event on main form (KitView)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Calendar_Click()
  2. 'Set current calendar date value to variable
  3. cboOriginator.Value = Calendar.Value
  4. 'Set focus back to original textbox
  5. cboOriginator.SetFocus
  6. 'Hide calendar
  7. Calendar.Visible = False
  8. 'Clear variable
  9. Set cboOriginator = Nothing
  10. End Sub
Aug 21 '08 #1
6 10773
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. I think you will need to move the definition of your variable to a public code module (one which is visible from the Modules tab of your database) as form code modules are inherently private - that is, forms and reports cannot see each other's code modules, regardless of whether or not a function or variable is declared Public.

Even within the same code module, you cannot declare variables inside a Sub or Function and have them in scope outside of that function - their scope is restricted to that Sub or Function only. If you need variables to be available to all subs in a module they must be declared in the header for the module. If you need the variable to be available in all modules everywhere then you need to use a public code module to do so, not a (private scope) form or report module.

Whilst subforms can refer to controls on the main form in code (and vice-versa) the subform and mainform code modules can't refer to each other's variables, or call each other's subs or functions.

In the heading of a public code module all you need is to declare your variable using the Public keyword
Expand|Select|Wrap|Line Numbers
  1. Public cboOriginator as textbox
and it will be available to all code modules - to whatever forms you have that need to use it.

Using global variables in this way is not best practice, because it is unclear to anyone reading your code where the variable came from and where it is really declared. Bear it in mind that you may forget where it is yourself if you leave your code for three months and come back to it afresh, so comment its use appropriately!

-Stewart
Aug 21 '08 #2
viperRider
9 New Member
Worked like a charm! I new there had to be a higher leverl for declaring a variable. and it is well commented!

here's a related question. I use that variable cboOriginator to set the focus back on the date i originally clicked on. but now when it comes to that point in the code - cboOriginator.setfocus - it no longer sets to focus back to the original date textbox so i can hide the calendar. any thoughts?
Aug 21 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. This isn't because of the use of a global variable - its the use of different forms which is resulting in the loss of focus of the form concerned (it works fine if only one form is involved).

To overcome it, use the parent property of the global textbox. It will point to the form involved, so we can setfocus to the form first before setting focus to the textbox within it:

Expand|Select|Wrap|Line Numbers
  1. cboOriginal.Parent.Setfocus
  2. cboOriginal.setFocus
-Stewart
Aug 21 '08 #4
viperRider
9 New Member
K, hit a wall here. I get an error "there is an invalid expression in an expression" and it points to the cboOriginator.Parent.setfocus.

let me clarify my earlier explanation a little :) I am trying to point back to the original textbox which is inside a subform, on the form i am using. basic rundown - I click on the date in the subform, global variable takes on the values of the textbox, the calendar pops up, i click on a new date, the record changes - here's the problem - i need to set the focus back to that original textbox in the subform, before i can hide the calendar.

any thoughts are greatly appreciated! i've been beating my head thus far - and if this setfocus thing won't work, i am happy with what i have after your help so far!
Aug 21 '08 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Ah, its because your combo is in the subform - so its parent property points to the wrong form. One way to deal with it is to go a level up again - to use the 'parent of the parent' property to reach the main form itself. I tested this in my Access 2003 installation - not elegant but it works on my test main form/subform.

Expand|Select|Wrap|Line Numbers
  1. cboOriginator.parent.parent.setfocus
  2. cboOriginator.setfocus
Of course, you could also set the focus to the main form directly if you are not going to be using different forms, using its name as follows:

Expand|Select|Wrap|Line Numbers
  1. Forms("your main form name goes here").Setfocus
  2. cboOriginator.setfocus
By the way, I have assumed that there is more than one control involved on your subform - otherwise you could just set focus to the single control involved by specifying the form then the subform control without using the global at all as follows:

Expand|Select|Wrap|Line Numbers
  1. Forms("your main form name goes here").Setfocus
  2. Forms("your main form name goes here")![Your subform control name].Form![your combos name].Setfocus
-Stewart
Aug 21 '08 #6
viperRider
9 New Member
Yes, in the subform I just have a datasheet that show fields for my homebrew fermenting rack; what needs to be done, what date it needs to be done and a checkbox if it has been completed. This is where i am using this calendar stuff; makes it much easier to see the days go by than to count in your head!

I still can not get the focus back to the original record in the datasheet, but I am happy with the way it is working now!

Thanks for your help!
Aug 21 '08 #7

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

Similar topics

2
4709
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a main form with a continuous subform. On the main form I have a text box that references a field on the subform. What I'd like it to do is show the value...
3
2770
by: shumaker | last post by:
This code from the subform works for getting the value of a field of the main form named "WorkSheet": MsgBox Form_WorkSheet.Recordset.Fields("Clerk").Value Each record in the mainform datasheet...
4
1837
by: mplogue | last post by:
I have a form (frmMain) with a subform (frmSub), each with enumerated fields of the same name (txt1, txt2, etc). I'm trying to make a function that will take the values for each field in frmMain,...
2
4041
by: Axel | last post by:
Hi, a question about something that seems very simple at first glance: is it possible to reference other controls of a subform in a query window without referencing through the parent form? I...
6
2733
by: Mat | last post by:
Dear all, What I want to do is be able to use a string to refer to a control on a subform. IE: Forms!("Form1!form2!controlA").name or
9
7352
by: Alan | last post by:
Hmmm, I'm not too good with the syntax of referencing a subreport. I have frmInvoice which has the invoice details (e.g. ProductCode, ProductCost etc) in the subform frmInvoiceDetails. I'm trying...
21
5066
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
9
8072
by: freeskier | last post by:
I am working on a form to edit student field placements. the student is selected from a combo box (cboStudent) which then populates a list box(lstPlacements) with field placements for that student....
3
3648
by: ManningFan | last post by:
Here's a fun one, mein froinds! I have a form. The form (call it MainForm) has a subform control (call it SubForm). The actual subform that SubForm displays can change based on choices made on...
0
7204
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
7342
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
7464
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
5586
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,...
0
3171
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...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.