Ran into a sticky issue the other day where a customer wanted a drop down list to be bound to a table (they put it as enforced referential integrity), but wanted to allow the users the ability to add values to the dropdown on the fly. The idea that I came up with was to put a button on the form to allow a second form to be opened, allow a new entry to be entered and submitted to the reference table that drove the dropdown, and to automatically close that form. The issue was that the new value was not automatically available in the dropdown of the original form. It’s easy enough to put a second button on the form to requery all datasources, but that wasn’t very elegant. I pinged our internal guru group and got a good response from Alex Vaynshteyn, that I’ve posted the meat from below.
Use “submit via code” in the secondary form; after submitting the new record, do something similar to this:
// submit the secondary form (the one that adds records)
masterForm = Application.XDocuments(0);
// close the secondary form
I have a few concerns about the approach above, that will require some refinement. First and foremost, the way you submit programmatically is like so:
The way you programmatically close a form is like so:
Why I think this is not fully baked is that it’s possible to have multiple forms open at once, and how you get the right index value in the Application.XDocuments() collection can be a challenge. When I asked Alex about this in more detail he referred me to documentation on the XDocuments object model to learn more about how to reference the collection members by name instead of index. I have not dug into this any deeper, but invite you to by following this link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/xdproItem_2_HV01021295.asp and learning more.
Newflash! A useful post from a reader (Crazy Raymond) in response to this article:
One thing concerning your article: “Adding Data to Dropdowns and Requerying in One Step”. You were worred about using XDocuments(0). I don’t think you have to use the collection at all. The XDocument object refers to the current form automatically. So, you can just use:
for a secondary data connection.