VS Tools/VBA - Populating a dropdown list

This is one of the cooler pieces of code that I've come across in a while.

This technique is coded using VS Tools, but it was ported from VBA code so it'll run just fine in VBA. You'll have to edit the data access code in that case to use ADODB.

The object here is to place a dropdown list on a NATIVE Dynamics form (not one that you added using .NET) and then to populate the dropdown with data.

This piece of code uses 'Pass through SanScript' to do the deed. It's not too complicated and it works just fine, well worth studying.

If I've not explained it clearly please let me know, I'll help.

Edit 4/30:

A couple of notes, now that I've had the weekend to go over this.

First, this technique was originally blogged by David Musgrave in 2008: http://blogs.msdn.com/b/developingfordynamicsgp/archive/2008/07/24/hybrid-dynamically-populating-a-modifier-added-list.aspx. Props to David. There are a lot of cool things that might be done using this approach. I'd realy love to hear anyone's thoughts.

If you don't follow David's blog... you should. He's the author of Menus for VS Tools and spoke (OK, evangelized) at Convergence 2012 on the Support Debugging Tools for Dynamics.

Second, after coding the example below I discovered that if you were to use this technique to put 'A' and 'C' into a dropdown list, the 'value' that list returns would be '1' and '2'. If the dataset changed in the future to 'A', 'B' and 'C', then 'C' would now return a value of 3. That would cause trouble with saving the 'value' in the database; so I'm recommending this technique only for items that remain static (like SOP Order Types)

 

Sub OpenAfterOriginal(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
        Dim CompilerApp As Object
        Dim CompilerMessage As String = ""
        Dim CompilerError As Integer
        Dim CompilerCommand As String = ""
        CompilerApp = CreateObject("Dynamics.Application")
 
        'get a dataset of items that we want to add to the dropdown list
        Dim oDT As DataTable = RLData.SPs.Dyn_MC40301_SEL().getTable
        For Each oRow As DataRow In oDT.Rows
            'loop through the datatable
 
            'format the SanScript commant
            CompilerCommand = String.Format("add item ""{0}""  to '(L) Exchange' of window GL_Account_Maintenance of form GL_Account_Maintenance;", oRow("EXGTBLID"))
            'MsgBox(CompilerCommand)
 
            ' Execute SanScript   
            CompilerApp.CurrentProductID = 0 'Dynamics
            CompilerApp.CurrentProduct = CompilerApp.CurrentProduct & "!Modified"
            CompilerError = CompilerApp.ExecuteSanscript(CompilerCommand, CompilerMessage)
            If CompilerError <> 0 Then
                Throw New Exception(CompilerMessage)
            End If
 
        Next
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, True)
    End Try
 
End Sub

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences