Você está na página 1de 8

Creating a VBA Login Box for Smart View

The Problem: How to Pass Login Credentials using Smart View VBA
I was tasked with creating a VBA solution that connects all the worksheets in a given workbook to a Hyperion application. The solution required looping through each worksheet and connecting using the HypConnect VBA function provided by Oracle as part of the Smart View VBA set of functions . The syntax I used is as follows:

HypConnect("Sheet1","UserDrew","Password","Connection Friendly Name")


If you have no idea what that's doing, I recommend reading Chapter 16 of the Smart View User's Guide. The specific problem I had with this function is that I did not want to explicitly pass my credentials into the VBA code (for obvious reasons). So I set out to create a generic login box that can be used to connect to any number of designated connections. I am working with version 11.1.2.1 using Smart View 64-bit in MS Excel 2010 Professional Plus on Windows 7.

Prerequisites
1. Create and save a new macro-enabled workbook. The workbook I created for this post is called HypConnect.xlsm. 2. Make sure the Developer tab is available in Excel by selecting File > Options and then selecting "Customize Ribbon." Make sure the Developer tab is selected in the right pane as shown below.

3. Smart View VBA functions are required to have this solution work, so we're going to have to import them. Select Visual Basic from the Developer tab. The editor window should open up. Then on the menu click File > Import File...

The file is located at EPM_ORACLE_HOME\SmartView\Bin\smartview.bas. Select it, click Open and it should be located in the VBA Project Pane under modules as SmartViewVBA.

If you're working in 64-bit Excel, open up SmartViewVBA. The last prerequisite you have to take care of is addingPtrSafe to the function declarations. So this:

Becomes this:

Step 1: Create a User Form in Visual Basic


Select Visual Basic from the Developer tab. The editor window should open up. Then on the menu click Insert > UserForm.

A new, blank user form will open in the editor window.

If

you

can't

see

the

toolbox,

click

View

>

Toolbox.

Next let's change two of the properties of the user form. First we'll change the name from UserForm1 to frmLogin. Then change the caption from UserForm1 to Welcome. Just to be... welcoming.

Next we'll add the form labels. Click on the icon with the capital A on it (second from the left on the top row in the toolbox) to add a label.

Left-click and hold down the mouse button while dragging the cross hair to shape up your label. Release when you have a happy, little label. Repeat this again for a second happy, little label.

First, change the name of the first label to lbl_UN and the name of the second label to lbl_PW. Then change the caption of the first label to "Username" and the second to "Password." Then style the label box and font to taste. Here's what I've done.

Next we'll add the form text boxes for credentials input. Click on the icon with the ab| on it (third from the left on the top row in the toolbox) to add text boxes for both username and password. Name them tb_UN and tb_PW, respectively.

In order to make the password entered invisible to the user, also change the PasswordChar property of the tb_PW text box to an asterisk. This will convert inputted text characters to asterisks.

Last, we're going to add a login button to send user credentials to the Smart View VBA HypConnect function. Do this by selecting the Command Button icon in the toolbox (second

from the left, third row from the top) and drawing a button. Change the name of the button to btnLogin and the caption to Log in.

Quick Grammar Lesson: Login can be used as a noun and as an adjective, but not as a verb. So you can log in using your login, but you can't login using your log in. And you can never, ever log in using anyone's login but your own.

Step 2: Adding VBA Code to the Login Button


The next thing we have to do is add some code to the login button so that it actually does something. Right click on the login button and select View Code. Here is the code we're going to add.

The first section declares each of the two parts of the user credentials as string variables. The next section assigns to those variables values from the username and password input text boxes. Then those values are passed explicitly to a procedure we are going to create in the next section: ConnectSheetsToEssbase. The last section unloads and closes the login dialogue box.

Step 3: Adding the ConnectSheetsToEssbase Procedure in a New Module


Now select Insert > Module from the Visual Basic Editor menu. This will open up a new module editor window.

Next from the menu select Insert > Procedure to add our new function. A new dialogue box will pop up to select a few options. Name the procedure ConnectSheetsToEssbase.

A new procedure shell will be added to the editor. Add the following code.

This loops through the worksheets in the active workbook (as opposed to this workbook, or the workbook in which the macro resides, HypConnect.xlsm) and connects them using the Smart View VBA function HypConnect. The 1. 2. 3. 4. four parameters passed are:

The name of the worksheet currently being processed by the loop The username input entered into tb_UN. The password input entered into tb_PW. A Smart View connection friendly name. This was already set up. Some considerations about

this

code:

1. There's no validation for invalid worksheet names, user credentials, or friendly connection names. This should be added.

2. The use of active workbook versus this workbook is non-trivial. By using active workbook, I can keep HypConnect.xlsm open and call the macros in it from the workbook in which I'm working. You could also save it in your personal macro workbook. I really haven't thought through which is preferable, but I suppose creating both and seeing what happens with both is probably the best approach to figuring that out.

Step 4: Loading the Login Form


You may have noticed that I've provided no means up until this point for launching the login dialogue box. I'm going to add another new procedure in HypConnect.xlsm called GoLogin. Here is what it looks like.

You can assign a keystroke combination to the macro to bring up the login box, or whatever means you prefer for doing that sort of thing.

More Considerations
The reason that I separated the login button click function from the actual connection procedure was my insane desire to keep my code completely DRY. In this way, the user form can be used in many other procedures and/or other parameters could be added to connection procedures, all while minimizing the amount of new code (read: pain) required to add functionality in the future. And also: validation, validation, validation, and error handling. Don't go implement this without doing any of that fun stuff. Without it you're going to be the error handler and that is never fun. Happy connecting!

Você também pode gostar