Make pre-prepared sql command in libreoffice basic - libreoffice-basic

I'm trying to make a prepared query based on the value in a field in my form in libreoffice basic.For this, I created a macro.
But it returns an error on the query line saying
BASIC syntax error.
Unexpected symbol: oInstruction_SQL
Sub concatMotherName
Dim oSourceDonnees As Object
Dim oConnexion As Object
Dim stSql As String
Dim oResultat As Object
oSourceDonnees = thisComponent.Parent.dataSource
oConnexion = oSourceDonnees.getConnection("","")
oInstruction_SQL = oConnexion.createStatement()
Dim valueData As String
Dim dateLabel As String
valueData = ThisComponent.Drawpage.Forms.getByName("Form").getByName("id_mother_label").getCurrentValue()
stSql = "SELECT NOM_MERE FROM ""T_MOTHER"" WHERE ""NUM_MOTHER"" = ?" _
oInstruction_SQL = = oConnection.prepareStatement(stSql)
oInstruction_SQL.setString(1, valueData)
oResultat = oInstruction_SQL.executeQuery(stSql)
If Not IsNull(oResultat) Then
oResultat.Next()
MsgBox oResultat.getString(1)
End If
End Sub

There are two syntax problems. The first is the _ after the query string, which indicates that the next line is a continuation of that one. It's not a continuation, so remove it.
The second error is on the next line: = =.
When these errors are fixed, the code compiles successfully.

Related

VBA dictionary as a property of a class

I have this simplified class named clsWarehouseSum.
Option Compare Database
Option Explicit
Private wh_units As Scripting.Dictionary
Public Function availableUnits(warehouse As String) As Long
'Debug.Print wh_units(warehouse)
If wh_units Is Nothing Then Set wh_units = New Scripting.Dictionary
If Not wh_units.Exists(warehouse) Then
Dim SQL As String
Dim RS As DAO.Recordset
SQL = "SELECT sum(units) as tot_units " _
& "FROM warehouse " _
& "WHERE warehouse = '" & warehouse & "' "
Set RS = CurrentDb.OpenRecordset(SQL)
wh_units.Add (warehouse), RS("tot_units")
End If
availableUnits = wh_units(warehouse)
End Function
I try to use it like this:
Sub test()
Dim wh As New clsWarehouseSum
Debug.Print wh.availableUnits("Cohasset")
Debug.Print wh.availableUnits("Cohasset")
End Sub
While the first Debug.Print prints what's expected, the second one gives me an error:
Run time error 3420, Object Invalid or no longer set. When I step through the code, it correctly evaluates both if statements as false. Yet, the last line of the function gives me the error mentioned above. What am I doing wrong?
Why?
Add Debug.Print TypeName(wh_units(warehouse)) before the availableUnits = wh_units(warehouse) line and if it prints anything else than Long to the Immediate window then you might want to cast to Long using CLng while you also have some error handler in place.
Or, you might want to make sure that the line wh_units.Add (warehouse), RS("tot_units") is adding a Long to your dictionary so you should check the type before you add.
As a general rule, when you return a specific data type from a dictionary or collection, you should always have checks in place either when you add the data to the dict/coll or when you return it so that you avoid type incompatibility and runtime errors.

Access VBA SQL Complie Error: Type Mismatch

This following bit of code is placed in the On Load event of a form:
Dim LAOQWeekdaySQL As String
Dim LAOQWeekday As QueryDef
Dim EnqIDTest As String
EnqIDTest = Me.txt_EnquiryID.Value
LAOQWeekdaySQL = "SELECT tbl_Costing_Labour.EnquiryID, tbl_Costing_Labour.PhaseLabour, tbl_Costing_Labour.TotalPhaseLabourHours " & _
"FROM tbl_Costing_Labour " & _
"WHERE (((tbl_Costing_Labour.EnquiryID)= " & EnqIDTest & ") AND ((tbl_Costing_Labour.PhaseLabour) Like ""*weekday*""));"
Set LAOQWeekday = CurrentDb.QueryDefs("LAOQWeekdayQRY").SQL = LAOQWeekdaySQL
Me.txt_LAOQ_Weekday.Value = DSum("TotalPhaseLabourHours", "LAOQWeekdayQRY")
Essentially it returns records matching the Inquiry Number and sums the final column in the query and outputs it to a textbox on the form.
However, when I got run the form it gives me a
compile error: type mismatch
Prior to this I did try and use a temporary query but then the Dsum has no name to reference when performing its function so I changed it .CreateQueryDef, which work fine the first time. Thereafter, obviously, it threw an error saying the query already exists so I went down the route of .QueryDefs to update the SQL instead and now I get the mismatch error.
I cannot see anything obvious, what am I missing here?
Thank you in advance for any help you can provide!
The following line evaluates to a Boolean:
Set LAOQWeekday = CurrentDb.QueryDefs("LAOQWeekdayQRY").SQL = LAOQWeekdaySQL
Try changing it to this:
Set LAOQWeekday = CurrentDb.QueryDefs("LAOQWeekdayQRY")
LAOQWeekday.SQL = LAOQWeekdaySQL

VB net & Mongo: Using where clause with LINQ causes error "Unsupported where clause: (Boolean)Operators.CompareObjectLess"

I have a collection in MongoDB and I'm using MongoDB Driver in VB net. I want to update several documents depending on a condition.
For this, I want to use LINQ, but the select causes an error and I don't know how to fix it.
Here's the code:
Dim update_for As UpdateBuilder
Dim query_for As IMongoQuery
Dim coll_for = db.GetCollection(Of MyClass)("collection_1")
Dim queryMun = (From a In coll_for _
Where (a.field_1 < 10000) _
Select a)
For Each emp In queryMun
query_for = Query.EQ("_id", emp.Id)
update_for = Update.Set("field_1", BsonValue.Create("0" + emp.field_1))
coll.Update(query_for, update_for, opts)
Next
When it executes de For Each sentence, it raises the exception: Unsupported where clause: (Boolean)Operators.CompareObjectLess(a.field_1, 10000, true).
What am I doing wrong?
Thank you very much for your help.
I think the error is clear:
You can't use a Less Than "<" operator in you WHERE clause because it's unsupported.
I have found a way to do this update based on the value of the attribute itself. What I want to do is add a "0" at the beginning of the attribute value, for example, if field_1=4567, after the update field_1='04567'.
Here's the code:
Dim update_for As UpdateBuilder
Dim query_for As IMongoQuery
Dim opts = New MongoUpdateOptions
opts.Flags = UpdateFlags.Multi
Dim coll_for = db.GetCollection(Of MyLINQClass)("collection_1")
Dim queryMun2 As New QueryDocument
Dim query_1 = Query.LT("field_1", MongoDB.Bson.BsonValue.Create(10000))
queryMun2.AddRange(query_1.ToBsonDocument)
Dim queryMun = coll_for.Find(queryMun2)
For Each emp In queryMun
query_for = Query.EQ("_id", emp.Id)
update_for = Update.Set("field_1", BsonValue.Create("0" + emp.FField_1.ToString))
coll.Update(query_for, update_for, opts)
Next
And here is the definition of MyLINQClass:
Public Class MyLINQClass
<BsonElementAttribute("_id")> _
Public Property Id() As ObjectId
<BsonElementAttribute("field_1")> _
Public Property FField_1() As Object
End Class

VBA debugging "Expected: ="

Function PrintTableDefs()
Dim aDB As DAO.Database
Dim aTD As DAO.TableDef
Dim aTableName As String
Dim aForeignTableName As String
Dim aString As String
Dim count As Integer
Set count = 0
Set aDB = CurrentDb()
For Each aTD In aDB.TableDefs
Debug.Print aTD.Name
Debug.Print aTD.Connect
Debug.Print ""
count = count + 1
Next
Debug.Print "There are " & count & "table defs."
End Function
When calling this function I get "Compile Error: Expected: =". I have noticed through other questions it is related to parentheses however being mildly unfamiliar with VBA I'm unsure where my syntax is off.
Remove the set from set count = 0
set is only for assigning object references and an Integer is not an object instance.
Alex pointed out the only issue in your function that could throw a compile error: you don't need a set to initialize your count variable, because it is not an object. The rest is correct and should not throw an error, so it is elsewhere.
However your code shows that you don't really understand what is the difference between a sub and a function and it might be helpful to explain it.
A function returns a value, a sub doesn't. That's what's make them different.
You are not returning any value so you should use a sub.
Although your code works fine, because functions that doesn't return a value are accepted by the VBA compiler, it is semantically incorrect and a bad programming habit.
You can make a proper function out of it by returning the number of tables you found (your count variable)
Sub test()
Debug.Print "There are " & PrintTableDefs() & " table defs."
End Sub
Function PrintTableDefs() As Integer
Dim aDB As DAO.Database
Dim aTD As DAO.TableDef
Dim aTableName As String
Dim aForeignTableName As String
Dim aString As String
Dim count As Integer
count = 0
Set aDB = CurrentDb
For Each aTD In aDB.TableDefs
Debug.Print aTD.Name
Debug.Print aTD.Connect
Debug.Print ""
count = count + 1
Next
PrintTableDefs = count
End Function
And if you don't need anything to be returned, make a sub:
Sub PrintTableDefs()
Dim aDB As DAO.Database
Dim aTD As DAO.TableDef
Dim aTableName As String
Dim aForeignTableName As String
Dim aString As String
Dim count As Integer
count = 0
Set aDB = CurrentDb
For Each aTD In aDB.TableDefs
Debug.Print aTD.Name
Debug.Print aTD.Connect
Debug.Print ""
count = count + 1
Next
Debug.Print "There are " & count & " table defs."
End Sub
Additionally, you don't need () when you assign the CurrentDb object :
Set aDB = CurrentDb
My experience is that when calling a function or subroutine in VBA, enclosing your parameters (or lack thereof) in parentheses will cause this error.
There are two ways I know of to handle this:
Just don't use parentheses and always call the sub or function without them. This is fine if you are going from "PrintTableDefs()" to "PrintTableDefs" but can be problematic if the call is nested in a control structure or as another parameter.
Keep your parentheses, because you like parentheses and they remind you of C based language syntax, which helps you forget you are using the sadness of VBA. In that case, always assign the value of a function to a variable "result = PrintTableDefs()" and always use the Call keyword when invoking subroutines.
As an aside: your function returns no value. You should probably replace it with a subroutine, which is done by just replacing the word "Function" with the word "Sub".

LINQ Query Causing Exit Sub or Swallowing Error

My code is as follows:
Using _EntityModel As New AboveNemaSalesDatabaseEntities()
Dim _SelectActiveOptionCodes = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select _OptCodes.fdDescription, _OptCodes.fdOptionCode).ToList()
Dim _SelectActiveOptionCodes2 = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select New optionCodes With {.description = _OptCodes.fdDescription,
.optionCode = _OptCodes.fdOptionCode})
sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes
OptionCodeListBox.DisplayMember = "fdDescription"
OptionCodeListBox.ValueMember = "fdOptionCode"
End Using
The first query works fine and returns a list in the format [index]{description = "descritption here", optionCode = "option code here"}
The second query creates but when it is called to save to my custom class the program exits the sub or swallows an error. Stepping through the code, the line starting with sortedOptionCodes and after never runs.
The main issue I was dealing with is that my query was producing a list of optionCodes and my variable was not prepared to store this.
Old variables:
Dim sortableOptionCodes As optionCodes
Dim sortedOptionCodes As optionCodes
New variables:
Dim sortableOptionCodes As List(Of optionCodes)
Dim sortedOptionCodes As List(Of optionCodes)
I also added a .ToList() function to the end of the second query.