Search code examples
vbams-accessmodulems-access-forms

MS Access: Resizing dialog form from within module


I am trying to open a form in dialog mode, and then resize it to a desired size using DoCmd.MoveSize from a module. Ideally I would like this all to run from said module.

Currently the form opens, but does not get resized. I am guessing this is either because the form is not yet active when the command is being run or because acDialog in the OpenForm command prevents it from being executed. I think I could get away with it plugging the MoveSize command into the OnlOad event of the form but I would prefer having as much of the functionality in the module instead. Is there some way around this issue? Like maybe there is some kind of method to wait for the form to be active or some way to plug a command into the OnLoad event from the module itself? As you can tell I am not too familiar with vba yet. It's part of a little project I am doing. Appreciate any help.

Some sample code is as follows:

This one does not resize the form:

Option Compare Database
Option Explicit

Public Function OpenFormDialog()

    DoCmd.OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acDialog
    DoCmd.MoveSize , , , 9500
    
End Function

This one throws an error because the form is not found:

Option Compare Database
Option Explicit

Public Function OpenFormDialog()

    DoCmd.OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acDialog
    Forms.("MsgBoxForm").DoCmd.MoveSize , , , 9500
    
End Function

Solution

  • You could open the form in normal mode, resize and make modal. Just make sure the Modal property on the form is No.

    With DoCmd
        .OpenForm "MsgBoxForm", acNormal, , , acFormAdd, acNormal
        .MoveSize , , , 9500
    End With
    
    Forms.MsgBoxForm.Modal = True
    

    Keep in mind, any code below the last (modal) statement will still execute. If you need code execution to stop, your only option is to resize in the Load() event directly.

    Personally, I believe the the Load() solution is better and you could pass the various sizes through the OpenArgs parameter.