Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Multiple Parameters For Query

Introduction.

Queries are an essential component of data processing, and we rely on them extensively in various ways. One of the main challenges when creating queries is how to filter data in a user-friendly manner, making the process seamless for the user. To address this, we employ several methods that allow users to easily pass values as criteria to the queries.

  1. You can create Parameter Queries by inserting variables, such as, [EnterSalesDate], Into the Criteria row of a query. When run, the query will prompt the user to enter the parameter value, allowing them to filter records directly. To define the data type for a parameter variable, use the Parameters… option from the Query menu while in Design View.

  2. You can place TextBoxes or Combo Boxes on a Form, where the user can enter or select values before running a Report or viewing data. The underlying queries reference these controls in their Criteria rows—for example, Forms![MyForm]![myDateCombo]. Based on the values entered or selected, the queries filter the data accordingly, producing the desired results in Reports or data views.

  3. Another way to filter records is by specifying a range of values. For example, to retrieve Sales records for a particular period, the query criteria for the Sales Date might be Between #01/01/2008# AND #03/31/2008# if constants are used. Alternatively, these values can be dynamically passed from TextBoxes on a Form, allowing the user to specify the date range interactively.

    In such cases, I prefer to create a small table—let’s call it a Parameter Table—with a single record and two fields: StartDate and EndDate. Then, I create a Datasheet Form for this table and embed it as a Sub-Form on the Main Form. This allows the user to conveniently enter the date range values directly into the table.

    This table is included in the main query, with the StartDate and EndDate fields placed in the Criteria row using the expression:

    Between [StartDate] AND [EndDate]

    It is important to note that the Parameter Table should contain only one record; otherwise, the main table’s results will be duplicated if the Parameter Table has multiple records. To prevent this, set the Allow Additions property of the Datasheet Form to No, so the user cannot inadvertently add more records.

    When the user clicks a button to generate the Report or other outputs based on this date range, the Parameter Sub-Form can be refreshed first to update the values in the table. After that, the query can be executed to reflect the latest StartDate and EndDate values.

  4. The above example retrieves all data between StartDate and EndDate. However, sometimes we need to filter specific, non-sequential values—for instance, Employee Codes 1, 5, 7, and 8. In such cases, we are forced to enter the codes manually in the Criteria row of the query, using one of several methods, as illustrated in the sample image below:

Query Parameter Input Methods

I would like to share another method I use to let users select parameter values for reports—by simply checking boxes in a Parameter Table.

For example, assume that our company has several branch offices across the country, and management occasionally requests reports for selected branches. Since branch names remain constant, we can enable users to pick the required branches by placing check marks beside them. The check-marked entries can then serve as criteria for filtering data.

To illustrate this method more clearly (and to keep it simple), let’s use a list of months as an example. We will see how the selected months are used in the criteria of the main query. The image below shows how this list of months appears to the user in a datasheet form, displayed as a subform on the main form.

We will need two queries for this process—one to filter the selected months from the list, and a second (the main query) that uses the results of the first query as parameters to filter data for the report.

The first query should return the values 3, 6, 9, and 12, based on the month selections shown in the image above. The following SQL statement can be used to achieve this result:

Query Name: Month_ParamQ

SELECT Month_Parameter.MTH
FROM Month_Parameter
WHERE (((Month_Parameter.[SELECT])=True));

When the user selects or deselects check marks on the parameter screen, these changes may not immediately update in the underlying Month_Parameter table. To ensure the latest selections are reflected, we must refresh the Month_Parameter subform before opening the report that retrieves data from the main query (which uses the above query as its criteria).

To handle this, include the following statement in the On_Click() event procedure of the Print Preview command button:

Private Sub cmdPreview_Click()
     Me.Month_Parameter.Form.Refresh
     DoCmd.OpenReport "myNewReport", acViewPreview
End Sub

Now, how can the selected months filtered in the Month_ParamQ be used in the Main Query as a criterion? It is easy to look at the third method we have used as a criterion in the first Image given above. I will repeat it below:

IN(1,5,7,8)

Here, we will compare the EmployeeID values with the numbers 1, 5, 7, 8, and select records that match any of these numbers as output.

Similarly, all we need to do here in the Main Query is to write this as a Sub_Query in the Criteria Row to use the Month Values from the Month_ParamQ. The above criteria clause, when written in the form of a sub-query, will look like the following:

IN(SELECT MTH FROM MONTH_PARAMQ)

The User doesn't have to type the Parameter values for the Report, only put check marks on the required items, click a Button, and the Report is ready.

Share:

Form Menu Bars and Toolbars

Introduction.

During the development of a database, most of our time is spent creating tables, defining relationships, designing forms and reports, and planning process steps that transform raw data into meaningful reports, helping users make timely, informed decisions. Microsoft Access provides a wide range of menus and toolbars that make these design tasks relatively easy.

Once development nears completion, our focus shifts to security and usability—specifically, how users will interact with the database in their daily operations and what actions they should or should not perform. We want to prevent users from tampering with forms, reports, or other design elements and unintentionally disrupting the system.

By properly implementing Microsoft Access security features, we can control what each user or group is allowed to do. Additionally, removing the default menu bars and toolbars—and replacing them with custom menus and toolbars tailored to user needs—helps ensure a clean, intuitive interface for everyday use.

The Form/Report-Property Sheet.

When you open a Form or Report in Normal View, certain settings on the Form/Report's Properties influence the display of Menus or Tool Bars associated with them. An image of the Form's Property Sheet is given below:

When you click the drop-down arrow in the Menu Bar property, a list of all custom menu bars you have created (or imported from another database) will appear. You can select the desired menu bar from this list to assign it to the form. Similarly, you can specify custom toolbars and shortcut menu bars in their respective property fields.

You can also enable or disable a form’s shortcut menu by setting its Shortcut Menu property to Yes or No, respectively.

When the form is opened in Normal View, the assigned custom menus and toolbars will automatically appear according to the property settings.

NB: You can go through the following Posts to learn more about designing Custom Menus and Toolbars:

Automating Menus/Toolbars Setting

When your database contains numerous Forms and Reports, opening each one individually in Design View to set these properties manually can quickly drain the enthusiasm you had while building the database. Fortunately, this tedious task can be automated with a simple VBA routine.

This routine can scan the entire database in less than a minute, updating all Forms and Reports by assigning the specified Custom Menu Bar and Tool Bar names to their corresponding properties automatically.

Simply copy and paste the following VBA code into a global module in your database, and then save the module.

Public Function MenuToolbarSetup()
'-----------------------------------------------------------
'Author : a.p.r. pillai
'Date   : September, 1998
'URL    : www.msaccesstips.com
'All Rights Reserved by www.msaccesstips.com
'-----------------------------------------------------------
Dim ctr As Container, doc As Document
Dim docName As String, cdb As Database
Dim msg As String, msgbuttons As Long

On Error GoTo MenuToolbarSetup_Err

Set cdb = CurrentDb
Set ctr = cdb.Containers("Forms")
msgbuttons = vbDefaultButton2 + vbYesNo + vbQuestion
' Set MenuBar, toolbar properties of Forms
msg = "Custom Menus/Toobar Setup on Forms. " & vbCr & vbCr _& "Proceed...?"
If MsgBox(msg, msgbuttons, "MenuToolbarSetup()") = vbNo Then
   GoTo NextStep
End If
For Each doc In ctr.Documents
  docName = doc.Name  
'Open the Form in Design View and hidden mode   
DoCmd.OpenForm docName, acDesign, , , , acHidden
   With Forms(docName)
     .MenuBar = "MyMainMenu"
     .Toolbar = "MyMainToolBar"
     .ShortcutMenu = True
     .ShortcutMenuBar = "MyShortCut"
   End With  
'Save and Close the Form after change
   DoCmd.Close acForm, docName, acSaveYes
Next

NextStep:
'MenuBar,Toolbar properties of Reports
msg = "Custom Menus/Toobar Setup on Reports. " & vbCr & vbCr _& "Proceed...? "

If MsgBox(msg, msgbuttons, "MenuToolbarSetup()") = vbNo Then
   GoTo MenuToolbarSetup_Exit
End If

Set ctr = cdb.Containers("Reports")
'Reports cannot be opened in hidden mode
For Each doc In ctr.Documents
 docName = doc.Name
 DoCmd.OpenReport docName, acViewDesign
 Reports(docName).MenuBar = "MyMainMenu" 
Reports(docName).Toolbar = "MyReportToolBar" 
DoCmd.Close acReport, docName, acSaveYes
Next

msg = "Custom Menus/Toobar Setup Completed successfully. "

MsgBox msg

Set ctr = Nothing
Set cdb = Nothing

MenuToolbarSetup_Exit:
Exit Function

MenuToolbarSetup_Err:
MsgBox Err.Description
Resume MenuToolbarSetup_Exit
End Function

Run the Code from the Debug Window

Since this is purely a design-time task, you can execute the code directly by placing the cursor anywhere within the procedure and pressing the F5 key, or by calling it from the On_Click() event of a Command Button on a form.

However, remember that this form (the one containing the command button) will also be opened in Design View when the code runs to update property values. If you want to prevent that from happening, include an If...Then condition in your code to bypass this form.

Share:

Seriality Control Finding Missing Numbers

Introduction.

In accounting and auditing, it is a standard practice to maintain strict control over the use of important documents such as checkbooks, receipt vouchers, payment vouchers, and local purchase orders. The usage of these documents is closely monitored to prevent misuse that could negatively impact the company’s operations or reputation.

These documents are usually printed in books containing 20, 50, or 100 sheets, each bearing sequential serial numbers. All transactions involving these documents are recorded along with their corresponding serial numbers.

Periodic audits are conducted to verify that the serial numbers recorded in the system match the continuity of used documents in hand. Any missing numbers, whether due to cancellation, loss, or other reasons, are investigated and properly documented.

To illustrate this process, we’ll create a sample program that identifies and lists missing serial numbers from recorded transactions. For this, we’ll need the following tables containing the necessary information:

Preparing for Trial Run

  1. Parameter Table: with Start-Number and End-Number values. Uses this number range to find the missing numbers from within the Transaction Table.

  2. Transaction Table: where the actual transaction details of the Documents are recorded, and our program should check and bring out the missing cases.

  3. Missing_List Table: where the missing list of Numbers will be created.

  4. Copy the following VBA Code and paste it into a new Global Module in your Database.

The VBA Code

Option Compare Database
Option Explicit

Type Rec
    lngNum As Long
    flag As Boolean
End Type

Public Function MissingNumbers()
'------------------------------------------------------
'Author : a.p.r. pillai
'Date   : 05/10/2008
'URL    : www.msaccesstips.com
'All Rights Reserved by www.msaccesstips.com
'------------------------------------------------------
Dim db As Database, rst1 As Recordset, rst2 As Recordset
Dim lngStart As Long, lngEnd As Long
Dim ChequeNo As Long, j As Long, ChqSeries() As Rec
Dim NumberOfChqs As Long, k As Integer, bank As String
Dim strSeries As String

On Error GoTo MissingNumbers_Err

'initialize the Report Table
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE Missing_List.* FROM Missing_List;"
DoCmd.SetWarnings True

Set db = CurrentDb
'Load Cheque Book Start and End Numbers
'from parameter table
Set rst1 = db.OpenRecordset("Parameter", dbOpenDynaset)
Do While Not rst1.EOF
    bank = rst1!bank
    lngStart = rst1!StartNumber
    lngEnd = rst1!EndNumber
' calculate number of cheques
    NumberOfChqs = lngEnd - lngStart + 1
    strSeries = "Range: " & lngStart & " To " & lngEnd

'redimention array to hold all the cheque Numbers
'between Start and End numbers
    ReDim ChqSeries(1 To NumberOfChqs) As Rec

'Generate All cheque Numbers between
'Start and End Numbers and load into Array
    k = 0
    For j = lngStart To lngEnd
        k = k + 1
        ChqSeries(k).lngNum = j
        ChqSeries(k).flag = False
    Next

'Open Cheque Payment Transaction Records
    Set rst2 = db.OpenRecordset("Transactions", dbOpenDynaset)

'Flag all matching cheque Numbers in Array
    k = 0
    rst2.MoveFirst
    Do While Not rst2.EOF
        ChequeNo = rst2![chqNo]
        If ChequeNo >= lngStart And ChequeNo <= lngEnd And rst2![bnkCode] = bank Then
            j = (ChequeNo - lngStart) + 1
            ChqSeries(j).flag = True
        End If
        rst2.MoveNext
    Loop
    rst2.Close

'create records for unmatched items in Report Table
    Set rst2 = db.OpenRecordset("Missing_List", dbOpenDynaset)
    k = 0
    For j = lngStart To lngEnd
        k = k + 1
        If ChqSeries(k).flag = False Then
            With rst2
                .AddNew
                !bnk = bank
                ![MISSING_NUMBER] = ChqSeries(k).lngNum
                ![REMARKS] = "** missing **"
                ![CHECKED_SERIES] = strSeries
                .Update
            End With
        End If
    Next
    rst2.Close

rst1.MoveNext
Loop
rst1.Close

Set rst1 = Nothing
Set rst2 = Nothing
Set db = Nothing

MissingNumbers_Exit:
Exit Function

MissingNumbers_Err:
MsgBox Err & " : " & Err.Description, , "MissingNumbers()"
Resume MissingNumbers_Exit
End Function

To try out the above program, create the first two tables with the same Field Names and data type, as suggested by the sample data given above, and enter the same data or similar records of your choice, excluding some serial numbers from the range of values in the Parameter Table.

Create the third Table (Missing_List) with the same Field Names and data type of the sample records shown above, but without adding any records to it.

VBA Code Analysis

In the global area of the Module, we have created a User-Defined Data Type Rec with two elements, lngNum to hold the Serial Number and Flag to mark when a match is found in the Transaction Table, with Long Integer and Boolean data types, respectively. After creating the new data type in the Global area, we have declared an empty array variable ChqSeries() as Rec with the newly created data type within the Program.

The program opens the Parameter Table, starts with the first record, calculates the number of records that come within the given range, and re-dimensions the array to hold all the numbers between the lngStart and lngEnd parameter values.

In the next step, the program generates all the serial numbers between lngStart and lngEnd and fills the chqSeries().lngNum array. The Flag element value is set as False.

Next, open the Transaction Table and scan through it for matching Bank Code, and for Cheque Numbers between lngStart and lngEnd, and when a match is found, the 'chqSeries().Flag' is marked as True for that entry within the array, and continues this process till the end of the file is reached.

In this process, if the 'chqSeries().Flag' is not marked as True, then the Serial Number corresponding to that entry is found missing in the Transaction Table. In the next step, we scan through the Array and check for entries with 'chqSeries().Flag' = False, and write them out in the Missing_List.

This process continues for all the records in the Parameter Table.

Note: This method is not the most efficient in terms of processing speed when handling a large volume of transactions. In such cases, it is advisable to filter the data in the Transaction table using a parameterized query and use the filtered dataset in place of the full Transaction table.

This needs extra steps in the program to create a Dynamic Query with SQL Statement just before the following statement: Set rst2 = db.OpenRecordset("Transactions", dbOpenDynaset), replacing Transactions with the Query name.

Share:

Wave Shaped Reminder Ticker

Introduction.

We have already seen how to create and install a Reminder Ticker that runs in a straight line on the Main Screen. We could do this with a few lines of VBA code and a Label control on the Main Screen. We will try something different this time. This ticker runs in a Zigzag form. An image of a sample run of this method is given below:

To create this Ticker, we need a series of labels arranged in a wavelike form, and each one must be named in a way that makes it easy to address them in code. A sample design is given below:


Automating the Labels Creation

There are about 42 identical labels to be created. Even if we create them manually once, arranging them in the required zigzag layout is a challenging task. However, this can be done efficiently with a small program. The program generates a new form, creates all 42 labels, arranges them in a zigzag pattern, assigns them sequential names such as lbl1 through lbl42, and adjusts their other properties as described above.

  1. Copy the following Code into a Global Module of your Database and save it.
    Public Function ZIGZAG()
    '-----------------------------------------------------------
    'Author  :  a.p.r. pillai
    'Date    :  01/10/2008
    'URL     :  www.msaccesstips.com
    'All Rights Reserved by www.msaccesstips.com
    '-----------------------------------------------------------
    Dim frm As Form, ctrl As Label, t As Long, lngleft As Long
    Dim lngwidth As Long, lngheight As Long, lngtop As Long
    Dim j As Integer, k As Integer, h As Long, G As Long
    
    h = 30: G = 0: t = 0
    lngwidth = 0.1146 * 1440
    lngheight = 0.2083 * 1440
    lngtop = 1 * 1440
    lngleft = 0.16667 * 1440
    Set frm = CreateForm
    For j = 1 To 42
    Set ctrl = CreateControl(frm.Name, acLabel, acDetail, , , lngleft, lngtop, lngwidth, lngheight)
    lngleft = lngleft + lngwidth
    With ctrl
        .Name = "lbl" & j
        .FontName = "Tahoma"
        .FontSize = 8
        .Caption = ""
        .BackStyle = 0
        .ForeColor = 255
     End With
     Next
    G = 0
    For j = 1 To 3
         For k = 1 To 7
            G = G + 1
            Set ctrl = frm.Controls("lbl" & G)
            With ctrl
                .Top = .Top - (h * k)
            End With
            DoEvents
         Next
        t = frm.Controls("lbl" & G).Top
         For k = 1 To 7
             G = G + 1
            Set ctrl = frm.Controls("lbl" & G)
            With ctrl
                .Top = t + (h * 1)
            End With
            t = frm.Controls("lbl" & G).Top
            DoEvents
        Next
    Next
    End Function
    
  2. You can run the above Code directly by placing the cursor in the middle of the Code and pressing the F5 Key, or running from a Command Button's On Click Event Procedure or a Macro.

    Each time the code is executed, it creates a new form with the labels arranged in a zigzag pattern. Once you’ve created it, you can export this form to other projects where you want to install the ZigZag Ticker. Alternatively, you can place the code in a common library database and run it from your new project after attaching the library file to your project.

    Placement of the Ticker Labels.

  3. After creating the Labels, click somewhere outside the Labels and drag over them so that all the Labels are selected without disturbing the Labels' arrangement.

  4. Select Copy from the Edit Menu.

  5. Open the Main Switch Board (Control Form) in Design View and paste them.

  6. When all the labels are still in the selected state, drag and place the Labels into a position where you want the Ticker to appear on the Form.

    We have two more Sub-Routines, which are run from the Form_Load() and Form_Timer() Event Procedures. In the Form_Load() Event Procedure, we can create a Text Message in a String either with a constant value or with Field Values from a Table/Query that provides useful information to display to the User as a reminder. Refer to the earlier example Reminder Ticker Form, which uses data from within the Application as a reminder.

    Formatting Ticker Text

    The Form_Timer() Event Procedure will control the Display of Label values, shifting one character at a time in succeeding labels, giving it a sense of motion.

  7. Copy and paste the following Sub-Routines into the Form Module where you have pasted the above labels.

    Option Compare Database
    Option Explicit
    Dim txt As Variant
    
    Private Sub Form_Load()
        txt = Space(42) & UCase("Excellence is not a matter of chance. It is a matter of Change. It is not a thing to be waited for. It is a thing to be achieved.")
        Me.Timerinterval=250
    End Sub
    
  8. See that the Dim txt As Variant is placed in the Global Area of the Module, which is referenced from the Form_Load() and Form_Timer() Event Procedures.

    Private Sub Form_Timer()
    Dim x As String, k As String, j As Integer, ctrl As Control
    
      x = Left(txt, 1)
      txt = Right(txt, Len(txt) - 1)
      txt = txt & x
      k = Left(txt, 42)
    For j = 1 To Len(k)
        Set ctrl = Me.Controls("lbl" & J)
        Ctrl.Caption = Mid(k, j, 1)
    Next
    End Sub 
    

    Disable Ticker on inactive Form

  9. The following lines of code are useful if you plan to disable the ticker when the Main Form is inactive and run it when the Main Form is active again, so that other processes are not interrupted by the Ticker.

Private Sub Form_Deactivate()
   Me.TimerInterval = 0
End Sub

Private Sub Form_Activate()
   Me.TimerInterval = 250
End Sub

Download


Download Demo Database Access2007.zip



Download Demo Database Access2002-03.zip

Share:

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