Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add new row in form table only when a field is not blank 1

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
520
US
Hi All;

I am trying to add a new row in a table only when the last field in the table is not blank. Currently I have the code for adding the line and blank fields. I can run the code when the field is exited without any issues.

Several things that I would like to do:

1) I want to only activate this on the last field on the last row of the table only if it is not blank
2) I don't know how to do this based on a random field bookmark name that is created from the following code
3) How do I de-activate the code in any lines above the new blank table row and field where this code has already been been run

The code currently activates on the exiting of form field. There are some issues.

1) If statement is not correctly activating due to me not knowing the proper formula to activate the if statement
2) In the second with statement, I don't know how to update the form field name from the current name "Text52" to the newly created form field
3) Other issues are also occuring

any assistance would be appreciated.

Mike

Code:
Sub test()

If Selection.FormFields > 0 Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        
    With Selection.FormFields(1)
    With Selection.FormFields
    
        '.Name = "Text52"
        .EntryMacro = ""
        .ExitMacro = "test"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdRegularText, Default:="", Format:=""
            .Width = 0
        End With
    End With
    

Else: End If

End Sub
 
Hi,

Not a Word guru, but here’s a tack to take.

[pre]
Sub test()
If [Table Cell Row] = [Table Row Count] Then
‘Put code to insert row here
End If
End Sub
[/pre]

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Hi Skip,

I updated the code per your recommendation and I am now getting a stupid error. Else without if statement or end if without if. Any ideas on how to correct it?

Code:
Sub test3()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        
    'With Selection.FormFields(1)
        With Selection.FormFields(1)
        
            '.Name = "Text52"
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With
    'End With
    

Else

If Selection.FormFields(1) < ActiveDocument.Tables(1).Rows.Count Then

End If


End Sub
 
You do not need...
Code:
Else[s]
If Selection.FormFields(1) < ActiveDocument.Tables(1).Rows.Count Then[/s]

End If

You also seem to have a missing End With.

...and does this return a row number???
Code:
Debug.Print Selection.FormFields(1)

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
I am getting a compile error now. "Else without if" and it is again highlighting the Else above.

I created a mini macro that returns the proper row count.

Code:
number_of_Rows = ActiveDocument.Tables(1).Rows.Count
 
Did you take care of the missing End With?

Need to see your current code.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Code:
Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With

Else: End If

End Sub
 
[highlight #FCAF3E]This[/highlight] does not return a Row Number!

You are missing [highlight #FCE94F]this[/highlight]
Code:
Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If [highlight #FCAF3E]Selection.FormFields(1)[/highlight] = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
            [highlight #FCE94F]End With[/highlight]
        End With

Else: End If

End Sub

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
I am now getting another error:

5941 - The requested member of the collection does not exist. Error occurs on line 1 of the code

current code:

Code:
Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With
    End With

Else: End If

End Sub
 
Check out faq707-4594.

Highlight Selection.FormFields(1) and then Add a Watch Window.

Observe the results.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Not much actually happened... it looks like it is just having an error...

expression: Selection.FormFields(1)
Value = <Out of context>
Type = empty
context: New:Macros, Test4

I am not sure what this might mean...
 
Hi marcropod,

I don't have any issue with adding a row with fields. It is the conditions in which the row is to be added.

I need to only add the new line and fields when the last field in the last row is filled in. While the macro will launch on exiting the field, I need it to check that it is both not empty and that it is in the last row of the table.

If the last box in the row is filled in, but it isn't in the last row, I don't want a new row added.
 
Plz upload your document.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
I can’t seem to find the FormField ID of the Selection.

Maybe marcropod can help. As I conceive it, you have ActiveDocument.FormFields.Count and the Index of the FormField in question, which I cannot seem to access. When those values are equal, you’ve exited the bottom-right cell of your table.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
remeng: You really should have looked at the code I directed you to.

Try replacing all of it with:

Code:
Sub AddRow()
Application.ScreenUpdating = False
'This macro adds another row to the table.
' If you need the formfields named, activate the commented-out code.
' As coded, the formfield names assume each column uses a different formfield
' name ending in a two-digit number corresponding to the formfield row. For
' example, formfield names on tabe row 2, which is formfield row 1, end with 01.
Dim i As Long, j As Long, FmFld As FormField, Prot As Variant
'Dim StrNms As String
Const Pwd As String = "" 'Insert password here
With Selection
  i = .Tables(1).Range.FormFields.Count
  j = ActiveDocument.Range(.Tables(1).Range.Start, .Range.End).FormFields.Count
End With
If i = j Then
  With ActiveDocument.FormFields(ActiveDocument.Tables(1).Range.FormFields.Count)
    If .Result = .TextInput.Default Then Exit Sub
  End With
  If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
    With ActiveDocument
      Prot = .ProtectionType
      If .ProtectionType <> wdNoProtection Then
        Prot = .ProtectionType
        .Unprotect Password:=Pwd
      End If
      With Selection.Tables(1).Rows
        i = .Count
        With .Last.Range
          'For Each FmFld In .FormFields
            'StrNms = StrNms & "|" & Left(FmFld.Name, Len(FmFld.Name) - 2) & Format(i, "00")
          'Next
          .Next.InsertBefore vbCr
          .Next.FormattedText = .FormattedText
        End With
        With .Last.Range.FormFields
          For i = 1 To .Count
            With .Item(i)
              If .Type = wdFieldFormCheckBox Then .CheckBox.Value = False
              If .Type = wdFieldFormTextInput Then .TextInput.Clear
              If .Type = wdFieldFormDropDown Then .DropDown.Value = .DropDown.Default
              '.Select
              'With Dialogs(wdDialogFormFieldOptions)
                '.Name = Split(StrNms, "|")(i)
                '.Execute
              'End With
            End With
          Next
          .Item(1).Select
        End With
      End With
      .Protect Type:=Prot, Password:=Pwd, Noreset:=True
    End With
  End If
End If
Application.ScreenUpdating = True
End Sub
and making 'AddRow' the on-exit sub called by your initial last formfield.

Cheers
Paul Edstein
[MS MVP - Word]
 
Code:
Dim iFldCnt As Integer
iFldCnt = ActiveDocument.FormFields.Count
If Trim(ActiveDocument.FormFields(iFldCnt).Result) <> “” Then
‘Your add row code here
End If

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Hi skip,

I tried adding your code. The code just jumps to the End If statement at the end. Maybe I did something incorrect...

Code:
Sub create_row2()

' create_row Macro

Dim iFldCnt As Integer
iFldCnt = ActiveDocument.FormFields.Count
If Trim(ActiveDocument.FormFields(iFldCnt).Result) <> “” Then

' Add row code

    Selection.InsertRowsBelow 1
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.PreviousField.Select
    With Selection.FormFields(1)
       ' .Name = "Text55"
        .EntryMacro = ""
        .ExitMacro = "create_row2"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdRegularText, Default:="", Format:=""
            .Width = 0
        End With
    End With

' end add row code

End If

End Sub
 
Well was there a Value in the last FormField?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top