MS Access - the best way to modular / decoupling forms for refactoring

So, I recently worked with a large access database, and I was thinking about how to start refactoring. One of the main difficulties of refactoring is that most forms are based on data in other forms.

The way this is currently being done is as follows:

Form1has a button on it called Cmd1that opens a new form ( DoCmd.OpenForm "Form2") and pre-populates some form controls using Forms!Form2.Control = Me.Control. When closing, Form2data is returned on Form1, calling Forms!Form1.Control = Me.Control.

This presents a problem if I want to change the names of both forms, or which form opens the popup. It also requires that both forms be open, which you cannot rely on when you cannot use modal pop-up forms, because users want to be able to share between forms.

I see 5 methods of passing values ​​back and forth between forms, each with its own problems, and I was wondering what was recommended.

  • The method is already installed, as described above.
  • Using OpenArgs - makes many values ​​complicated, since you need to parse a string and be unidirectional.
  • Using global variables - this will mean a lot of variables in a random module and is a nightmare to review.
  • Storing values ​​that must be passed between forms in a temporary table.
  • Public master As Form
    Private Sub Form_Load()
        Set master = Screen.ActiveForm
        Me.Control = master.Control
    End Sub
    

    , , master.master.control, , .

, ?

+4
2

, Access.

-, :

  • # 1 # 2
  • # 3 # 4 : , . .
  • # 5 . .

- , . # 3 # 4, .

, frmFoo modFrmFoo. frmFoo:

modFrmFoo:

Private mvID As Variant

' read-only (no Let)
Public Property Get frmFoo_ID() As Variant
    frmFoo_ID = mvID
End Property

Public Sub frmFoo_Show(ID As Variant, Name As Variant)
    ' note use of temporary object reference
    Dim rFoo As Form_frmFoo
    mvID = ID
    DoCmd.Open acForm, "frmFoo"
    Set rFoo = Forms("frmFoo")
    rFoo.ShowName Name
End Sub

frmFoo :

' only frmFoo should refer to its internal controls and code
Public Sub ShowName(Name As Variant)
    lblName.Caption = Name
End Sub

frmFoo:

SELECT ID, [other stuff] FROM tblFoo WHERE tblFoo.ID = frmFoo_ID() 

, frmFoo_ID().

frmFoo :

' frmBar
Private Sub cmdShowFoo_Click() 
    frmFoo_Show 123, "your name"
End Sub

:

  • . , .
  • private business private - , .
  • , - , frmFoo_Show() .

, .

, , .

-1

:

. Open Args

, ,

GlobalVars, :

Public Form_MyFormNameToOpen as Form_MyFormNameToOpen

( , " ", , .

MyFormNameToOpen - , , Form_ . , " ", .. .

, , :

' Opens the form using it as it were a class
Set GlobalVars.Form_MyFormNameToOpen = New Form_MyFormNameToOpen

' The modify the form you have just opened however you want to.
With Form_MyFormNameToOpen
    .Visible = True

    ' This relies ont he control names never changing
    .Controls("ProviderID") = 10
    .Controls("ProviderFileID") = 11


    ' it better to use properties created on the called form
    ' eg
    .MyLetProviderID = 10
    .MyProviderFileID = 11
End With

, .

, . .

(, , ..)

-1

All Articles