web 2.0


Alternate row color in an Access Report

This tip shows you how to use VBA to alternate the row color of an Access report. Many times, when creating a report, we find it hard to read the details. By alternating the row color we can make life much simpler...

 

 Private rowCount As Long
 
  Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
 
    rowCount = rowCount + 1
 
    If rowCount / 2 = CLng(rowCount / 2) Then
      Me.Detail.BackColor = 16777215
    Else
      Me.Detail.BackColor = 15263976
    End If
  End Sub

Tags: , ,

Microsoft Access | Microsoft Access - VBA

Building custom toolbars in MS Access - Part 2

This article is the second part of the series on building custom toolbar in MS Access. If you have not read part one, please click here to read or print the article.

In this article, I will show you how to create a popup menu using the right-click of your mouse. The picutre below shows one of Access's popup menus (and this is what we will be building):

 
Picture 1 – MS Access popup menu

When you click with the right button on a form Access will automatically show the popup menu available for that object as shown above. Thus we will have to cancel this event and force Access to show our own popup instead..

To complete this exercise, you will need to:

  • Install the references to Microsoft Office xx Object Library (xx is the version of your MS Office – See first article on how to do this)
  • Insert a module
  • Inser a form
  • The first part of this exercise consists of writing the code that will build the popup. This code must be inserted in the module you created in the previous paragraph. The code has been broken down into two parts:

    • A sub procedure to build the popup menu
    • A sub producedure to remove the popupmenu

Adding the code to a module:

Public Const POPUPMENU As String = "POPUP TOOLBAR"
 
 Sub popMenu()
  Dim cmdBar As CommandBar
  Dim mnu As CommandBarButton
 
  delPopMenu 'Delete the popmenu if it exists
  Set cmdBar = CommandBars.Add _
   (Name:=POPUPMENU, Position:=msoBarPopup, Temporary:=True)
 
  Set mnu = cmdBar.Controls.Add(Type:=msoControlButton)
    With mnu
     .Caption = "My Popup 1"
     .OnAction = "msg1"
     .FaceId = 308
   
End With
 
  Set
mnu = cmdBar.Controls.Add(Type:=msoControlButton)
    With mnu
     .Caption = "My Popup 2"
     .OnAction = "msg2"
     .FaceId = 209
  
End With
 
  Set
mnu = cmdBar.Controls.Add(Type:=msoControlButton)
    With mnu
     .Caption = "My Popup 3"
     .OnAction = "msg3"
     .FaceId = 1714
    
End With
 
  Set
mnu = cmdBar.Controls.Add(Type:=msoControlButton)
    With mnu
     .Caption = "&About..."
     .OnAction = "about"
     .FaceId = 326
     .BeginGroup = True
   
End With
 
  Set
mnu = cmdBar.Controls.Add(Type:=msoControlButton)
    With mnu
     .Caption = "&Help"
     .OnAction = "Help"
     .FaceId = 984
     .BeginGroup = True
   
End With
 
 End Sub
 
 
 Sub
delPopMenu()
   On Error Resume Next
   CommandBars(POPUPMENU).Delete
 End Sub

With this part our of the way, we are read to write the code to go on the form that will call the menu everytime the user right-clicks on the form.

Tags: , , , ,

Microsoft Access | Microsoft Access - VBA