Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Feb 2002
    Posts
    537
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can i assign a control as a constant? (Access 2000)

    In my public functions i often have a recourse to one and the same controls of one and the same main form and subform.
    The main form is called FOrderInformation and the subform is called FOrder details extended.

    Here is an example:

    Dim Main As Form
    Dim MySubform As Form
    Dim Quantity As Control
    Dim cartons As Control
    Dim office As Control
    Dim customer As Control
    Dim liters As Control
    Dim size As Control
    Dim items1 As Control
    Dim unitprice As Control
    Dim extendedprice As Control
    Dim strWhere As String
    Dim strCondition As String


    Set Main = [Forms]![FOrderInformation]
    Set MySubform = [Forms]![FOrderInformation]![Forder details extended].[Form]
    Set Quantity = [Forms]![FOrderInformation]![Forder details extended].[Form].[Quantity]
    Set cartons = [Forms]![FOrderInformation]![Forder details extended].[Form].[cartons]
    Set size = [Forms]![FOrderInformation]![Forder details extended].[Form].[size]
    Set liters = [Forms]![FOrderInformation]![Forder details extended].[Form].[liters]
    Set items1 = [Forms]![FOrderInformation]![Forder details extended].[Form].[items1]
    Set unitprice = [Forms]![FOrderInformation]![Forder details extended].[Form].[unitprice]
    Set extendedprice = [Forms]![FOrderInformation]![Forder details extended].[Form].[extendedprice]
    Set customer = Main![Customerid]
    strCondition = "ProductID=" & MySubform.Productid
    strWhere = " WHERE " & strCondition



    I often use these controls in different functions, say liters = Quantity * size and so on, for may different occasions.
    Each time, for each function i have to declare the above controls with Dim and Set.
    Is there any way to simplify my work, and make all these variables constant, or to call them somehow in my functions
    from a given stored place?

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Can i assign a control as a constant? (Access

    There's no such thing as a "control constant". As defined in "Help", a constant is "a named item that retains a constant value throughout the execution of a program. A constant can be a string or numeric literal, another constant, or any combination that includes arithmetic or logical operators except Is and exponentiation." You can declare an object variable as Public in a standard module, and use this variable in another module, but the Set statement where you assign the object reference for variable can only be used within a procedure. Attempting to set reference in the Declarations section of a module will result in an "Invalid outside procedure" error.

    Here are some examples of generalized functions you can use in a standard module to get current value of a specified control on an open form or subform:
    <pre>Function GetFormValue(strFrm As String, strCtl As String)

    Dim frm As Form
    Dim ctl As Control

    If Not IsLoaded(strFrm) Then
    DoCmd.OpenForm strFrm
    End If

    Set frm = Forms(strFrm)
    Set ctl = frm.Controls(strCtl)
    GetFormValue = ctl.Value

    Set frm = Nothing
    Set ctl = Nothing

    End Function</pre>

    <pre>Function GetSubformValue(strFrm As String, strSubfrm As String, _
    strCtl As String)

    Dim frm As Form
    Dim ctl As Control

    If Not IsLoaded(strFrm) Then
    DoCmd.OpenForm strFrm
    End If

    Set frm = Forms(strFrm)
    Set ctl = frm.Controls(strSubfrm).Form.Controls(strCtl)
    GetSubformValue = ctl.Value

    Set frm = Nothing
    Set ctl = Nothing

    End Function</pre>

    Example of use (from Northwind.mdb):
    <pre>GetSubFormValue("Orders","Orders Subform","OrderID")</pre>

    Note that strSubfrm argument refers to name of subform CONTROL on main form, NOT name of subform itself. The IsLoaded function is standard function used to test if a form is open, example can be found in Northwind.mdb Utility Functions module. If using these functions repeatedly in a sub use variables for form name, control name, etc to simplify syntax & avoid repetition.

    HTH

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can i assign a control as a constant? (Access 2000)

    You can put the declarations (Dim ... As ...) in a standard module; then they will be available throughout the database. You should remove the declarations from the individual routines; if you have both global and local variables of the same name, you're in for confusion.

    The assignments ( ... = ...) are something else - you can't put them in a standard module, because they are only valid if the forms they refer to are open. Moreover, the value of a variable like strCondition isn't constant - it depends on the ProductID in the current record of the subform. So you will have to set the value explicitly each time you need it. A variable is, well, variable and not a constant ... <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •