Sometimes there was an error on line#36.
What code should be needed more ?
Edited:
Attached new revised file:
0035DisposedoStatement_Revised.ods (51.8 KB)
Option Explicit
Sub SaveData
Dim db, oStatement, oResult As Object
Dim CursorTest As Variant
Dim sSql, dDatabase As String
dDatabase = "file://" & Environ("HOME") & "/Documents/LibreOffice/LibreOfficeAskLibreOffice/0035DisposedoStatement/" & "inventory1.odb"
Dim fFruitCode,wWeekNumber As String
Dim qQuantity As Double
fFruitCode = ThisComponent.Sheets.getByName("UI").getCellRangeByName("C4").String
wWeekNumber = ThisComponent.Sheets.getByName("UI").getCellRangeByName("C6").String
qQuantity = ThisComponent.Sheets.getByName("UI").getCellRangeByName("C7").Value
' Check available balance
If qQuantity < 0 Then
If (getBalance(fFruitCode) + qQuantity) < 0 Then
MsgBox "Fruit balance is insufficient.",,"Error"
Stop
End If
End If
On Local Error GoTo CloseConn
sSql = "SELECT DISTINCT ""FruitCode"" FROM ""Table1"" WHERE ""FruitCode"" = '" & fFruitCode & "'"
db = ConnectDatabase(dDatabase)
oStatement = db.CreateStatement
oResult = oStatement.ExecuteQuery(sSql) ' Line 36'
CursorTest = oResult.first
' Check if selection exists'
If CursorTest = "True" Then
' When code is correct, add to table.'
sSql = "INSERT INTO ""Table1""(""FruitCode"",""WeekNumber"",""QuantityInOutKilogram"") VALUES('" & fFruitCode & "', '" & wWeekNumber & "', '" & qQuantity & "')"
oStatement = db.CreateStatement
oStatement.executeUpdate(sSql)
Else
MsgBox "Wrong fruit code",,"Error"
End If
MsgBox "Data saved successfully",,"Result"
Exit Sub
CloseConn:
MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")",,"Error"
DisconnectDatabase(db)
End Sub
Function getBalance(pFruitCode)
Dim db, oStatement, oResult,oRowSet As Object
Dim dbf, sSql, dDatabase As String
dDatabase = "file://" & Environ("HOME") & "/Documents/LibreOffice/LibreOfficeAskLibreOffice/0035DisposedoStatement/" & "inventory1.odb"
sSql = _
"SELECT ""FruitCode"", SUM( ""QuantityInOutKilogram"" ) " & _
"FROM ""Table1"" GROUP BY ""Table1"".""FruitCode"" " & _
"HAVING ( ( ""FruitCode"" = '" & pFruitCode & "' ) ) ORDER BY ""FruitCode"" ASC"
On Local Error GoTo CloseConnection
dbf = dDatabase
db = ConnectDatabase(dbf)
oStatement = db.CreateStatement
oRowSet = GetRowSet(db, sSql)
While oRowSet.Next
If (GetRowSet(db, sSQL).RowCount = 1) Then
getBalance = Val(oRowSet.GetString(2))
End If
Wend
DisconnectDatabase(db)
Exit Function
CloseConnection:
MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")"
DisconnectDatabase(db)
End Function
Function ConnectDatabase(dbFilename$) As Object
Dim dbContext As Object : dbContext = createUNOService("com.sun.star.sdb.DatabaseContext")
Dim oDataSource As Object : oDataSource = dbContext.GetByName(dbFilename)
ConnectDatabase = oDataSource.GetConnection("","")'>>("Username","Password")'
End Function
Sub DisconnectDatabase(db)
db.getParent().flush
db.Close
db.Dispose()
End Sub
Function GetRowSet(db As Object, iSQL$) As Object
Dim oRowSet As Object : oRowSet = CreateUNOService("com.sun.star.sdb.RowSet")
With oRowSet
.ActiveConnection = db
.Command = iSQL
.Execute
End With
GetRowSet = oRowSet
End Function
inventory1.odb (3.8 KB)
0035DisposedoStatement.ods (45.9 KB)
LibreOffice:
Version: 7.3.6.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.6-0ubuntu0.22.04.2
Calc: threaded
Base:
Firebird
OS:
Ubuntu GNOME Desktop 22.04 LTS