Page Number Function: 'PageNo()'
The following are standard functions that you can use in the Report Header or Footer sections when designing reports.
To use them, simply copy and paste the VB code into a global module in your database and save it.
Write the Functions in the form of a Formula in Text Boxes as shown in the Syntax.
Function to display formatted Page Numbers.
Function: 'PageNo()'.
Syntax: =PageNo([page],[pages])
Result: Page: 1 / XXX, where XXX stands for the maximum number of pages of the Report.
Note: [page],[pages] are MS-Access built-in Report Variables and must be used as shown.
Code:
Public Function PageNo(ByVal pg As Variant, _ByVal pgs As Variant) As String
'----------------------------------------------------------
'Output : Page: 1/25
' : Call from a Report Text Box control
'Author : a.p.r. pillai
'Date : 01/09/2007
'Remarks : The Formatted Text takes up 15 character space
'----------------------------------------------------------
Dim strPg As String, k As Integer
On Error GoTo PageNo_Err
pg = Nz(pg, 0): pgs = Nz(pgs, 0)
strPg = Format(pg) & "/" & Format(pgs)
k = Len(strPg)
If k < 15 Then
strPg = String(15 - k, "*") & strPg
End If
strPg = "Page: " & strPg
For k = 1 To Len(strPg)
If Mid(strPg, k, 1) = "*" Then
Mid(strPg, k, 1) = Chr(32)
Next
PageNo = strPg
PageNo_Exit:
Exit Function
PageNo_Err:
Msgbox Err.Description,, "PageNo()"
PageNo = "Page : "
Resume PageNo_Exit
End Function
Report Period Function: Period()
Function to print Period with formatted Start-Date and End-Date on Report Header or Footer.
Function : Period()
Syntax : =Period([StartDate], [EndDate])
Result : Period: 15/09/2007 To 30/09/2007
Note: Format String in the Code may be modified for country-specific date format.
Code:
Public Function Period(ByVal prdFrm As Date, _ByVal PrdTo As Date) As String
'-----------------------------------------------------------------
'Output : Period: dd/mm/yyyy To dd/mm/yyyy
' : Call from Report control to insert date
'Author : a.p.r. pillai
'Date : 01/09/2007
'Remarks : Modify Format String for Country specific date format.
'-----------------------------------------------------------------
On Error GoTo Period_Err
Period = "Period: " & Format(prdFrm, "dd/mm/yyyy") & " To " & Format(PrdTo, "dd/mm/yyyy")
Period_Exit:
Exit Function
Period_Err:
MsgBox Err.Description,, "Period()"Resume Period_Exit
End Function
Report Date Function: Dated()
Function to print formatted System Date in the Header or Footer of the Report.
Function : Dated()
Syntax: Dated()
Result : Dated: 15/09/2007
Code:
Public Function Dated() As String
'----------------------------------------------------------------
'Output : Dated: 20/08/2007
' : Call from Report Text Box control
'Author : a.p.r. pillai Date : 01/09/2007
'Remarks : Change Format String for Country specific Date Format
'----------------------------------------------------------------
On Error GoTo Dated_Err
Dated = "Date: " & Format(Date, "dd/mm/yyyy")
Dated_Exit:
Exit Function
Dated_Err:
MsgBox Err.Description,, "Dated()"
Resume Dated_Exit
End Function
You can design the full Page Footer of a Report with the Date and Page Number at one go with a single Function. Read my earlier Article: Reports . . . Page Border. Use the Function DrawPageFooter() Code & procedure explained there.
You can add frequently used Expressions or Routines as Public Functions into a Global Module of your Database and run from where you need them (Forms, Reports, Query Expressions, etc.) rather than repeating the code everywhere.
Library Database with User-Defined Functions.
You can further enhance the use of commonly used functions by organizing them into a separate library database and linking it to your new projects. This way, any shared functions or even reusable forms—such as a custom Form Wizard—can be centrally maintained in the library database.
When you call a library function that references a form, Microsoft Access will first look for the form in the library database. If it’s not found there, it will then check in the current project and open it from there.
Follow the procedure outlined in the earlier post, Command Button Animation, to link the essential library files to your project. If your library database does not appear in the list of installed references, use the Browse button to locate it manually. Once found, attach it to the references list and select it to make its functions and objects available in your project.
You can save this Library file in a compiled state by converting it into an MDE File. Select Tools -> Database Utilities -> Make MDE File to convert and save the current database into MDE Format. You cannot edit the code in the MDE database. Preserve the MDB file for future changes and compilation, if it becomes necessary.
When installing your project, ensure that the common library database is included and properly linked to your project in the new location. The same rule applies to any other built-in library files used by your application. Keep in mind that the version of certain built-in libraries on the target machine may differ from those used during development. This can occur if other Visual Basic–based applications were previously installed on the system. In such cases, these libraries may appear as MISSING in the References list. You must then reattach the correct or available version of the library from the installed location to resolve the issue.
If your Project is shared by different Versions of MS Office Applications, then it is a good idea to attach an older version of the built-in library file (if available) to the project.
Refer to the earlier Post on Sharing an Older Version Database under the topic: Ms-Access Security.
Earlier Post Link References: