Link to home
Start Free TrialLog in
Avatar of Milewskp
MilewskpFlag for Canada

asked on

Is it possible set a form to design view via code in its own module?

As part my form's OnOpen event, I'm trying to convert some text box (tbo) controls to combo box (cbo) controls (long story). To do so, I first need to set the form to design view But, I'm finding that I can't execute this statement in the form's module without an error:
        DoCmd.OpenForm FormName:=MyForm.Name, View:=acDesign

(I also tried to put this statement in a subroutine of a regular module, and then have the OnOpen event call that subroutine, but it won't be fooled).
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

So, what about what we had yesterday ? Why can't you use that?

mx
The bottom line is ... you cannot switch to Design view inside of the Form Open event.  The error message is pretty specific.  So how about an extension of what we did yesterday:

Public Function aaaaTest6()

    With DoCmd
        .OpenForm "frmChangeToComboBox", acDesign
        Forms("frmChangeToComboBox").Controls("text0").ControlType = acComboBox
        .Close acForm, "frmChangeToComboBox", acSaveYes
        .OpenForm "frmChangeToComboBox"
    End With
   
End Function

mx
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have used code like this in the past:

Dim ctl As Control
Dim strFormName As String

strFormName = "frmChangeToComboBox"
   
    'Open the form in Design View, Hidden
    DoCmd.OpenForm strFormName, acDesign, , , , acHidden
   
    'Loop the controls
    For Each ctl In Forms(strFormName)
        '(Obviously, each control to be changed will have to have this property set)
        'If the Tag property is set to "CTC" ( (C)hange (T)o (C)ombobox )
        If ctl.Tag = "CTC" Then
            'Select the control
            ctl.InSelection = True
            'Change selected control's ControlType
            ctl.ControlType = acComboBox
        End If
    Next ctl
   
    DoCmd.Close acForm, strFormName, acSaveYes
   
    MsgBox "Done changing Controls.", vbInformation


Note: You should compile the code and compact the DB after this code is run, ...just to be on the safe side.


JeffCoachman

The above code will loop all the control that have the Tag property set to "CTC"

Since you stated:
I'm trying to convert some text box (tbo) controls to combo box (cbo) controls
Control(S), meaning plural, multiple controls


You may be able to tidy up the syntax, but it works...

Jeff
MX

Thanks, I was beginning to think that this was secret question just between you two.
LOL!


I'm off to my parents now.

Happy Turkey day to you too...

Jeff
Guy and turkey standing in barn yard.  Turkey says "What for dinner?".  Guy says: "If I tell you I'll have to kill you."  Happy TGD ;-)
<<As part my form's OnOpen event, I'm trying to convert some text box (tbo) controls to combo box (cbo) controls (long story). >>

  Why bother?  Create text controls and combo's, hide them by default, then switch between the two by simply making visible the ones you need in the OnLoad event.

JimD.
Avatar of Milewskp

ASKER

Folks,
Mx's solution is the best for me. Thanks to all for your input.
<<Mx's solution is the best for me. Thanks to all for your input. >>

  you are aware that it will put your app in an un-compiled state when you change the control (assuming you have code attached to it).

  Generally, unless your writing something that by design is modifying an object (like a Wizard), flipping into design view and changing things as a normal matter of course is not a great idea and for me, is something I avoid at all costs.

JimD.
"  you are aware that it will put your app in an un-compiled state when you change the control (assuming you have code attached to it)."

Actually, even with no actual code in the form, BUT ... with Has Module = Yes ... it will un-compile.

mx
<<Actually, even with no actual code in the form, BUT ... with Has Module = Yes ... it will un-compile.>>

  Nice to know.  Never caught that before.

Thanks :)
JimD.

Hi Jim,
<you are aware that it will put your app in an un-compiled state when you change the control (assuming you have code attached to it).>
 No, I wasn't. Thanks for that. In my case, only the administrators of the database would be making these design changes, so I will have to tell them to recompile.
Two questions for you:
 - What are the consequences of an uncompiled app? Will it run slower, or are there other problems?
 - Is it possible to use a vba procedure to recompile all modules except for the one the procedure is in?
<<- What are the consequences of an uncompiled app? Will it run slower, or are there other problems?>>

  It simply will run slower under the situation you have.  During development, it may mask errors.

<< - Is it possible to use a vba procedure to recompile all modules except for the one the procedure is in?>>

  There is a undocumented SYSCMD function (SysCmd(504, 16483) ) to compile all modules.

  I however would be leery of doing so.  Generally I find anytime I start doing stuff like this with Access, it comes back to bite me in the end.

JimD.
Thanks Jim,
I will post a question to see if anyone else has ideas on this (you should see if within the next 10 minutes).