Functions and Arrays

I have 2 functions func1 and func2 that are grabbing values from a sql db. I’m trying to populate the values to one form but everytime I run the agent it’s creating 2 forms. At the end of each function I have a BuildDetailDoc. Can someone help?

Thanks.

Sub func1

.

.

.

'go get the tags and associate them with the fields on the doc, and build the doc

BuildDetailDoc

End Sub

Sub BuildDetailDoc

Dim DetDoc As NotesDocument

Set DetDoc = New notesdocument(db)

Detdoc.form = "Details"



For yy = 0 To 5

	tag = TagArray(yy,0)

	value = TagArray(yy,1)

	Select Case tag

	Case "50FC106" : DetDoc.t50_1_lb = value

	Case "50FI218" : DetDoc.t50_2_lb  = value

	Case "50FQT_PRODUCT" : DetDoc.AceticProduct  = value 			

		

	Case "56F106C" : DetDoc.t56_1_lb = value

	Case "56F112C" : DetDoc.t56_2_lb = value

	Case "56F101" : DetDoc.t56_3_lb = value

		

		

	End Select

	

Next

success = detdoc.Computewithform(True, True)

Call detdoc.save(True,True)

End Sub

Subject: Functions and Arrays

code snippet looks ok

debug the code to see where the points of creation are.

Subject: RE: Functions and Arrays

It looks to be here (BuildDetailDoc) in each function. Run through func1 and builds a form, then it runs through func2 and builds a form. How can I combine?

Subject: RE: Functions and Arrays

As your BuildDetailDoc subroutine creates a new document and saves it then you will end up with two documents if you call both func1 and func2.

One way to do this would be instead of saving the document in BuildDetailDoc call func2 and pass your new document in as a parameter. Then save it at the end of func2

At the end of BuildDetailDoc

Call func2(DetDoc)

and

Sub func2(DetDoc as notesdocument)

If you have other scenarios when only func1 needs to be called then you will have to tweak this slightly but this is the general idea.

Subject: RE: Functions and Arrays

I’m getting one document now, but no data on the form. Any ideas?

Subject: RE: Functions and Arrays

Things I would check are:

you are saving the document at the end of func2.

the fields in the document properties to make sure that they really do not exist and it is an empty document. It could be that the fields do exist but are not displayed when you open the document.

run through the code in the debugger to see what the values are before you save the document.

If you still have problems then post your code and I will take a look.

Subject: RE: Functions and Arrays

I’ll give it a try and let you know either way, thanks.

Subject: RE: Functions and Arrays

Matt, I can only get data from my func1 or Dept50 function. Posting my code as you suggestted, can you tell me what I’m missing? Thanks.

[Options]

Option Public

Uselsx “*LSXODBC”

[Declarations]

Dim TagArray(5,1) As String

Dim db As notesdatabase

Dim DetDoc As NotesDocument

[Initialization]

Sub Initialize

Call Dept50

End Sub

[Dept50]

Sub Dept50

'===============================

’ Dim DetDoc As NotesDocument

'Set DetDoc = New notesdocument(db)

'Detdoc.form = "Details"



'===============================	







Dim session As New notessession



Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Dim comptag As String

Dim comptime As String

Dim compvalue As String

Dim compstatus As String

Set db = session.currentdatabase

Set qry.Connection = con



'build a list of all the tags to gather

BuildTagArray



BegDateTime = Format(Datenumber ( Year(Today) , Month(Today) , Day(Today)  ) , "dd-mmm-yy") + " 0:00"

EndDateTime = Format(Datenumber ( Year(Today) , Month(Today) , Day(Today)  ) , "dd-mmm-yy") + " 0:59"

'connect to PI and pass username

flag =	con.ConnectTo("Server1", "password")

If con.GetError <> DBstsSUCCESS Then

	Messagebox con.GetExtendedErrorMessage,, con.GetError & " " & con.GetErrorMessage

	Exit Sub

End If

'loop through all the tags and get the data from PI - should be one entry returned for each tag

For xx = 0 To 2

	

	qry.SQL = | SELECT tag, time, status, value FROM picomp WHERE tag = '| + TagArray(xx,0) + |'  AND time >= DATE('| + _

	BegDateTime + |') AND time <= DATE('| + EndDateTime + |') |

	

	

	Set result.Query = qry

	If qry.GetError <> DBstsSUCCESS Then

		Messagebox qry.GetExtendedErrorMessage,, qry.GetError & " " & qry.GetErrorMessage

		con.Disconnect

		Exit Sub

	End If

	

	result.Execute

	If result.GetError <> DBstsSUCCESS Then

		Messagebox result.GetExtendedErrorMessage,, result.GetError & " " & result.GetErrorMessage

		con.Disconnect

		Exit Sub

	End If

	

	If result.IsResultSetAvailable Then

		Do

			result.NextRow

			comptag = result.GetValue ( "TAG" )

			comptime = result.GetValue( "TIME" )

			compvalue = result.GetValue( "VALUE" )

			

			If comptag <> "" Then

				If compvalue = 0 Then

					compvalue = "N/A"

				Else

					compvalue = compvalue

				End If

				TagArray(xx,1) = compvalue

			Else

				TagArray(xx,1) = Round(Cdbl(compvalue),2)

			End If

			

			

			

		Loop Until result.IsEndOfData

		result.Close(DB_CLOSE)

		

	Else

		Messagebox "No result for " + TagArray(xx,0) + " from 1122."

	End If

Next xx



con.Disconnect

'go get the tags and associate them with the fields on the doc, and build the doc

BuildDetailDoc

End Sub

[Dept56]

Sub Dept56 (DetDoc As NotesDocument)

'===============================



'Dim DetDoc As NotesDocument

'Set DetDoc = New notesdocument(db)

'Detdoc.form = "Details"



'===============================	





Dim session As New notessession



Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Dim comptag As String

Dim comptime As String

Dim compvalue As String

Dim compstatus As String

Set db = session.currentdatabase

Set qry.Connection = con



'build a list of all the tags to gather

BuildTagArrayZ



BegDateTime = Format(Datenumber ( Year(Today) , Month(Today) , Day(Today)  ) , "dd-mmm-yy") + " 0:00"

EndDateTime = Format(Datenumber ( Year(Today) , Month(Today) , Day(Today)  ) , "dd-mmm-yy") + " 0:59"

'connect to PI and pass username

flag =	con.ConnectTo("Server2", "password")

If con.GetError <> DBstsSUCCESS Then

	Messagebox con.GetExtendedErrorMessage,, con.GetError & " " & con.GetErrorMessage

	Exit Sub

End If

'loop through all the tags and get the data from PI - should be one entry returned for each tag

For xx = 0 To 2

	

	'qry.SQL = | SELECT tag, time, status, value FROM picomp WHERE tag = '| + TagArray(xx,0) + |'  AND time >= DATE('| + _

	'BegDateTime + |') AND time <= DATE('| + EndDateTime + |') |

	

	qry.SQL = |SELECT tag, time, status, value FROM picomp WHERE tag = '| + TagArray(xx,0) + |' AND time >= DATE('| +_

	Format(Datenumber ( Year(Today) , Month(Today) , Day(Today)) , "dd-mmm-yy") + | 0:00')|

	

	Set result.Query = qry

	If qry.GetError <> DBstsSUCCESS Then

		Messagebox qry.GetExtendedErrorMessage,, qry.GetError & " " & qry.GetErrorMessage

		con.Disconnect

		Exit Sub

	End If

	

	result.Execute

	If result.GetError <> DBstsSUCCESS Then

		Messagebox result.GetExtendedErrorMessage,, result.GetError & " " & result.GetErrorMessage

		con.Disconnect

		Exit Sub

	End If

	

	If result.IsResultSetAvailable Then

		Do

			result.NextRow

			comptag = result.GetValue ( "TAG" )

			comptime = result.GetValue( "TIME" )

			compvalue = result.GetValue( "VALUE" )

			

			If comptag <> "" Then

				If compvalue = 0 Then

					compvalue = "N/A"

				Else

					compvalue = compvalue

				End If

				TagArray(xx,1) = compvalue

			Else

				TagArray(xx,1) = Round(Cdbl(compvalue),2)

			End If

			

			

			

		Loop Until result.IsEndOfData

		result.Close(DB_CLOSE)

		

	Else

		Messagebox "No result for " + TagArray(xx,0) + " from Dept56."

	End If

Next xx



con.Disconnect

'go get the tags and associate them with the fields on the doc, and build the doc

'BuildDetailDoc



'Call Dept50 (DetDoc)



success = detdoc.Computewithform(True, True)

Call detdoc.save(True,True)

End Sub

[BuildTagArray]

Sub BuildTagArray

TagArray(0,0) = "50FC106"

TagArray(1,0) = "50FI218"

TagArray(2,0) = "50FQT_PRODUCT"

End Sub

[BuildTagArrayZ]

Sub BuildTagArrayZ

TagArray(0,0) = "56F106C"

TagArray(1,0) = "56F112C"

TagArray(2,0) = "56F101"

End Sub

[BuildDetailDoc]

Sub BuildDetailDoc

Dim DetDoc As NotesDocument

Set DetDoc = New notesdocument(db)

Detdoc.form = "Details"



For yy = 0 To 5

	tag = TagArray(yy,0)

	value = TagArray(yy,1)

	Select Case tag

	Case "50FC106" : DetDoc.t50_1_lb = value

	Case "50FI218" : DetDoc.t50_2_lb  = value

	Case "50FQT_PRODUCT" : DetDoc.AceticProduct  = value 			

		

	Case "56F106C" : DetDoc.t56_1_lb = value

	Case "56F112C" : DetDoc.t56_2_lb = value

	Case "56F101" : DetDoc.t56_3_lb = value

		

		

	End Select

	

Next







'success = detdoc.Computewithform(True, True)

'Call detdoc.save(True,True)



Call Dept56 (DetDoc)

End Sub

Subject: RE: Functions and Arrays

I think I see the problem.

You intitially call Dept50 which retrieves some values and then calls BuildDetailDoc which creates a new document and sets some values. You then call Dept56 which retrieves some more values but as you have commented out the call to BuildDetailDoc in this subroutine no more values are set before you save the document.

Your initial problem was caused by the fact that you were creating and saving a new document in BuildDetailDoc which was being called by both Dept50 and Dept56.

I would dim DetDoc as a global variable, uncomment out the call to BuildDetailDoc in Dept56 and then amend BuildDetailDoc so that a new document is only created the first time it is called.

If DetDoc Is Nothing Then

Set DetDoc = New notesdocument(db)

End If

Subject: RE: Functions and Arrays

Is there another way to do this? I’m getting 10 documents created now.

Subject: RE: Functions and Arrays

I get no doc created with :

If DetDoc Is Nothing Then

	Set DetDoc = New notesdocument(db)		

End If 

And if I uncomment the code below, I still get 2 docs created.

'success = detdoc.Computewithform(True, True)

'Call detdoc.save(True,True)

Can you show me exaclty what you’re talking about with the code I sent? Thanks…this is really frustrating when I know it should be straight forward.

Subject: RE: Functions and Arrays

Amend your function BuildDetailDoc to

Sub BuildDetailDoc

If DetDoc Is Nothing Then

Set DetDoc = New notesdocument(db)

'for testing add this. You should only reach this line of code once when it is called by Dept50

Msgbox “Document Created”

End If

Detdoc.form = “Details”

For yy = 0 To 5

tag = TagArray(yy,0)

value = TagArray(yy,1)

Select Case tag

Case “50FC106” : DetDoc.t50_1_lb = value

Case “50FI218” : DetDoc.t50_2_lb = value

Case “50FQT_PRODUCT” : DetDoc.AceticProduct = value

Case “56F106C” : DetDoc.t56_1_lb = value

Case “56F112C” : DetDoc.t56_2_lb = value

Case “56F101” : DetDoc.t56_3_lb = value

End Select

Next

Call Dept56

End Sub

You no longer need to pass DetDoc as a parameter to Dept56 as it is a global so amend this to

Sub Dept56

…Main code

'go get the tags and associate them with the fields on the doc, and build the doc

BuildDetailDoc

success = detdoc.Computewithform(True, True)

Call detdoc.save(True,True)

End Sub

Subject: RE: Functions and Arrays

Getting an ‘Out of Stack Space’ with this. Will have to research this error. Thanks.

Subject: RE: Functions and Arrays

This explains stack space

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/21d858ba66c8a7a785256dc6006ee3a1?OpenDocument&Highlight=0,out,of,stack,space

In your Dept56 this line is commented out so I don’t see why you would receive this error

'Call Dept50 (DetDoc)

If it weren’t then your Dept50 function calls BuildDetailDoc which in turn calls Dept56. This would then call Dept50 which would call BuildDetailDoc which would call Dept56 and on and on until you ran out of memory.

Subject: RE: Functions and Arrays

Matt,

That’s what was causing the Stack error, Dept56 was getting called continuously and generating the error.