Dynamically Add Option Buttons to Excel UserFrm with One Event Handler - vba

I have a custom user form in Excel where the number of option buttons is dynamic, depending on what a user selects from a combo box. I have written a script that will add and format those option buttons with appropriate captions, groupname, etc. However, I would like it so that after those buttons are added dynamically, if a user clicks a button from a group just added, then more code will be executed. I am aware that one can create a custom class so that multiple userform controls are handled with one event handler, but I can't figure out how to get this to work with controls that were added dynamically to a form, after the form initialized. I am hoping this question makes sense. Thanks in advance for any help you can offer.

Related

in Access, Bound Combo Boxes do not update unless the form is already Dirty

I'm working on a fairly complex Access Database, trying to build forms with custom buttons for working with records. I'm using list boxes to display and navigate through records and all fields for existing records are disabled unless the user presses an edit button. The problem I'm having is that if I press the Edit or Add New button, enabling all of the fields, and then try to change the selection in a combo box, it does not update on the first try.
If I edit a text box first then the combo boxes work fine.
I've determined that the control's beforeUpdate and afterUpdate events are not firing on the first try but the action triggers the form_Dirty event and then it works as expected. I tried setting Me.Dirty = True with the Edit button and that solved the problem but it causes problems with some of my other code and it seems like an unnecessary workaround if only I understood the actual cause of the problem. It also works as expected if I leave the Combo Box unbound, but I am trying to build a template that doesn't require too much work to build new forms off of and I would rather not go that route.
It must have something to do with some bit of code I'm using because I can start a basic form and the combo boxes work fine.
I've started a basic test form and am adding code from my template form bit by bit until the problem arises, but it's a tedious process. Any help would be appreciated.
What am I missing? Is there some way of getting the Combo Box events to fire before the form is dirty?
UPDATE:
I have templates for a basic form, a form with a single subform, and a form with multiple subs in a tab control.
After some more testing I discovered that this problem does not apply to the basic form which has no subforms. This template uses similar code to the others for new, edit, cancel, save and delete buttons and for preventing accidental changes, preventing Form_unload during an edit and so on. The main difference I can think of off the top of my head is that the templates with subforms use class modules and collections to hold various data and pass it between the main form and subforms. Not sure if or how this might relate to combo box functionality.
I built most of this last winter then got too busy over the summer to work on it. Just now picking it up again and I'm having to re-learn a lot of the details of how my code works.
you can try to use Me.CboName.Requery in your After Update event.
I figured it out. I was calling a procedure with a form refresh in it from the Form_Dirty event and for whatever reason the refresh at exactly the wrong time was causing the combo box Before_Update and After_Update events to be skipped and the combo box value to not change. It was also causing text entered into a textbox to overwrite existing text on the first keystroke.

How to use Mouseout Function in VBA with a condition Check?

Hi I have a VBA application with a combobox selection option.One of the combobox option is Other which when user select that option an invisibled label will pops up and prompt the user to fill the txtOther control.now my question is how I can get rid of the prompted label and make it to invisible after user fill the txtOther and move(focused)in other control?
here is a shot of my app:
Thanks for your time and help
It depends on how often or when you want the check to be done. I couldn't find a good reference to show you all of this, but you can view your VBE.
Within the code behind your userform you can use events for the text box.
If you want the check done every time you leave the text box:
Use the TextBox_Exit event.
If you want it to fire whenever the contents are changed, used TextBox_Change. There's lots of options, but based on your explanation I'd probably use Exit.
This answer shows some syntax examples for using the Textbox_Exit event.

VB.NET/Visual Studio 2010, incrementing control names at runtime

I am coding an invoicing application. The user should be able to add a new line to the billing section of the invoice by selecting a product from a ComboBox, which subsequently fills out various other fields (price, description, etc) based on the selected product.
My problem is this: When the user adds a product, I would like the program to automatically generate a new line for the next product. It seems like the only way to accomplish this would be to create a new set of controls with an incremented name, i.e. Product1, Price1, Desc1 for the first product, Product2, Price2, Desc2 for the second product, etc. Is this possible, or am I going about it all wrong?
I had to do something like this at one point and what I did was create a custome user control, in your case it would consist of a combo box, that is filled with products on load, and all the related controls, which would be filled out on the selectedindexchanged event of the combo box. On your main form where you want to display this info place a panel and add your custom user control to the panels.controls collection.
since all the functionality will basically be contained in the usercontrol itself you shouldn't run into naming issues.
As for adding the user control when the user makes a selection, you could dim the control withevents and attach a custom event to the user control and handle it in the main form. since the controls would be added at run time you would need to attach the event handlers using the AddHandler call.
So to break it down you would need:
A Custom user control with all the controls you want for each product and an ID in the code behind
An event on your custom user control to tell the main form that the combo box has a selected value
A panel on your main form to hold the custom user controls
An event handler to add a new customer user control to the panel when a selection is made in the combo box.
if you want to interact with a certain product with the same button you can attach an ID in the code behind of the user control and set that everytime a new user control is added. this is not as hard it I think I make it sound, you can actually run linq on the panels control collection and get all the panels where any condition you designate is met and then do what you want from there.
As for your second question, I would assume your user control is pulling a bogus connection string from somewhere. I would try to track down where that is happening and go from there. Anything beyond that would just be a WAG and that doesn't really help you.

Option Group frame: can I add text boxes that are part of the frame instead of rad button options?

Ok so this maybe a simple/silly question but I don't know so here goes:
In access let's say I want to have a frame control, so I click the option group button and add it to the desgin surface. However, I am not wanting to use this as a option group with radio button selection, instead I would like to add text boxes instead the frame, so that when I reference the frame, it references every control instead of it, hence the text boxes, cbo boxes, etc.....just as it would if they were radio option selections.
So can you do this?
I want whatever controls I add inside the frame to be easily referenced (i.e. make all controls visible just by using frameExample.visible = true) so that I can build my own tab control groupings.....
can this be done?
Thanks!
EDIT:
What I am trying to accomplish is having a form that includes a collection of controls (input controls - cbo boxes, text boxes, etc), that serve as the Main record information. These are saved to a table via an INSERT statement on button_click because this form is unbound.
Next I have 8 categories that are relative per each main record (and data that goes along with it). Each of these categories could have a sub form area and a button click that bring it's relative form into the sub form area. These sub forms would be unbound as well as I would just save data via SQL statement. So i know I could accomplish this by running the insert statement from the parent form, on the main collection control's data that would create the KeyID number, then run a SQL statement that would turn around and load that KeyID number right back onto the page in a hidden text box.
Then when I click one of the sub forms and load its relative collection of controls, I could then save that data along with KeyID for each of these sub-forms/tables.
SO......
I was wondering if instead you could define these controls as a collection so that you could hide and make visible all the ones you need on button clicks and avoid the need for additional forms (subs). I know that if a user enters data into a text box, and then somewhere along the way that box becomes hidden, the data still exists in it and still ends up in the SQL statement....
So I want all these controls to exist on the same form, but I thought what is I could encapsulate them into a frame like an option group, then I could call the frame and all the relative controls would be called up (made visible) as needed.
Sorry for the long explanation but I thought it would help.
I do not think you can do it with an Option Group, but what you are describing is pretty much a subform, yesno?
Some examples of hiding the tab control from an app that went live in March 1998:
Tab driven by transparent command buttons over labels styled to look like colored command buttons:
Same approach, more buttons:
In this case, fake colored command buttons don't drive the tab, but insted show/hide the tab and a subform. In this case, the tab is actually driven by the listbox:
A view of when the tab is hidden and the subform revealed. The listbox drives navigation within the subform, which has a visible tab on it:
So, there's a lot that can be done without showing the tab control.

Hiding columns in a datasheet

I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:
txtControl.ColumnHidden = True
This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!
Edit: Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.
Try setting it in either the form's Current or Load events. You will also probably need to requery the control after setting that property: Me.TextControl.Requery Current is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.
I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.
Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery
See if this technique works for your particular situation.