Search code examples
vbams-access

reload the form with a button in access forms


I have an access form (edit only form). It has an OnLoad event that will show only a combobox for the user to choose from, which then by an AfterUpdate event will show some controls in the form, through visible and enable properties of the controls. Then the user can click a button to save the dirty record by this code:

    If Me.Dirty Then
          Me.Dirty = False
    End If

Till here everything is ok.

What I need is with the same button click event, the form to reload to its original status, i.e. to reload the form as if it is closed and re-opened like the previous OnLoad event to show only my combobox

I tried the requery and refresh methods, but that does not bring me back to the OnLoad event shape.


Solution

  • Try to abstract your code by putting logic into a separate sub.

    Instead of

    Sub MyCombo_AfterUpdate()
        Me!foo.Visible = True
        Me!bar.Enabled = True
        ' etc.
    End Sub
    

    create a sub for this:

    Sub ShowEditControls(bShow As Boolean)
        Me!foo.Visible = bShow 
        Me!bar.Enabled = bShow 
        ' etc.
    End Sub
    

    and then simply

    Sub MyCombo_AfterUpdate()
        ShowEditControls True
    End Sub
    

    and

    Sub btSave_Click()
        If Me.Dirty Then
              Me.Dirty = False
        End If
    
        Me!MyCombo.SetFocus
        Me!MyCombo.Value = Null   ' or whatever initial value you need
    
        ShowEditControls False
    End Sub