Display Excel Value Directly on Form
We have already seen how to open dBase Files directly and work with the data in VBA in the earlier Post Opening dBase File Directly. But, there are other alternatives (besides linking the files) to work with dBase Files in Access. We will explore this aspect in detail later. This Post was originally intended for a demo of opening Excel based Data Table directly and working with it in VBA Code. We will do that in the next Post. Before that we will look into some interesting and very easy trick with Excel.
Why I said very easy, because you don’t have to struggle with VBA Code to do this. I know you will be happier if you don’t have to work with my spaghetti VBA Code. So, I thought we will take a break and do something different and interesting before continuing with databases.
You got the clue from the title itself. That’s right, you can display Values from Excel Worksheet directly on Microsoft Access Forms. We will go straight into a Demo.
- Open Microsoft Excel.
- Enter your name in Cell A1 on Sheet1 and minimize Excel, don’t close it. You may save the Workbook with a name before minimizing it. The WorkBook may keep interrupting for saving it, if Auto-save feature is on.
- Open any one of your Microsoft Access Databases or create a new one.
- Open a New Form in Design View.
- Select the Text Box Tool from the Tool Box and draw a Text Box on the Detail Section of the Form.
- Click on the Text Box and display the Property Sheet (View – -> Properties).
- Write the following expression in the Control Source Property of the Text Box:
- Save the Form with a name of your choice.
- Open the Form in Normal View. You will see your Name written on Excel Cell is appearing in the Text Box on the Form.
The DDE() Function stands for Dynamic Data Exchange. The first two Parameters are Excel Application and worksheet name Sheet1 and the third one is the Cell Reference where your name is written in R1C1 (Row-1 Column-1 or Cell A1). The Cell reference must be used in this way rather than A1 style.
But, there are chances that you may end up with nothing showing on the Text Box. In that case you have to see a particular Option is correctly set in the General Tab of Options. . . in Excel Tools Menu.
- Click on the Excel icon on the Taskbar to open it. Click on Tools – -> Options. . . and select the General Tab.
- Remove the check mark (if it is set) from the option Ignore Other Applications under the Settings Options.
- Minimize Excel, Close MS-Access Form and open it again. Now you must see your name from the Excel Cell on the Form. Don’t close the Form.
- Maximize Excel again and make some change in your Name and Minimize again. The change may not reflect immediately on the Form. You can update the Form value manually without closing and opening the Form again.
- While the Access Form is in Normal View, select OLE/DDE Links from Edit Menu.
- The Links Dialog Box will open up. Select DDE Links in the Links Group control. The Link’s list will appear below. Select the Link and click on the Update Now Command Button. Now the change on the Worksheet will reflect on the Form Value.
There are Option settings in MS-Access also similar to the one we have made in Excel. The general rule is that instead of refreshing the change manually, as we did above, it should happen automatically at a fixed interval as per the Tools – – > Option. . . settings in Microsoft Access, but I didn’t see it happen successfully on my machine so far.
Select Tools – -> Options. . . – -> Advanced Tab. Put a check mark in Enable DDE Refresh option, if it is not set. You can see the default Values set for automatic refreshing for Dynamic Data Exchange and ODBC links, how many retries and in case of failures how long Microsoft Access should wait before trying again etc.
You can display Values from Excel in two more Access Controls, in Combo Boxes and Option Group Control. See the sample image given below.
- Minimize Access and Maximize Excel.
- Enter few Names of people, books or anything else in Cells A9 to A17.
- Enter Value 2 in Cell C1 and minimize Excel Application Window.
- Maximize MS-Access and open the Form in Design View.
- Disable the Control Wizards Button (top right button, if it is active) on the Tool Box. Select the Combo Box Tool and draw a Combo Box Control on the form in such a way that it looks like a List Box. See the sample image given above.
- Display the Property Sheet and write the expression
in the Control Source Property of the Combo Box.
You may give a Name to the Range: R9C1:R17C1 and use that Name in the expression replacing the Range Address. To Name the Excel Cell Range, highlight the Range, select Insert – -> Name – – > Define and type the name, say List, in the Names in Workbook control. Replace the third Parameter R9C1:R17C1 with the Range Name List in the DDE Function.
- Open the Form in Normal View. The Names from the Excel Range will appear in the Combo Box.
Note:You cannot select any of these values and insert into the Text Box portion of the Combo Box and use it in a data field.
We will try one more example with the Option Group Control.
- Turn On the Control Wizard on the Tool Box. Select the Option Group Tool from the Tool Box and Draw an Option Group Control on Form. Refer the example given above.
- Type three Labels in the Wizard: Data View, Print Preview and Print, or anything else you prefer, on the control and Click Finish.
- Click on the outer frame of the Option Group and display the Property Sheet (View -> Properties).
- Write the following expression in the Control Source Property:
The selection of item on the Option Group will be based on the Value given in the Excel Cell C1. Now the value in Cell C1 is 2. The second item on the Option group will now show as selected. Change the Value in Cell C1 to 3 and refresh the Form as explained above, the option will change to 3.
Note: If the Excel Application is not active when you open the Form with the DDE() Function then Access will show the following Error Message:
"You tried to open a form or report that includes a DDE or DDESend function in a calculated control that specifies an OLE server application."
"Do you want to start the application Excel?"
If you respond with Yes then Excel Application will be started with Blank Sheets. You must open the Excel Workbook that provides information for DDE() Function manually to show up the values in Access Form.