Stuck on row one when populating a table from data

L

Linda

I thought that this was pretty straightforward code but I cannot get
off of row one.

When I execute this bit I end up with a table with one row of data and
it is the last row from the dataset pulled. It overwrites the header
row and every other recordset of information that the SQL statement
pulls. If I take the loop out, I get just the first row of data as you
would expect. There are no errors. Just poor results LOL.

Can someone spot why I am not getting additional rows? There should be
4 including the header.

With vRecordSet
If Not .EOF Then

.MoveFirst
rowcount = 1
Set objTable = ActiveDocument.Tables(1)
objTable.Cell(rowcount, 1).Range.Text = "Resolution #"
objTable.Cell(rowcount, 2).Range.Text = "Topic"
objTable.Cell(rowcount, 3).Range.Text = "Department"

Do While Not .EOF

rowcount = rowcount + 1
vResolutionNumber = vRecordSet("resolutionNumber")
vTopic = vRecordSet("topic")
vDepartment = vRecordSet("department")

objTable.Cell(rowcount, 1).Range.Text =
vResolutionNumber
objTable.Cell(rowcount, 2).Range.Text = vTopic
objTable.Cell(rowcount, 3).Range.Text = vDepartment

'Go to next record, exit after last
vRecordSet.MoveNext
Loop

Set objTable = Nothing

Else
'if you ARE *else* (are .EOF), that means no record was
matched, tell the user
MsgBox "No possible match was found."
End If

End With

********************************************************
I have tried a number of ways to insert rows but the following code
which seems like it should work, claims that part or all of the 2nd
line is not in reference to a table.

objTable.Rows(rowcount).Select
Selection.InsertRowsBelow
 
D

Doug Robbins - Word MVP

Use:

With vRecordSet
If Not .EOF Then

.MoveFirst
Set objTable = ActiveDocument.Tables(1)
objTable.Cell(1, 1).Range.Text = "Resolution #"
objTable.Cell(1, 2).Range.Text = "Topic"
objTable.Cell(1, 3).Range.Text = "Department"

Do While Not .EOF

ObjTable.Rows.Add
vRow=ObjTable.Rows.Count
vResolutionNumber = vRecordSet("resolutionNumber")
vTopic = vRecordSet("topic")
vDepartment = vRecordSet("department")

objTable.Cell(vRow, 1).Range.Text =
vResolutionNumber
objTable.Cell(vRow, 2).Range.Text = vTopic
objTable.Cell(vRow, 3).Range.Text = vDepartment

'Go to next record, exit after last
vRecordSet.MoveNext
Loop

Set objTable = Nothing

Else
'if you ARE *else* (are .EOF), that means no record was
matched, tell the user
MsgBox "No possible match was found."
End If

End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

Linda

Thank you. I tried that and now it is throwing an error saying I
cannot edit that region because document protection is in effect. It
is my understanding that you need to lock the document to make the form
fields work. is this what it is talking about or is there something
(obviously) that I am missing?
 
D

Doug Robbins - Word MVP

Sounds like the document is protected for forms.

At the beginning of the code insert

ActiveDocument.Unprotect

and at the end add

ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top