As I understand the situation, you want to set ControlTipText
properties dynamically each time your form loads. Since you indicated in a comment that this application is intended for a tablet device, you might prefer to limit the processor load when opening your form. You could do that by saving the ControlTipText
properties with the form's design.
Try the following procedure with your form's name like this:
SetControlTipText "YourFormName"
Here is the procedure. I didn't find any problems in limited testing. It sets ControlTipText
for check boxes, combos, list boxes, and text boxes. Change the first Case
line to target a different set of controls.
Public Sub SetControlTipText(ByVal pFormName As String)
Dim ctl As Control
Dim db As DAO.Database
Dim frm As Form
Dim rs As DAO.Recordset
DoCmd.OpenForm pFormName, acDesign
Set frm = Forms(pFormName)
If Len(frm.RecordSource) > 0 Then
Set db = CurrentDb
Set rs = db.OpenRecordset(frm.RecordSource)
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acCheckBox, acComboBox, acListBox, acTextBox
If Len(ctl.ControlSource) > 0 _
And Not ctl.ControlSource Like "=*" Then
ctl.ControlTipText = _
GetDescription(rs.Fields(ctl.ControlSource))
End If
Case Else
' pass '
End Select
Next ctl
rs.Close
End If
Set ctl = Nothing
Set rs = Nothing
Set db = Nothing
Set frm = Nothing
DoCmd.Close acForm, pFormName, acSaveYes
End Sub
SetControlTipText
calls this function:
Public Function GetDescription(ByRef pObject As Object) As String
Dim strReturn As String
On Error GoTo ErrorHandler
strReturn = pObject.Properties("Description")
ExitHere:
GetDescription = strReturn
On Error GoTo 0
Exit Function
ErrorHandler:
strReturn = vbNullString ' make it explicit '
GoTo ExitHere
End Function
That SetControlTipText
procedure ignores unbound forms. If the control source for a bound field doesn't have a Description
property assigned, its ControlTipText
will be set to an empty string.
This approach will require you to run the procedure once for the form, rather than running some other procedure each time the form loads. If you later change Description
properties for any of the form's record source fields, you can re-run SetControlTipText
to update ControlTipText
's.
Or you could run the procedure for all your application's forms as part of your preparations to release a new version of the application.
Dim frm As Object
For Each frm in CurrentProject.AllForms
SetControlTipText frm.Name
Next frm