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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterate over empty dropdown formfields

Status
Not open for further replies.

shelley2000

Programmer
Sep 9, 2008
2
US
Hi, Help, and Thanks

I am using Word 2003 and Access 2003. For reasons out my control, I have to use Word Forms vice Access Forms. I will be sending a Word Form to my department employees to fill out. the number of fields they need to fill out is variable. I want to check each of the form fields to see if the they have data or not. My dropdown form field check is erroring with ...
"5825; Object has been deleted"

For example, I have a Word table to select up to 15 Projects. each of the 15 rows in Word TAble has 1) a drop down to allow users to select the projects they have worked on. The drop down list is populated from Access Project name table and I was unsuccesful on getting word dropdown to take a default. The second column is the number of years.

Users will just select the number of projects the other drop downs will be empty. I need to check the drop down to see if it is empty and I don't know how to do it without getting error.

Projects: (Select up to 15 projects)
Count Project Name Years
1. <drop down list> <text numeric>
...
15. <drop down list> <text numeric>

Here are the things I have tried that crash:
If IsNull(ActiveDocument.FormFields(FieldName1))
If Len(Nz(ActiveDocument.FormFields(FieldName1).Result, "") & vbNullString) > 0 Then


Here is the code:
Dim rstEmployeeProject As New ADODB.Recordset
Dim rstProjects As New ADODB.Recordset
Dim ProjectNameIn As String
Dim prjctid As Integer

rstEmployeeProject.Open "tblEmployeeProject", cnn, adOpenKeyset, adLockOptimistic
strSQL = "select ProjectID, ProjectNameShort from tblProjects order by ProjectNameShort"
rstProjects.Open strSQL, cnn, dOpenKeyset, adLockOptimistic, adCmdText
With rstEmployeeProject
For Counter = 1 To 15 ' There are 15 Projects allowed in form

FieldName1 = "fldSklPExpNm" & Counter
FieldName2 = "fldSklPExpNm" & Counter & "Yr"
If IsNull(ActiveDocument.FormFields(FieldName1)) Then
MsgBox "null field"
Counter = 16
Else
rstProjects.MoveFirst
If Len(Nz(ActiveDocument.FormFields(FieldName1).Result, "") & vbNullString) > 0 Then
ProjectNameIn = ActiveDocument.FormFields(FieldName1).Result
recCount = rstProjects.RecordCount
'prjctid = DLookup("[ProjectID]", "tblProjects", "[ProjectNameShort]='QL'")
rstProjects.Find "ProjectNameShort='" & ProjectNameIn & "'"
If rstProjects.EOF Then
rstProjects.MoveFirst
MsgBox "ProjectNameShort =" & ProjectNameIn & "was not found. Using 26: other Project name. please log problem."
rstProjects.Find "ProjectNameShort='Other'"
If rstProjects.EOF Then
MsgBox "ProjectNameShort = 'Other' was not found. No project was added. Please log problem."
Else
prjctid = rstProjects.Fields(0)
End If
Else
prjctid = rstProjects.Fields(0)
End If
rstEmployeeProject.AddNew
rstEmployeeProject!employeeID = rstEmployees!employeeID
rstEmployeeProject!projectID = prjctid
rstEmployeeProject!Years = ActiveDocument.FormFields(FieldName2).Result
rstEmployeeProject.Update
Else '****Either an empty record was found or the last record was processed ****"
Counter = 16
End If
End If
Next
rstProjects.Close
rstEmployeeProject.Close
End With '**** end with rstEmployeeProject ****'

Help: how do I check the dropdown formfields to see if they have been updated or not with getting error.

Thanks,
shelley
 





"5825; Object has been deleted"

Is your Word Form PROTECTED?


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,

I turned off lock before I ran the code. So, the form was not protected. Should it be? My export from word and push to access module is associated with Word Doc because I want the admin to run the macro when she receives the word doc as an attachment.

Thanks,
shelley
 
Hi Shelley,

You can test the dropdown values quite simply, using code along the lines of:
Code:
Sub Demo()
Dim oFld As FormField
For Each oFld In ActiveDocument.FormFields
  If oFld.Type = wdFieldFormDropDown Then
    MsgBox oFld.Result
    MsgBox oFld.DropDown.Value
  End If
Next
End Sub
With this code, 'oFld.Result' returns the displayed value and 'oFld.DropDown.Value' returns the index number of the displayed value.

You also mention having trouble setting the default value. Using the above routine, you could do that with:
oFld.DropDown.Default = 2
where '2' is the index number of the entry you want to set as the default.

Regarding the protection issue, if the document isn't protected for forms, then your users are liable to delete the formfields when they try to update the document - and then your macro would fail.

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top