referencing a variable from a subform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viperRider
    New Member
    • Jan 2008
    • 9

    referencing a variable from a subform

    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

    Code:
    Private Sub ActionDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'Set ActionDate textbox to variable
    Set cboOriginator = ActionDate
    'Show calendar
    Forms![KitView]!Calendar.Visible = True
    "set focus on calendar
    Forms![KitView]!Calendar.SetFocus
    'If textbox had a date in it, set calendar to that date, else set to todays date
    If Not IsNull(cboOriginator) Then
       Forms![KitView]!Calendar.Value = ActionDate.Value
    Else
       Forms![KitView]!Calendar.Value = Date
    End If
    End Sub
    Code for onclick event on main form (KitView)

    Code:
    Private Sub Calendar_Click()
    'Set current calendar date value to variable
    cboOriginator.Value = Calendar.Value
    'Set focus back to original textbox
    cboOriginator.SetFocus
    'Hide calendar
    Calendar.Visible = False
    'Clear variable
    Set cboOriginator = Nothing
    End Sub
    Last edited by Stewart Ross; Aug 21 '08, 07:18 PM. Reason: added code tags to code extracts.
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    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
    Code:
    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

    Comment

    • viperRider
      New Member
      • Jan 2008
      • 9

      #3
      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.s etfocus - it no longer sets to focus back to the original date textbox so i can hide the calendar. any thoughts?

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        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:

        Code:
        cboOriginal.Parent.Setfocus
        cboOriginal.setFocus
        -Stewart
        Last edited by Stewart Ross; Aug 21 '08, 09:03 PM. Reason: clarification of parent property

        Comment

        • viperRider
          New Member
          • Jan 2008
          • 9

          #5
          K, hit a wall here. I get an error "there is an invalid expression in an expression" and it points to the cboOriginator.P arent.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!

          Comment

          • Stewart Ross
            Recognized Expert Moderator Specialist
            • Feb 2008
            • 2545

            #6
            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.

            Code:
            cboOriginator.parent.parent.setfocus
            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:

            Code:
            Forms("your main form name goes here").Setfocus
            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:

            Code:
            Forms("your main form name goes here").Setfocus
            Forms("your main form name goes here")![Your subform control name].Form![your combos name].Setfocus
            -Stewart
            Last edited by Stewart Ross; Aug 21 '08, 09:24 PM. Reason: added note re form referencing

            Comment

            • viperRider
              New Member
              • Jan 2008
              • 9

              #7
              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!

              Comment

              Working...