VBA – Determining the company name selected in the Company Login window


Patrick Roth - Click for blog homepageA question came up a while back where the user wanted to know what company the user was logging into in order to give them a message depending on the company selected.


The specific issue was that VBA cannot get the name of the company from the DDL (drop down list), it can only give you the position.  Also the position in the list doesn't have any bearing on the company id of the company displayed.  It is just an ordered list by alphanumeric sorting by the Company Name field.


The question wasn't specific to when exactly the user needed to see this message.  When initially I read the question and based on how it was posed, it seems they wanted to know right when the DDL was selected.  But it could have been when the OK button was pressed and it also could be just after they log in.


Given that the technique used would be different, I will present both solutions.


Situation 1


For this solution, we will assume that the programmer needs to know the company after the user logs into Dynamics GP in order to give them some information or take a specific action.


The easiest way to do this it to use a Window_AfterClose() event on the Company Login window (Switch_Company form in Dexterity) and use the UserInfoGet object to retrieve the company logged in.


In the VBA code sample below, before the window opens we see what the current company is.  We could either being logging into Dynamics GP for the first time or just switching companies.  And if switching companies, we might press Cancel or just re-select the same company.  So by checking the previous logged in company and comparing we only show our message if the company we are going to really changed.


VBA Code for Company Login window

Situation 2


While the solution given for the first situation is OK if we want to let the user log in, it is possible that we might want to give a message before the GP login occurs.  Or potentially stop the user from being able to log into the company for some specific reason.


If that is the case, we would then need to know the selected company before the Company Login window was closed.  The only way to do that is to know the company name selected in the Company DDL.


Because we cannot directly read the name from the Company DDL field, we have to pull the information directly from SQL in the order that Dynamics is displaying it.  Then we'd know the company being selected and could act upon that.


VBA Code for Company Login window 2

 

Depending on your exact needs; either one of these solutions might work for you or at least get you started.

 

Best Regards,

Patrick

Developer Support

// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

Comments (3)

  1. Jeff L says:

    Hi Patrick – Quick question: How do I add the Switch_Company window to VBA for situation 1?

  2. David Musgrave says:

    Hi Jeff

    To add a modal window to VBA you will need to use the keyboard shortcut keys as you will not be able to access the menus with the mouse.

    Ctrl-F11 to Add the Window to VBA

    Shift-F11 to Add the Fields to VBA

    Hope this helps

    David

Skip to main content