Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Opening Excel Database directly

Introduction

In the world of Personal Computers, many applications emerged and disappeared. One of the very popular applications was WordStar (Word Processor), used under Operating Systems CP/M-80 (Control Program for Microcomputers) and MS-DOS (Disk Operating System) till 1992. There were dedicated Word Processing Machines in those days, using WordStar and competing Software WordPerfect

The first spreadsheet program, VisiCalc, was released in 1979 and made a groundbreaking impact. It soon inspired several successors—SuperCalc, Multiplan, Lotus 1-2-3 (1983), and eventually Microsoft Excel (1985)—each adding more power and functionality than the original VisiCalc. (Source: Wikipedia)

Like Microsoft Access, Microsoft Excel also includes powerful automation capabilities that make spreadsheet-based computing both flexible and engaging.

Spreadsheet programs are designed for analytical tasks that involve a chain of calculations, with all relevant data visible on the sheet. A small change made at the beginning of a calculation can trigger a cascading effect through dependent cells, instantly updating the final results. This makes Excel an invaluable tool for “what-if” analysis. And when it comes to creating graphical charts, Microsoft Excel remains the first name that comes to mind.

Excel Table

Although Excel includes limited database-like features, such as built-in functions, filters, and sorting capabilities, it can still be used effectively for structured data management. This brings us to our next example—creating a table in Excel that can be read from or updated by Microsoft Access.

While Excel provides the tools needed to follow general database principles when creating and maintaining a table, these rules are often overlooked. Users are free to design their worksheets in any manner, even mixing regular data and database-like tables side by side on the same sheet.

In contrast, Access enforces strict data integrity rules—for example, text cannot be entered into numeric fields, field names must be unique, and data cannot exceed defined field sizes. The same principles technically apply in Excel, but violations often go unnoticed because Excel doesn’t always flag them. As a result, the worksheet may not function as intended when used as a data source.

Moreover, not all Access data types exist in Excel—for instance, the True/False field type in Access has no direct equivalent, although Boolean values like TRUE and FALSE are still valid entries in Excel cells.

Before we create a database-style table in Excel and open it directly in Access (as we did with a dBase file), let’s explore how to set data validation rules in Excel to restrict cell entries. You may be surprised by how powerful these validation features can be.

Validation Settings.

Example: Accept only values between 25 and 100 in a Cell or Cells.

  1. Open Excel and select a Cell in Sheet1.

  2. Select Data -> Validation ...  -> Settings.

  3. Select Whole Number in the Allow control.

  4. Enter 25 in the Minimum Control and 100 in the Maximum Control.

  5. Select the Input Message Tab. Enter Age in the Title Control and type Enter Value between 25 and 100 in the Input Message.

  6. Select the Error Alert Tab and type Value Error in the Title Control.

  7. Type Valid Value between 25 and 100 in the Error Message Control and click OK to close it.

Try entering a value less than 25 or greater than 100 in this cell, and it will display the Error message that you have set up in the Validation Control.

You can apply the same rules quickly to other cells. Copy the Cell, highlight the Range of Target Cells, and select Edit -> Paste Special -> Validation. If you paste it over existing data, it will not validate the field contents if the wrong value is already present in the Cell. The validation check is performed only when you manually key in values.

Following the same procedure, try setting a validation rule in a Cell to accept only Text Length is Less Than 15 characters. Try entering 16 characters or more into that Cell.

When planning to create a database in Excel, begin by defining short, meaningful headings in the top row—these will serve as your field names. Next, apply data entry rules (as explained earlier) to each column so that only valid values can be entered. Excel also provides a built-in Data Entry and Search Form, similar to the one available in Microsoft Access.

If you already have a data table in Excel, simply click anywhere within the table and select Form from the Data menu. This will open a Data Entry/Search Form. By clicking the Criteria button, you can switch it into search mode, where you can enter specific criteria in any field to locate a record.

You can experiment with this feature after we create an Excel table, which we’ll later open directly in MS Access through a VBA program.

Preparing for a Trial Run.

  1. Open Microsoft Excel (if it is already closed).

  2. Open the NorthWind.mdb sample database. Check the link Saving Data on Forms not in Table for location references, if you don't know where this file can be found.

  3. Open the Categories Table in Datasheet View.

  4. Right-click on the top left corner of the Datasheet View and select Copy from the shortcut menu.

  5. Click on the Excel Icon on the Taskbar to open it and select Cell A1.

  6. Select Paste from the Edit Menu.

  7. While the highlighting is still on the pasted Table, select Insert -> Name -> Define and type Categories in the Names in Workbook control, click the OK button to close it.

  8. Save the Workbook with the name: C:\My Documents\myData.Xls, and close Microsoft Excel and close the Northwind.mdb sample Database.

  9. Open any one of your Databases or create a new one.

  10. Copy and paste the following code into the Global VBA Code Module of your Database and keep the Module open. You may save the Module by selecting the Save Toolbar Button or with the File -> Save option.

    Public Sub OpenDirectExcel()
    '-----------------------------------------------------
    'Open Excel Table directly
    'Author : a.p.r. pillai
    'URL    : www.msaccesstips.com
    'All Rights(c) Reserved by msaccesstips.com
    '-----------------------------------------------------
    Dim db As Database, rst As Recordset
    Dim strSql As String, i As Integer
    Dim msg As String
    
    strSql = "SELECT Categories.* FROM Categories IN 'C:\My Documents\myData.xls'[Excel 5.0;];"
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSql, dbOpenDynaset)
    
    i = 0
    With rst 
    msg = ""
    Do While Not .EOF And i < 5
       msg = msg & ![Category Name] & vbCr
       If ![Category Name] = "Confections" Then
          .Edit
          ![Category Name] = "Chocolates"
          .Update
       End If
       i = i + 1
       .MoveNext
    Loop
    .Close
    End With
    
    MsgBox msg, , "Product Categories"
    
    Set rst = Nothing
    Set db = Nothing
    
    End Sub
    
  11. Click in the middle of the Code and press F5 to run it. Displays the Product Category Names of the first five Records from the Categories Table in C:\My Documents\myData.xls in a Message Box.

Note: In the VBA Code, we have tested the Category Names Field for the value Confectionaries, and updated the Value Chocolates back into the Excel Cell, overwriting the word Confectionaries.

Check the SQL Syntax in the Code that pulls the data directly from the Named Range Categories in C:\My Documents\myData.xls file.

  1. Microsoft Excel and Automation
  2. Microsoft Excel and Automation-2

Next: Database Connection String Properties

Earlier Post Link References:

  1. Roundup Function
    of Excel in MS-Access
  2. Proper Function of Excel in Microsoft Access
  3. Appending Data from Excel to Access
  4. Writing Excel Data Directly into Access
  5. Printing MS-Access Report from Excel
  6. Copy-Paste Data From Excel to Access 2007
  7. Microsoft Excel Power in MS-Access
  8. Rounding Function MROUND of Excel
  9. MS-Access Live Data in Excel
  10. Access Live Data in Excel- 2
  11. Opening an Excel Database Directly
  12. Create an Excel Word File from Access
Share:

1 comment:

  1. [...] tables directly in the Query and add the data into the target table. You can learn this method from here. __________________ http://www.msaccesstips.com (Learn MS-Access Tips and Tricks) Learn [...]

    ReplyDelete

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code