ElseIf working but not If

timothyl

Registered User.
Local time
Today, 13:58
Joined
Jun 4, 2009
Messages
92
Can some one please tell me what I am doing incorrectly, the ElseIf is working But not the If. Thanks Tim


Sub ClearFormText(frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls

If ctl.ControlType = acTextBox And ctl.Tag = "*" Then
If ctl.Name = "txtNewField" Or ctl.Name = "txtNewField1" Then
txtNewField = Hcpc
txtNewFieldi = Description

ElseIf ctl.ControlType = acTextBox Then
ctl.Value = ""

End If
End If
Next ctl

'Call the code from any event with the following syntax:

'ClearFormText Me

'The 'Me' keyword will pass in a reference to the current
'form and the code will clear all text boxes.
End Sub
 
To say that code is "not working" is like telling your doctor you "feel sick".
To get more diagnosis provide more symptoms. ;)
Cheers,
 
It clears the controls on my form, which is what I want it to do, the one's "taged" *, I need to have a default value, but everything clears. Also I am trying to understand the tag property. New at this as you can tell. Tim
 
You should not use the asterisk as a tag

ctl.Tag = "*"

if that is what you actually have in the tag, because the * is a special character which has special meaning.

If you are just wanting all text boxes that have a tag then it would be:

If ctl.ControlType = acTextBox And ctl.Tag <> "" Then
 
Can some one please tell me what I am doing incorrectly, the ElseIf is working But not the If. Thanks Tim

Code:
Sub ClearFormText(frm As Form)
    Dim ctl As Control
    For Each ctl In frm.Controls
 
        If ctl.ControlType = acTextBox And ctl.Tag = "*" Then
 
            If ctl.Name = "txtNewField" Or ctl.Name = "txtNewField1" Then
 (1)               txtNewField = Hcpc
 (1)               txtNewFieldi = Description
 (2)       ElseIf ctl.ControlType = acTextBox Then
                ctl.Value = ""
            End If
 
        End If
    Next ctl
 
    'Call the code from any event with the following syntax:
 
    'ClearFormText Me
 
    'The 'Me' keyword will pass in a reference to the current
    'form and the code will clear all text boxes.
 
End Sub





I have reformatted your code as I think it is meant to be displayed, and have some questions.
  1. The variables Hcpc and Description do not appear to be defined in the Subroutine. What values will they contain? The code might indeed be working and substituting Null Values.
  2. The Elseif could be a simple Else, since the code will not even be executed unless the outer If statement has ctl.ControlType = acTextBox
 
Thanks evey one for your reponces. I have a form I am working on that builds an sql statement, the first part is to change the field names in one table so they match those of a differnt table. Then select those fields whose name has been changed and Union them to the table with the field names: Hcpc, Description. PartNumber, BestPrice. These names are constants which is why I want my clearing code above to not clear these four names. I could clear my controls in a nother way but I was trying to increase my skills by atempting to write it in they above format.
Tim
 

Users who are viewing this thread

Back
Top Bottom