Universal change field agent

This tip will perform the following functions:
-select one or more documents (same form)
- agent gets all the fields in the form and presents them alphabetically in an inputbox
- choose a field
- enter the new value of that field
- agent changes the fieldvalue for all selected documents

Only a few errors are anticipated. In most cases when an error occurs the agent stops. It is imperative that you know what you are doing, otherwise this agent can do a lot of damage.

Sub main
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim dc As NotesDocumentCollection
	Dim doc As NotesDocument
	Dim form As Notesform
	Dim formvariant As Variant
	Dim FieldCounter As Integer
	Set db=session.CurrentDatabase
	Set dc=db.UnprocessedDocuments 'select all selected documents
	FieldCounter = 0
	If dc.count=0 Then ' no documents selected
		Msgbox "No documents selected" 'display error message
		Exit Sub ' end script
	Else 'at least 1 document has been selected
		Set doc = dc.GetFirstDocument ' get first document from 
		formvariant = doc.form(0) 'get the name of the form
		Set form = db.GetForm(formvariant) ' set form
	End If
	Dim itemType As Integer ' 768=numeric , 1024=datetime ,  
	Dim item As NotesItem
	Dim Veldtype As String
	Forall field In form.Fields 'get all fields in the form
		FieldCounter = FieldCounter + 1
		Redim Preserve FieldGroup(FieldCounter-1) 'keep the values 
in the array
		Set item = doc.GetFirstItem( Field )
		itemType = item.Type ' 768=numeric , 1024=datetime ,  
		Select Case itemType
		Case 768 : Veldtype="Num"
		Case 1024 : Veldtype="Dat"
		Case 1280 : Veldtype="Txt"
		Case Else : Veldtype="???" 
		End Select
		FieldGroup(FieldCounter-1) = Field + "   " + Veldtype ' 
example "FIELDNAME   Txt"
	End Forall
' sort the array alphabetically 
	Dim swapped As Integer
	Dim LoopCounter As Integer
	Dim temp As Variant
		swapped = 0 
		For LoopCounter = 0 To FieldCounter-2 
			If FieldGroup(LoopCounter + 1) < 
FieldGroup(LoopCounter) Then ' if yes then swap
				temp = FieldGroup(LoopCounter) 'swap
				FieldGroup(LoopCounter) = 
FieldGroup(LoopCounter + 1) 'swap
				FieldGroup(LoopCounter + 1) = temp 'swap
				swapped = 1 
			End If
		Next LoopCounter
	Loop Until swapped = 0 'all are sorted
'  end sort array alphabetically 
	Dim workspace As New notesuiworkspace
	Dim ChosenField As String
	Dim ChosenFieldLength As Integer
xt","",FieldGroup())) 'prompt for fields
	ChosenFieldLength=Len(ChosenField) ' determine length of the 
chosen field
	Dim ChosenFieldCut As String
	If Right$(ChosenField,3)="Num" Then 'Numeric
		ChosenFieldCut = Left$(ChosenField , ChosenFieldLength - 6) 
'FIELDNAME minus "   Num"
		NewValue = Clng(Inputbox("Enter the new value of " + 
ChosenFieldCut)) ' set new value of the chosen field
	End If
	If Right$(ChosenField,3)="Txt" Then 'Text
		ChosenFieldCut = Left$(ChosenField , ChosenFieldLength - 6) 
'FIELDNAME minus "   Txt"
		NewValue = Cstr(Inputbox("Enter the new value of " + 
ChosenFieldCut)) ' set new value of the chosen field
	End If
	For LoopCounter = 1 To dc.count 'for all selected documents
		Set doc = dc.GetNthDocument(LoopCounter) 'get document x
		doc.ReplaceItemValue ChosenFieldCut , NewValue 'replace the
 value of the field that has been chosen
		Call doc.save(True,True) 'save the document
		Set doc = dc.GetNextDocument(doc) ' get the next document
End Sub

This was first published in June 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.