Subject: Problem setting Date in SQL-table
I agree with you: no problems with assigning variables, except dates as a variable into a SQL-table.
Here is a part of my code.
I use the ODBC-connection with the ODBC-driver “SQL Server”.
This configuration seems to work fine, for when I use AddRow I have no problems. But, as commented in my code, I prefer to use proper SQL-statements.
Sub Initialize
REM declaraties
On Error Goto Errhandler3
Print "Agent new prices started at "+Time$
Dim s As New NotesSession
Dim db As NotesDatabase
Set db=s.CurrentDatabase
Dim profdoc As NotesDocument 'document voorkeuren (BE)
Set profdoc=db.GetProfileDocument("voorkeuren")
Dim validfrom As NotesDateTime, validto As NotesDateTime
Dim validfromstring As String, validtostring As String
Dim con As New ODBCConnection 'Make ODBC Connection
If Not con.ConnectTo("DEVprices","********","********") Then
Print "Could not connect to DEVprices"
Exit Sub
End If
Dim qry As New ODBCQuery 'Define ODBC Query
Set qry.Connection = con
Dim result As New ODBCResultSet 'Set Query property of ODBCResultSet
Dim RetCode As Integer
Dim waarde As String, fomsexist As String
result.CacheLimit=DB_NONE
Dim prijslijst As String, artcode As String, tariefjaarstr As String
Dim tariefjaar As Integer
REM Set priceyear
tariefjaarstr=profdoc.nieuwtariefjaar(0)
REM tariefjaarstr=“2006”
tariefjaar=Cint(tariefjaarstr)
validfromstring="1-01-"+Cstr(tariefjaar)
validtostring="31-12-"+Cstr(tariefjaar)
Set validfrom=s.CreateDateTime(validfromstring)
Set validto=s.CreateDateTime(validtostring)
REM Add new records
REM ddoc and plijstdoc are set in previous art of the code, but now deleted
REM because this variables has nothing todo with my date-problem
Dim bedrag As String, prijs83 As String
REM the next two lines are to test setting the date in SQL
validfromstring=Cstr(tariefjaar)+"-01-01 00:00:00"
validtostring=Cstr(tariefjaar)+"-31-12 00:00:00"
prijslijst=ddoc.ProjectNr(0)
artcode=ddoc.GetItemValue("artcode1")(0)
prijs83=ddoc.GetItemValue("bedr1_1")(0)
Set result.Query=qry
REM The following line generates an error
sqlstr="INSERT INTO staffl(prijslijst, artcode, prijs83, bedr1, validfrom, validto,) values ('"+prijslijst+"', '"+artcode+"','"+prijs83+"','"+prijs83+"', '"+validfromstring+"', '"+validtostring+"')"
REM also tried following options, regarding to the responses in the IBM-forum
REM The following line generates an error
‘sqlstr="INSERT INTO staffl(prijslijst, artcode, prijs83, bedr1, validfrom, validto,) values (’“+prijslijst+”‘, ‘“+artcode+”’,’“+prijs83+”‘,’“+prijs83+”', ‘“+Cstr(validfromstring)+”’, ‘“+Cstr(validtostring)+”’)"
REM The following line is the only which works, but the year-part of the date has the be a variable (the current year)
‘sqlstr="INSERT INTO staffl(prijslijst, artcode, prijs83, bedr1, validfrom, validto,) values (’“+prijslijst+”‘, ‘“+artcode+”’,’“+prijs83+”‘,’“+prijs83+”', ‘2006-01-01 00:00:00’,‘2006-12-31 00:00:00’)"
Messagebox sqlstr
qry.SQL=sqlstr
result.Execute
REM The following code works, but I prefered to use proper SQL-statements.
%REM
result.AddRow
With result
Call .SetValue("prijslijst",prijslijst)
Call .SetValue("artcode",plijstdoc.GetItemValue("artcode"+Cstr(rij))(0))
Call .SetValue("validfrom",plijstdoc.validfrom(0))
Call .SetValue("validto",plijstdoc.validto(0))
End With
result.UpdateRow
%END REM
result.Close(DB_CLOSE)
con.Disconnect
Exit Sub
Errhandler3:
Print "An Error " & Error$ & "( " & Err & ") has occurred on line " & Erl & " in ExportAgent voor dossier. "&Cstr(prijslijst)
result.CurrentRow=currow 'na fout terug naar resultset
’ Resume Continue
Exit Sub
End Sub