Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Burgas, Bulgaria
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to declare only once (Access 2000)

    In my codes i am often referring to one and the same controls.Is it possible
    to declare these controls only once and then insert them in all my functions and subs?
    For example, i am often referring to these controls:


    Set main = [Forms]![frmStudents]
    Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
    Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]

    How and where can i declare them, say as constants?

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

    Re: how to declare only once (Access 2000)

    You have to be aware that frmStudents is only a member of the Forms collection as long as it is open. If you would set a global variable to Forms!frmStudents while the form is open, then close the form, the variable would not be valid any more, even if you would open the form again. You would have to set the variable anew. So I don't think it makes much sense to treat these variables as if they were constants.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Burgas, Bulgaria
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how to declare only once (Access 2000)

    Your reply is very relevant.In my case the form frmStudents is always open but i have a lot of codes and it would be easier to me if a have a global variable.
    So, given the conditon that the form is always s open is there any way to delcare the controls only once? I am especially interested in building constants
    out of these controls,but i am not sure is it possible at all

    regards

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: how to declare only once (Access 2000)

    You can set global variables for objects, but it isn't a good idea unless you put in a lot of code to make sure the objects get reset if they're broken, which pretty much negates what you're trying to accomplish. I have no idea of what you mean by "building constants out of these controls".

    Global variables aren't really used to make it "easier" on the developer. They're used to reduce trips to the server or to pass values between objects when there is no other means. If you declare a global object, how are you going to destroy it when the application is closed?
    Charlotte

  5. #5
    Lounger
    Join Date
    Dec 2003
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how to declare only once (Access 2000)

    If the form is alway open i think it is a very good idea to refer to some controls only once,instad of repeating them all over again.
    It will save time and space.
    I doubt whether you could do it with Access.

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

    Re: how to declare only once (Access 2000)

    I fail to see what you would gain. In the code module that belongs to frmStudents, there is no need to use Forms!frmStudents at all, you can use Me instead. In a form (or report), Me always refers to the current instance. Using Me has the advantage that it is independent of the actual name of the form; if you would rename the form to frmStudentForm, all references to Forms!frmStudents would cause an error, but Me would still work.

    If you want to put general procedures or functions in a standard module, you can pass Me as an argument:

    Public Sub DoSomething(frm As Form)
    ' Refer to the form as frm, for example:
    MsgBox frm.Name
    End Sub

    Call this procedure from the form as follows:

    DoSomething Me

    or

    Call DoSomething(Me)

    Similarly, if you have a general procedure or function that processes a control, you can do it as follows:

    Public Function SomethingElse(ctl As Control) As String
    ' Refer to the control as ctl, for example
    SomethingElse = CStr(ctl.Value)
    End Function

    Call it like this:

    MsgBox SomethingElse(Me.txtLastName)

  7. #7
    3 Star Lounger
    Join Date
    Feb 2004
    Location
    Burgas, Bulgaria
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how to declare only once (Access 2000)

    this is exactly what i wanted. Thank for the detailed and well grounded answer.I am pleased to see how professional the replies are

    regards

  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: how to declare only once (Access 2000)

    Perhaps it's foolish, but I do exactly what keks was looking to do with some of my forms. If you have a form (Form A) that's open a lot and Form A and/or certain of its controls get referred to a lot by code in modules other than Form A's class module, I think it arguably makes sense to set up global pointers to Form A and/or its much-referred-to controls. Not only does it streamline the references in the code, pointers also speed up the execution of the code.

    In case keks or anyone else would like a how-to, here's how I implement it:

    Let's say I wanted global pointers for frmProducts and 2 of its controls (cboProductName and cboProductType, both combo boxes).

    1. I have a separate standard module named GlobalVar with all my global variables in it. In its Declarations section, I'd include the following lines:

    Public frmPR As Form
    Public PR_Name As ComboBox, PR_Type As ComboBox

    2. In a separate standard module named basPointers, I'd put the following SetPointers_FrmPR procedure (error-handling omitted):

    Sub SetPointers_FrmPR()

    Set frmPR = Forms!frmProducts
    Set PR_Name = frmPR!cboProductName
    Set PR_Type = frmPR!cboProductType

    End Sub

    3. In the Form_Open procedure in the frmProducts class module, I'd add the following line:

    Call SetPointers_FrmPR

    4. In the Form_Close procedure in the frmProducts class module, I'd add the following lines:

    Set frmPR = Nothing
    Set PR_Name = Nothing
    Set PR_Type = Nothing

    5. If a database's code project gets "reset", all the variables get re-initialized, which means any global pointers get blown out. To recover, you can close and re-open the relevant forms (which will reset the pointers as part of the Form_Open procedure). But to make recovery easier, I have a ResetTheDatabase procedure (in a separate library database) that I can run by clicking a toolbar button. The ResetTheDatabase procedure includes the following line:

    Call SetPointers_All

    The SetPointers_All procedure (in basPointers -- in the main application, not the library database) would, in turn, include the following lines:

    If IsLoaded("frmProducts") Then
    Call SetPointers_FrmPR
    End If

    6. WATCH OUT: If you want to set up global pointers to a subform and/or its controls, follow the exact same steps outlined above for the subform (and/or its controls) -- i.e., treat the subform as if it was a completely separate form. DO NOT try to combine procedures for the main form and subform. For example, call the procedure that sets the subform-related pointers from the Form_Open procedure of the SUBFORM, rather than the Form_Open procedure of the main form. (Otherwise you can run into problems because, under different circumstances, sometimes the main form opens before the subform and sometimes the subform opens first.)

Posting Permissions

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