Link to home
Start Free TrialLog in
Avatar of developingprogrammer
developingprogrammer

asked on

delete textbox and label with vba access

hey guys, how do i delete a textbox and a label using vba in access? i saw this other question

https://www.experts-exchange.com/questions/22093818/Deleting-control-in-runtime.html
 
and the solution used this code
 pictureBoxEx1.Controls.Remove(lbl);
but it doesn't work for me.

remove doesn't even come up in the intellisense. i think maybe the ref lib is vb.net? dunno.

but how do i delete my textbox and label at runtime? thanks guys!! = ))
Avatar of mbizup
mbizup
Flag of Kazakhstan image

<< i delete my textbox and label at runtime? >>

You'd have to open your form in design view for starters:

Docmd.OpenForm, "YourFormName", View:=acViewDesign

When a form is in design view, you can add, delete, and otherwise manipulate design properties of controls and the form itself through VBA.

That said, this is not a recommended practice.  Displaying/hiding controls is a better option.
Why would you?
It is much faster ans simpler just to hide it:

Me!txtSomeBox.Visible = False

/gustav
<< That said, this is not a recommended practice.  Displaying/hiding controls is a better option. >>

Part of the reason for this is that forms have a limited number of controls that can be added over the lifetime of the form (deleting controls does not lower the 'control count' for this), and if you are repeatedly running this type of code against the same form, you can rapidly meet/exceed this limit.
Avatar of developingprogrammer
developingprogrammer

ASKER

yup got it. i'm adding and delete controls so that the format for the remaining controls would auto align to the left.

here's my workflow
1) generic template created
2) duplicate the template
3) edit the duplicated template to delete the controls so that the other ones would align to the left (i'm using continuous view tabular control layout)
4) resize the form width and any other buttons to fit nicely

 = )

mbizup, so sorry that i'm jumping through so many questions recently, but could you give me the sample code to delete a control once i've got the form opened up in design view? thanks! = )
yup yup, so now i got my form open in design view already. how do i go about deleting the controls? must i run the code from within the form's code module or can i do it from my class module referencing the form using forms("myform").whichever properties?

thanks guys!!!!!!! = ))))
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan 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
SOLUTION
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
Ah ok, I was thinking of resizing to 0 but haven't got to that stage of testing yet. Yup I will definitely follow your method of resizing to 0 = ) I wonder if size to fit will do the job if no data for text box. But of course won't work for label but just moot. Gonna answer myself soon with tests = )
Hrmm gustav what's the code you would use to make the size of the label 0? If there are 2 ways, one being MUST run from within the form and the other that I can run from outside like DeleteControl which is an Application method, could you share with me both? Thanks!! = ))
Looks like I need to run the code from within the form right? Is there a code that I can run from outside the form? I would very much prefer that = )
hrmm gustav i tried to set the width to 0 but then the gridlines look very bad now - like an empty cell

here's what it looks like. what do you think i should do? i read before posting this question that the max number of controls are 754 or something like that, but if i duplicate the form each time it's ok right? cause it's a totally different object. won't affect it.

User generated image
SOLUTION
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'm not familiar with that grid but I guess you should set the columwidth to zero to hide a column.

/gustav
User generated imagethanks gustav for all your help on this question and the others! = )

just to share i set the appropriate settings (which is Top Padding, Right Padding, Bottom Padding and Left Padding) to 0 and it's better but there's still a noticeably thicker line.

thus i will go with the delete controls way (though i know it's a bit risker), i'll try it and see how it goes, and if it really messes up then i guess i will have to revert to the Paddings set to 0 and a slightly thicker noticeable line.

thanks gustav and mbizup and irogsinta!! = ))
Yes, I realised it was a "home-made" grid.
You may wish to check of a form with a subform where the subform is in datasheetview.

/gustav