Does a Form Field Exist from VBA

Brian1960

Brian1960
Local time
Today, 23:35
Joined
Aug 13, 2004
Messages
141
I have a module called by several forms which builds word documents from templates. On nearly all the Forms there is an AccountNumber field but for one this field does not exist.
Does anyone know a way of saying something like this:
Code:
if Field.Exists(Forms!frmName!fieldname) = True then .typetext CStr(Forms!frmName!fieldname)

Thanks
PS I realise I could just add it hidden but that isn't very elegant!
 
Public Function FieldExists(strFieldName As String, strFormName As String)

Here's a function that determines if a specified control name exists on a specified form. It will return TRUE if a specified TextBox name is found on an (currently open) form. If the form control you seek is not limited to being a TextBox then remove the "If Typeof ctrl ... End If" and uncomment the "Select Case .ControlType ... End Select" lines.

CAVEAT: If you call this function using a Form name that is not currently open, it will fail.



Code:
Public Function FieldExists(strFieldName As String, strFormName As String) As Boolean
    On Error GoTo Err_FieldExists
    
    Dim ctrl As Control
    
    FieldExists = False

    For Each ctrl In Forms(strFormName)
        With ctrl
            If TypeOf ctrl Is TextBox Then
                If StrComp(.Name, strFieldName, vbTextCompare) = 0 Then FieldExists = True
            End If
'            Select Case .ControlType
'                Case acCheckBox, acTextBox, acListBox, acComboBox
'                    If StrComp(.Name, strFieldName, vbTextCompare) = 0 Then FieldExists = True
'            End Select
        End With
    Next

Exit_FieldExists:
    Exit Function

Err_FieldExists:
    MsgBox Err.Description, vbExclamation + vbOKOnly, "FieldExists"
    Resume Exit_FieldExists

End Function
 
Code:
        With ctrl
            If TypeOf ctrl Is TextBox Then
                If StrComp(.Name, strFieldName, vbTextCompare) = 0 Then FieldExists = True
            End If
'            Select Case .ControlType
'                Case acCheckBox, acTextBox, acListBox, acComboBox
'                    If StrComp(.Name, strFieldName, vbTextCompare) = 0 Then FieldExists = True
'            End Select
        End With

Umm.. why are you checking textboxes twice?
You could get rid of the whole If... endif and still check all pertinent boxes on the form.

If you want to check only specific types, again. Just do as above without the If.. endif and just add / remove from the list following the Case.
 
The Select Case code is commented out.

I indicated in my posting that if the need is to check textboxes exclusively, then the If End If section of code is sufficient. If the need is to check for a broader range of controls then one should discard the If End If stuff and uncomment the Select Case lines.

I used If Typeof in the first example because it is compact. I used Select Case in the second example because it easily incorporates a range of ControlTypes with one statement.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom