Vba – OpenArgs is Null issue


I am using the OpenArgs parameter to send a value when using DoCmd.OpenForm:

DoCmd.OpenForm "frmSetOther", acNormal, , , acFormAdd, acDialog, "value"

I then use Me.OpenArgs inside the opened form to grab the value. It sometimes sends a Null value instead of the original string. What is wrong?

Best Solution

This often happens during developpment whem the form is already oppened (in edit mode for example) and you invoke the docmd.OpenForm function. In this case, the form is placed in normal (view) mode and the OnOpen and OnLoad events are raised, but the OpenArgs property is set to null no mater what you passed to docmd.OpenForm.

The solution is obviously to close the form before you invoke it with docmd.OpenForm. However there is a workaround I like to use. In the OnOpen event I check if me.OpenArgs is null and if it is I replace it with some debug values.

if not isnull(me.OpenArgs) then
   myvalue = me.OpenArgs
   msgbox "Debug mode"
   myValue = "foo"
Related Question