PDA

View Full Version : If statement help



macca34
09-12-2014, 02:49 AM
I have a drop down list. When option 1 or 2 is selected I want three fields to be not null. I have code that already has the three fields validated. Grateful to how I can add an IF statement to incorporate the drop down list (combobox).


If ActiveDocument.CostCentre.Text = "" Then
If MsgBox("Please add Cost Centre", vbCritical + vbDefaultButton1, "STOP") = vbCancel Then
ActiveDocument.CostCentre.Text.Result = sInFld
End If
Exit Sub
End If

gmayor
09-12-2014, 09:08 PM
I am sorry but your question doesn't make any sense.

What sort of a dropdown list? Content control? Form Field? Userform? Active X?

What does "I want three fields to be not null" mean? Do you mean you want to check whether the fields have content? What sort of fields are they? From where do they get their content? What is the relevance of the combobox values to that?

CostCentre is not an object that relates to the ActiveDocument So what is that and what does it relate to?

You will have to explain rather better if you want help with this. We cannot see over your shoulder.

macca34
09-17-2014, 02:13 AM
I wil try to explain what I mean.

I have a combobox (form field with three options : Add, Amend, Delete). If Add or amend are selected. I want three fileds (textbox) Cost, Prog and Entity not to be null (validation check).

gmayor
09-17-2014, 02:36 AM
But what if they are null?

The following will test whether any of those text fields is empty and warn the user. You can run it on exit from the dropdown field.


Sub Test()
Dim i As Long
Dim oFF As FormField
Select Case ActiveDocument.FormFields("Dropdown1").Result
Case "Add", "Amend"
For i = 1 To ActiveDocument.FormFields.Count
If ActiveDocument.FormFields(i).Type = wdFieldFormTextInput Then
Set oFF = ActiveDocument.FormFields(i)
Select Case oFF.name
Case "Cost", "Prog", "Entity"
If oFF.Result = vbNullString Then
MsgBox oFF.name & " is empty"
Exit For
End If
Case Else
End Select
End If
Next i
Case Else
End Select
End Sub

macropod
09-17-2014, 08:28 PM
Given that a person could change the Cost, Prog and Entity fields after updating to dropdown (indeed, this may even be the normal processing sequence), I'd be inclined to implement the validation when the document is closed:

Sub AutoClose()
Dim FmFld As FormField, i As Long, ArrFmFld
ArrFmFld = Array("Cost", "Prog", "Entity")
Select Case ActiveDocument.FormFields("Dropdown1").Result
Case "Add", "Amend"
With ActiveDocument
For i = 0 To UBound(ArrFmFld)
If .FormFields(ArrFmFld(i)).Result = vbNullString Then
MsgBox ArrFmFld(i) & " is empty"
.FormFields(ArrFmFld(i)).Select
' Then cancel closing of the document.
' Mark the document as unsaved so a prompt
' appears asking whether to save changes.
.Saved = False
' Dismiss the displayed prompt.
SendKeys "{ESC}"
Exit For
End If
Next
End With
Case Else
End Select
End Sub

gmayor
09-17-2014, 09:31 PM
Given that a person could change the Cost, Prog and Entity fields after updating to dropdown (indeed, this may even be the normal processing sequence)The process as described doesn't make a lot of sense. Without taking some remedial action what would be the point of this? I simply added a message box, but it might be better to select the errant field. It might be a plan to run the code on exit from the text fields also. I fear this is yet another case where we are only getting part of the story.