Global ODBC setup

Hello all,

I’ve currently been using an agent that allows for odbc access to a DB2/400 database. However, calling this agent manually requires me to open up the connection to DB2 and do the processing.

I figured that it would work best if I opened up the connection when the form loads. I put the connection code in the Global declarations area, and then a drop down box would trigger the SQL statements to hit the database. I’m thinking that this would increase performance.

However, when running the sql scripts, I get a generic lotusscript error. Here’s the code. What am I doing wrong? Debugger tells me it’s choking at the “Set qry.Connection = ODBCcon” statement.

On the form:

[Options]

Option Public

Uselsx “*LSXODBC”

[Declarations]

Dim ODBCcon As ODBCConnection

Dim ODBCconnected As Variant ’ set to true when connected to an ODBC data source

Sub Initialize

connectODBC

End Sub

Sub connectODBC

Set ODBCcon = New ODBCConnection



If ODBCcon.ConnectTo("xxx", "xxx" , "xxx") Then    

	ODBCconnected = True

	

	

Else

	Print "Replicate Item Documents Is NOT Connected"

	Print ODBCcon.GetExtendedErrorMessage,,

	ODBCconnected = False

	Print odbccon.GetErrorMessage

End If

End Sub

The agent that does the SQL processing:

Sub process

Dim db As NotesDatabase

Dim view As NotesView

Dim workspace As New NotesUIWorkspace

Dim session As New notessession

Dim agent As Notesagent

Dim doc As NotesUIDocument

Dim notesyear As Variant

Dim notesmonth As Variant

Dim datstr As Double





Set session = New NotesSession

Set db = session.CurrentDatabase

Set doc = workspace.CurrentDocument



notesyear = doc.fieldgettext("Year")

notesmonth = doc.fieldgettext("Month")



Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Set qry.Connection = ODBCcon



qry.sql = "select * from library.database where PRDYR = '" +notesyear + "' AND PRDMN = '" + notesmonth + "'"



Set result.Query = qry

result.Execute

If Not result.Execute Then

	Messagebox result.GetExtendedErrorMessage,, _

	result.GetErrorMessage

	Exit Sub

End If

datstr = result.GetValue("PRDDYS")

Call doc.fieldsettext("ProratedDays",Cstr(result.GetValue("PRDDYS")))

result.Close(DB_CLOSE)

End Sub

Subject: Global ODBC setup

Where and how (and why??) are you calling the agent?

The global declarations in the form will not be seen by the agent. You could try instead to create a ScriptLibrary. Include the Script library in the Options section of your form. Move your form global declarations to the corresponding location in the script library. Your form initialise event (or postopen event if you prefer) can instantiate these global variables. I would then set up the agent code as subs/functions in the script library. These would then be visible anywhere on the form.

Subject: RE: Global ODBC setup

Thanks for your help. I’m fairly new to this.

I need to have combination boxes fire off sql scripts on their change events. That’s why I need the connection opened. Otherwise it opens the connection every time and it’s looking like a hog.

I’m not sure of a better way to accomplish what I need to do in this application.

I’ve moved all previous global declarations into a script library and I’ve included the library in the options section of the form. I’m not sure how to initialize the global variables however.