|
Home - Microsoft
Access Development - Microsoft Access Tips
Sample Microsoft Access VBA
Help Screen Function
Download Sample Database
Microsoft Access help usually requires a separate help file be
created using one of a number of tools available to create the file. Each text
box or combo has to have information in the properties detailing the help file.
This help function uses a different approach. It stores the help information
in a table within the application. Users can update the help with tips and
information themselves. If you do not want them to update the help, change
the edit button visible property to not display.

A sample database is available to download. I will outline the
key parts of the application below.

Help Table
The table is simple in that there are only three fields.
| Table Name: |
tblHelp |
| Fields: |
HelpRef - Text, 255, Indexed Yes(no duplicates). Set as Primary Key
HelpTitle - Text 50
HelpText - Memo |
The field HelpRef is a combination of the form and field name
separated by a dash. For example if a form frmCustomer had a field called txtSurname,
the HelpRef would be frmCustomer-txtSurname.

Forms
In order for the forms to display help, you need to intercept
the F1 key before it fires up Access Help. To do this, you change the Form.KeyPreview
to "Yes" and use the Form.KeyDown function to change KeyPress to
0. You then call subGetHelp.
Paste the following code into your form KeyDown. Remember to
change the form KeyPreview to "Yes".
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Error_Form_KeyDown
If KeyCode = vbKeyF1 Then
KeyCode = 0
Call subGetHelp()
End If
Exit_Form_KeyDown:
On Error GoTo 0
Exit Sub
Error_Form_KeyDown:
MsgBox "An unexpected situation arose in your program." & funCrLf & _
"Please write down the following details:" & funCrLf & funCrLf & _
"Module Name: Form_frmCustomers" & funCrLf & _
"Type: VBA Document" & funCrLf & _
"Calling Procedure: Form_KeyDown" & funCrLf & _
"Error Number: " & Err.Number & funCrLf & _
"Error Descritption: " & Err.Description
Resume Exit_Form_KeyDown
Resume
End Sub
|

Module modHelp
Create a new module called modHelp and paste the code below into
the module.
The code creates a string called strHelp which is a combination
of form and field name. This is what is stored in the table as HelpRef.
The code uses a few functions such as funIC which is a short function to
insert inverted commas. Just lazy I guess playing. Avoids around with lots
of inverted commas in a cancatenated string. It also uses funRecordCount which
returns the number of records in the recordset. SubOpenForm is just a generic
version of the DoCmd.OpenForm command which has some error trapping. All are
in a module called modGeneric.
There are three stages. First it searches within tblHelp.HelpRef
for a combination of form-field. If it is not found then it then searches for
just form. If neither is found, it displays a message that no help exists.
NOTE: You cannot step throught the code as it uses
Screen.ActiveForm.Form.Name & "-" & Screen.ActiveControl.Name.
If
the form is not the active screen it will fail. If you do want to step through
the code, pause after the line above has processed.
Option Compare Database
Option Explicit
Public Sub subGetHelp()
Dim dbs As Database
Dim rst As Recordset
Dim strHelp As String
Dim strSQL As String
Dim strCriteria As String
On Error GoTo Error_subGetHelp
strHelp = Screen.ActiveForm.Form.Name & "-" & Screen.ActiveControl.Name
strSQL = "SELECT tblHelp.HelpRef FROM tblHelp" & _
" WHERE tblHelp.HelpRef = " & funIC & strHelp & funIC & ";"
If funRecordCount(strSQL) > 0 Then GoTo LaunchHelp
strHelp = Screen.ActiveForm.Form.Name
strSQL = "SELECT tblHelp.HelpRef FROM tblHelp" & _
" WHERE tblHelp.HelpRef = " & funIC & strHelp & funIC & ";"
If funRecordCount(strSQL) > 0 Then GoTo LaunchHelp
Call MsgBox("There is no help available for this field or for the form.",
_
vbInformation, "No Help Available")
GoTo Exit_subGetHelp
LaunchHelp:
strCriteria = "[HelpRef] = " & funIC & strHelp & funIC
subOpenForms "frmHelp", , , strCriteria
Exit_subGetHelp:
On Error GoTo 0
Exit Sub
Error_subGetHelp:
MsgBox "An unexpected situation arose in your program." & funCrLf & _
"Please write down the following details:" & funCrLf & funCrLf & _
"Module Name: Form_frmImageMgmt" & funCrLf & _
"Type: VBA Document" & funCrLf & _
"Calling Procedure: subGetHelp" & funCrLf & _
"Error Number: " & Err.Number & funCrLf & _
"Error Descritption: " & Err.Description
Resume Exit_subGetHelp
Resume
End Sub
|

Help Form
The Help form is very straight forward. It displays the help,
and has two buttons. One button closes the form. The other button allows you
to edit the form. When you select "Edit", the button caption changes to "Lock".
Select "Lock" and it changes to "Edit".
When you edit, there is a third button visible. That button allows
you to insert bullets in Microsoft Access. Normally if you want bullets you
have to use something like a dash. How this works is described in the Project
Perfect Blog. We will not cover it again in this article.
The form has the three fields from tblHelp - the reference
number (HelpRef) which is hidden, the name of the help item (HelpTitle) and
the text of the help (HelpText). The text boxes are called txtHelpRef, txtHelpTitle
and txtHelpText.
SubFormDisplay is a generic sub to set the size of the form when
it opens. It is in the module modGeneric in the sample.
The two buttons are called btnEdit and btnClose. Create the form
and past in the code below. If you do not want users editing the form, change
the btnEdit.Visible to "No".
Option Compare Database
Option Explicit
Private Sub Form_Activate()
On Error GoTo Error_Form_Activate
subFormDisplay Me.Name, 7.5, 13 ' Height , Width
Exit_Form_Activate:
On Error GoTo 0
Exit Sub
Error_Form_Activate:
MsgBox "An unexpected situation arose in your program." & funCrLf & _
"Please write down the following details:" & funCrLf & funCrLf & _
"Module Name: Form_frmHelp" & funCrLf & _
"Type: VBA Document" & funCrLf & _
"Calling Procedure: Form_Activate" & funCrLf & _
"Error Number: " & Err.Number & funCrLf & _
"Error Descritption: " & Err.Description
Resume Exit_Form_Activate
Resume
End Sub
Private Sub btnClose_Click() On Error GoTo Error_btnClose_Click
DoCmd.Close
Exit_btnClose_Click:
On Error GoTo 0
Exit Sub
Error_btnClose_Click:
MsgBox "An unexpected situation arose in your program." & funCrLf & _
"Please write down the following details:" & funCrLf & funCrLf & _
"Module Name: Form_frmHelp" & funCrLf & _
"Type: VBA Document" & funCrLf & _
"Calling Procedure: btnClose_Click" & funCrLf & _
"Error Number: " & Err.Number & funCrLf & _
"Error Descritption: " & Err.Description
Resume Exit_btnClose_Click
Resume
End Sub
Private Sub btnEdit_Click()
On Error GoTo Error_btnEdit_Click
If Me.btnEdit.Caption = "Edit" Then
Me.txtHelpTitle.Enabled = True
Me.txtHelpTitle.Locked = False
Me.txtHelpTitle.BorderStyle = 1
Me.txtHelpText.Enabled = True
Me.txtHelpText.Locked = False
Me.txtHelpText.BorderStyle = 1
Me.btnEdit.Caption = "Lock"
Else ' Currently shown as "Lock"
Me.txtHelpTitle.Enabled = False
Me.txtHelpTitle.Locked = True
Me.txtHelpTitle.BorderStyle = 0
Me.txtHelpText.Enabled = False
Me.txtHelpText.Locked = True
Me.txtHelpText.BorderStyle = 0
Me.btnEdit.Caption = "Edit"
End If
Exit_btnEdit_Click:
On Error GoTo 0
Exit Sub
Error_btnEdit_Click:
MsgBox "An unexpected situation arose in your program." & funCrLf & _
"Please write down the following details:" & funCrLf & funCrLf & _
"Module Name: Form_frmHelp" & funCrLf & _
"Type: VBA Document" & funCrLf & _
"Calling Procedure: btnEdit_Click" & funCrLf & _
"Error Number: " & Err.Number & funCrLf & _
"Error Descritption: " & Err.Description
Resume Exit_btnEdit_Click
Resume
End Sub
|

Help Maintenance Form
Within the sample is a simple program to update the help information.
It allows you to either:
- Search for, and edit existing help records
- Create new help records. This includes looking up the form and field name
from drop down lists, and combining them to create the primary key (e.g.
if the form were frmForm1, and it had a textbox txtInfo, the name would be
frmForm1-txtInfo.
We have not provided a description of the code used in the help maintenance
form. You can just use it without having to modify any code. For those interested,
it creates a value list of forms, and based on the selected form creates a
value list of appropriate controls.

Summary and Download
The download also illustrates another tweak. On the Customer
form is a button to show help for the window. It uses the same module. Since
there is no help item for the combination of screen name and control name,
it just displays screen help.
This is a simple way to plug in help without putting together
a Microsoft Access help file. There is no need to buy the tools to create a
help file, and it allows users to update the information.
I would also like to
recognise the following people who provided input to the application.
Alex Dybenko and Stuart McCall. Thanks guys.
Please provide feedback on the application to Project Perfect.
To download the sample database,
click here.
Return to the top

|