Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Invoke Word Mail Merge from Access2007


Mail Merge is a powerful feature in Microsoft Word for creating and printing form letters.  For example Employment Contracts with fixed employment terms, but with changing personal details, like name, address, basic pay, allowances, contract period, etc., can have several pages. Normally this will be a preprinted document with standard terms of the contract and fill-in-the-dotted lines for personal details (that can be hand-filled or typewritten) model document.  But, this can be printed in the original, with personal details automatically filled-in in appropriate locations on the document with the standard terms of the contract.  This action can be automated and printed for several employees together within minutes.

All we need to do is to create a Table with personal details of employees; design a single Mail Merge Word Document with standard terms of the employment contract, link the Access table, with the Word Document, and insert the personal detail fields, wherever applicable to the document.  Merge-print the document to the printer.

In MS-Access2007 part of this mail-merge action can be invoked by selecting a Table or Query to link with the Mail-Merge Document in Microsoft Word directly.  You can link the Table/Query to an existing Word document or create a new document linked with the selected Table or Query.  Let us create a sample document to understand how to do this from Microsoft Access2007.

Prepare for a Demo Run.

  1. Open Microsoft Access2007.

  2. Open the sample database Northwind2007.accdb or Import the Employees table from this database into any database that you wish to open to try this out.

  3. Click on the Employees Table in the navigation pane to select it.

  4. Select External Data - - > Export - - > More - - > Merge it with Microsoft Word.

    Microsoft Word Mail-Merge Wizard opens up and gives you two choices; either to open an existing Word document or to create a new one and link the Employees Table with the document. 

    Depending on what option you have selected the Employee Table is linked to that document.

  5. Select the Word Menu Mailings - - > Insert Merge Fields to display the linked Employees Table field list. See the image given below.

  6. Now, you must place the attached Table's data fields in appropriate locations on the Document's body to insert their contents there.  I have placed the name and address of the attached Employees Table on the Document.  The sample Document image is given below:

  7. Place the insertion point where you want the field content to appear, then select the field from the Mailings - - > Insert Merge Fields Menu.

  8. Repeat this action to place other fields in appropriate places in the document.

  9. You can preview the Document by selecting Mailings - - > Preview Results at any stage of the preparation of the document.  The sample preview of the above test fields is given below:

If the Employees table has 50 records in it, then 50 copies of this Document will be printed when you select the option Mailings - - > Finish & Merge - - > Print Documents; i.e., one document for each employee with their respective personal details merged in.

Technorati Tags:

Copy Paste Data from Excel to Access2007


Methods are already built into office applications to transfer data between them. Importing/Exporting data or directly linking to it while maintaining the original data in the parent application.  Transfer data into the Clipboard (Copy) and Paste them into another.   All these methods were already there.

In earlier Access Versions you need to create a Table with matching data field types to Paste or Paste - -> Append data into them.  In Access2007 this is made even better now.  For example, you don't need a Table to paste data from Excel into Access2007.  Access2007 will ask you whether you have included the header lines in the copied data or not.  If your answers are Yes Access2007 create a Table ( with the worksheet name) and paste the data into it with the correct data types.

Let us find out How?

  1. Open Microsoft Excel and create a small database with the sample data given below:

  2. Open Microsoft Access2007 and open an existing .accdb database or create a new one.

  3. Make the Excel database window active.

  4. Highlight the Excel database range including the header row.

  5. Select Copy from Home Menu to transfer the data into the Clipboard.

  6. Make the Access2007 database window active.

  7. Right-click on the Navigation Pane of Tables and select Paste from the shortcut menu.  The following message box is displayed:

  8. If you have included the header line of the data when you have copied them, then you may click on the Yes Command Button otherwise select No.

A new Table will be created with the Worksheet name.  The header cell values will be used as field names.  The field data type (Text, Date, Number, etc.) will be correctly defined depending on the type of data that you have copied from excel.

If you have selected No then the data will still be pasted into a new table, but the field names will be like F1, F2, F3, etc.

Technorati Tags:
  1. Roundup Excel Function in MS-Access
  2. Proper Excel Function 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 Access2007
  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 Excel Database Directly
  12. Create Excel, Word Files from Access

User-level Access Security and Access2007


User-level/Object-level Access Security in Access versions earlier than Access2007.  All my Microsoft Access Applications were secured with User-level/Group-level Security and it is so effective when applications are shared on a Network.  I have never locked the VBA Modules with the password or converted the applications into a compiled state (converting to MDE format) to protect them from tampering.  User-level security provides enough resources to place a particular user where we want him to be while using the application. 

If you would like to use this feature in Access2007 then you should not convert the earlier version of databases (with .mdb extension) into Access2007 (.accdb format).  If you do then all the User-level security features are removed and you cannot set them back into the new version.  You can continue to maintain and use the User-level security features in Access2007 if your databases are in Access2003 or earlier formats (databases with .mdb extensions).

Database Objects and Permissions

With User-level Security you can control what users can do and what they should not do.  Check the following table to get some idea as to how to set permissions on each object type and what they do:

Permission Applies to these objects Result
Open/Run Entire database, forms, reports, macros Users can open or run the object, including procedures in code modules.
Open Exclusive Entire database Users can open a database and lock out other users.
Read Design Tables, queries, forms, macros, code modules Users can open the listed objects in the Design view. Note: Whenever you grant access to the data in a table or query by assigning another permission, such as Read Data or Update Data, you also grant Read Design permissions because the design must be visible to correctly present and view the data.
Modify Design Tables, queries, forms, macros, code modules Users can change the design of the listed objects.
Administer The entire database, tables, queries, forms, macros, code modules Users can assign permissions to the listed objects, even when the user or group does not own the object.
Read Data Tables, queries Users can read the data in a table or query. To grant user permissions to read queries, you must also give those user permissions to read the parent tables or queries. This setting implies Read Design permission, which means that users can read your table or query design in addition to the data.
Update Data Tables, queries Users can update the data in a table or query. Users must have permission to update the parent table or queries. This setting implies both Read Design and Read Data permissions.
Insert Data Tables, queries Users can insert data into a table or query. For queries, users must have permission to insert data into the parent tables or queries. This setting implies both Read Data and Read Design permissions.
Delete Data Tables, queries Users can delete data from a table or query. For queries, users must have permission to delete data from the parent tables or queries. This setting implies both Read Data and Read Design permissions.
Technorati Tags:

Earlier Post Link References:


Macros and Temporary Variables


If you are using Microsoft Access 2007 or a later version of Access then there is something new for you. You can use the SetTempVar action in Macros to define Global Variables.  After that, you can use the values in those variables in another macro or in an event procedure, or on the Form or Report. The temporary variables remain in memory till you clear them with the RemoveTempvar macro action or remove all the variables with RemoveAllTempVars macro action. All variables will be cleared from memory when you close the database.

The TempVar Usage in Macros.

Let us try a quick example to understand the usage in macro:

  1. Select Macros from the Create Menu.

  2. Select SetTempVar Action in the first row.

  3. Type myName in the Name argument.

  4. Type the expression Inputbox(“Type your Name”) in the expression argument.

  5. Save the Macro with a name (say macDefineVar).

  6. Right-click on the macro and select Run from the shortcut menu (or Double-click) to execute the Macro.  The Inputbox() Function will run and will prompt for a value to type.

  7. Type your name and click the OK Command Button.

    Your name is stored in the Variable myName. We have used the Function, InputBox() in the expression argument.  You can use constant values, functions, or expressions to assign values to the variable myName.

  8. Open a new form in the design view.

  9. Insert a Text Box in the details section of the Form.

  10. Type the expression =Tempvars!myName in the Control Source property.

  11. Change the form from Design view to Form View.

Your name will be displayed in the text box.  The above example shows how to define a temporary variable and how to reference it in expressions on a Form.  Let us learn how to remove this variable from memory.

  1. Close the Form.

  2. Select Macro from the Create menu to open up a new macro in the design view.

  3. Select RemoveTempvar from the Action list.

  4. Type myName in the Name parameter.

  5. Save the macro with the name macRemoveVar.

  6. Double-Click on the macRemoveVar macro to execute it.

  7. Open the form again to check whether your name still appears on the text box on the form or not.

The text box will be empty, indicating that the variable myName does not exist in memory.  RemoveTempvar action needs a variable name as a parameter.

TempVar Usage in Query.

Let us do something better than this and more useful in real-world solutions.  Let us calculate the Order-wise Percentage on the sum of Orders Quantity.  We have worked on this problem earlier in the blog post: Percentage on Total Query.  What we need here mainly is the sum of Orders' Quantity to calculate individual order percentages.  In the earlier example, we have used a separate Query to calculate the sum of Orders and linked it with a second Query on Order Number to calculate the percentage.

Here we will initialize a Temporary Variable with the sum of Quantity and use the Variable name in the percentage calculation expression.

  1. Import the Order Details table from the Northwind sample database.

  2. Select Query Design from the Create menu; don't select any table or query from the displayed list.

  3. Change the Query in SQL view; copy and paste the following SQL string and save the Query with the name OrderPercentageQ:

    SELECT [Order Details].OrderID, First([Order Details].UnitPrice) AS UnitPrice, Sum([Order Details].Quantity) AS Quantity
    FROM [Order Details]
    GROUP BY [Order Details].OrderID;
  4. Open the macro macDefineVar in the design view.

  5. Change the variable name, myName to TotalQuantity (myName variable will remain in memory).

  6. Change the expression InputBox(“Enter your Name”) to DSum(“Quantity”,”[Order Details]”).  Do not add the = symbol at the beginning of the expression.  Save the macro with the change.

  7. Double-click on the macro to run and calculate the total quantity and store the value into the temporary variable TotalQuantity.

    We will modify the OrderPercentageQ with the addition of a new column that calculates the order-wise percentage of total orders.

  8. Open a new Query in SQL View.

  9. Copy and Paste the following SQL String into the SQL editing window of the new Query and save it with the name OrderPercentageQ2:

    SELECT [Order Details].OrderID, First([Order Details].UnitPrice) AS UnitPrice, Sum([Order Details].Quantity) AS Qty, Sum([quantity])/[tempvars]![totalQuantity]*100 AS Percentage
    FROM [Order Details]
    GROUP BY [Order Details].OrderID;
  10. Open the Query in Design View and check how we have written the expression in the last column to calculate the percentage using the temporary variable [tempvars]![totalQuantity].

  11. Change the Query View into Datasheet View to display the Order-wise percentage of Total Quantity.

The TempVar Usage in VBA.

We can work with the Temporary Variable (Tempvars Object) in VBA.

With the Add method, we can define a Temporary Variable and assign an initial value to it.

Add() method of TempVars Object:

    Syntax: TempVars.Add "Variable Name","Initial Value"

    Example-1: TempVars.Add "TotalQuantity", DSum("Quantity", "[Order Details]")


    Example-2: TempVars!TotalQuantity =  DSum("Quantity", "[Order Details]")

You can define a total of 255 temporary variables in this way. 

Remove method of TempVars Object:

The Remove() method erases only one variable and frees the memory occupied by the variable.

    Syntax: TempVars.Remove "Variable Name"

    Example: TempVars.Remove "TotalQuantity"

RemoveAll method of TempVars Object:

The RemoveAll() removes all the temporary variables defined with the Add() method.

    Syntax: TempVars.RemoveAll

    Example: TempVars.RemoveAll

The Count property gives the count of all temporary variables defined in memory:

Example: Debug.Print TempVars.Count returns the count of temporary variables defined in memory

TempVars Item Indexes.

Each temporary variable defined in memory has an index number starting from 0 to the total number of such variables in memory –1.  Each variable can be referenced by the Item index and can be used to read the Name of the variable or its Value or to assign new values to the variable.

Debug.Print TempVars.Item(0).Name prints the name of the variable.

SumofQuantity = TempVars.Item(0).Value

You can also use this reference to modify the existing value in the temporary variable.

TempVars.Item(0).Value = TempVars.Item(0).Value + 1

You should not use a subscript beyond the existing number of temporary variables in memory otherwise an error will occur.  If you have defined 5 variables, then the valid index numbers are 0 to 4.

Earlier Post Link References:





Post Feed


Popular Posts

Blog Archive

Powered by Blogger.


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