can i assign a control as a constant?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: can i assign a control as a constant?

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    can i assign a control as a constant?

    [Originally posted by Johm]


    Can i assign a control as a constant?


    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
    Join Date
    Aug 2004
    Posts
    43,023

    Re:can i assign a control as a constant?

    [Originally posted by Andon K.]

    The value of a constant can only be a string or a numeric literal, not an object, even if you declare the constant as Object. (The declaration will be accepted, but you will get a runtime error).

    What you can do is simulate the behavior of a contant using public variables in a standard (.bas) module (if you want application level scope for your "constants"):

    Public Main As Form
    Public MySubform As Form
    ...

    Then declare a public sub that initializes your "constants":

    Public InitConstants()
    ˙ Set MySubform = [Forms]![FOrderInformation]![Forder details extended].[Form]
    ˙ Set Quantity = [Forms]![FOrderInformation]![Forder details extended].[Form].[Quantity]
    ˙ ' etc ...
    End Sub

    Call this sub before you use the value of one of the "constants" because the value in the corresponding control may have changed after the last call to this sub.

    In addition of this being a very clumsy solution, using public variables in a .bas module is bad practice because there is always a danger of inadvertantly modifying their values, as they are accessible from any part of the application. So, you will have to be very strict and make sure that your "constants" never get modified. Not always an easy task ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center