My environment:
Windows 10, LibreOffice Version: 6.4.0.3 (x64) HSQLDB Split Database
Scenario:
I have many macros from MSAccess using the DLookup function which I am converting to LO.
For efficiency and code clarity, I want to make a subroutine to do this.
Problem:
My problem is syntax, I cannot figure out how to use variables in the sql statement.
My Question:
What is the proper syntax for my sql line?
Error Details:
In the code below, I have rem’d out the different versions I tried.
Test #0 works. It is the usual syntax when used without variables in a subroutine.
The other versions are tests none of which work, giving following error messages whilst highhlighting line
sQuery = oStatement.ExecuteQuery(sSqlTask)
Test #1
BASIC runtime error. An exception occurred Type: com.sun.star.sdbc.SQLException Message: unexpected token: Coordinates.
Test #2
An exception occurred Type: com.sun.star.sdbc.SQLException Message: user lacks privilege or object not found: sSource.
Test #3
BASIC runtime error. An exception occurred Type: com.sun.star.sdbc.SQLException Message: user lacks privilege or object not found: sSource.
Test #4
BASIC runtime error. An exception occurred Type: com.sun.star.sdbc.SQLException Message: unknown token:
Here is the code
Sub TryLookUp
LookUpSql("Name ID","Coordinates","Coord ID",25)
End Sub
Sub LookUpSql (sSeekField As String,sSource As String,sWhereItem As String,sCompareItem AS String)
Dim oStatement As Object
Dim sQuery As Object
Dim sSqlTask As String
Dim sLookup As String
If IsNull(ThisDatabaseDocument.CurrentController.ActiveConnection) Then
ThisDatabaseDocument.CurrentController.connect
End If
' #0 sSqlTask = "SELECT distinct ""Name ID"" FROM ""Coordinates"" WHERE ""Coord ID"" = '" & sCompareItem & "'"
' #1 sSqlTask = "SELECT distinct '" & sSeekField & "' FROM '" & sSource & "' WHERE '" & sWhereItem & "' = '" & sCompareItem & "'"
' #2 sSqlTask = "SELECT distinct sSeekField FROM sSource WHERE sWhereItem = '" & sCompareItem & "'"
' #3 sSqlTask = "SELECT distinct ""sSeekField"" FROM ""sSource"" WHERE ""sWhereItem"" = '" & sCompareItem & "'"
' #4 sSqlTask = "SELECT distinct & 'sSeekField' FROM & 'sSource' & WHERE & 'sWhereItem' & = '" & sCompareItem & "'"
oStatement = ThisDatabaseDocument.CurrentController.ActiveConnection.createStatement()
sQuery = oStatement.ExecuteQuery(sSqlTask)
sQuery.Next()
sLookup = sQuery.GetString(1)
MsgBox ("Result= " & sLookUp)
End Sub
Thank you for assistance.
EDIT: Solved
For anyone interested here is a working example version of the code
Sub TryLookUp
LookUpSql("Name","Employees","PersonID",25)
End Sub
Sub LookUpSql (sSeekField As String,sSource As String,sWhereItem As String,sCompareItem AS String)
Dim oStatement As Object
Dim sQuery As Object
Dim sSqlTask As String
Dim sLookup As String
If IsNull(ThisDatabaseDocument.CurrentController.ActiveConnection) Then
ThisDatabaseDocument.CurrentController.connect
End If
sSqlTask = "SELECT distinct """ & sSeekField & """ FROM """ & sSource & """ WHERE """ & sWhereItem & """ = '" & sCompareItem & "'"
oStatement = ThisDatabaseDocument.CurrentController.ActiveConnection.createStatement()
sQuery = oStatement.ExecuteQuery(sSqlTask)
sQuery.Next()
sLookup = sQuery.GetString(1)
MsgBox ("Result= " & sLookUp)
End Sub