问题
I have a form in Libreoffice Base which is connected to the "Songs" table (basicaly db of music) and what I want to do is everytime I check/uncheck the CheckBox on that form I want the field "Played" of every record with the same name and Author as the one I have currently on the form to be checked/unchecked as well. I have read that the only way of doing so is with macros (as I don't want to use relations cause I have to many records for now). I've written such a macro:
Sub UpdatePlayed()
Context = CreateUnoService("com.sun.star.sdb.DatabaseContext")
databaseURLOrRegisteredName = "file:///C:/Users/grzes/Desktop/Muzyka.odb"
Db = Context.getByName(databaseURLOrRegisteredName )
Conn = Db.getConnection("","") 'username & password pair - HSQL default blank
dCheckBox = Forms("Formularz").Controls("CheckBox").Value
dAuthorBox = Forms("Formularz").Controls("AuthorBox").Value
dTitleBox = Forms("Formularz").Controls("TitleBox").Value
Stmt = Conn.createStatement()
strSQL = "UPDATE ""Songs"" SET ""Played"" = " + dCheckBox + " WHERE ""Title"" = '" + dTitle + "' AND ""Author"" = '" + dAuthor + "'"
Stmt.executeUpdate(strSQL)
Conn.close()
End Sub
(AuthorBox and TitleBox are textboxes and CheckBox is a CheckBox with checked set to 1 and unchecked to 0) but nothing happens when the macro is executed (bound as Mouse Button Pressed event to the checkbox itself)
I'm sure that whe way of executing SQL query is right as in another macro I use it as well without any problem so the problem must be either with setting the variables dcheckbox, dauthorbox and dtitlebox or with the strSQL. (The macro itself is running as when I change the control name I get an error). so the question is: What's wrong with it?..
Thanks in advance for your help.
回答1:
The reason nothing is happening is because the variables dTitle
and dAuthor
are empty. Notice the variable names do not match. So updating where title and author are empty affects 0 rows.
Here is working code:
Sub UpdatePlayed(oEvent As Object)
Context = CreateUnoService("com.sun.star.sdb.DatabaseContext")
databaseURLOrRegisteredName = "file:///C:/Users/grzes/Desktop/Muzyka.odb"
Db = Context.getByName(databaseURLOrRegisteredName )
Conn = Db.getConnection("","") 'username & password pair - HSQL default blank
oForm = oEvent.Source.Model.Parent
dCheckBox = oForm.getByName("CheckBox").getCurrentValue()
sAuthor = oForm.getByName("AuthorBox").getCurrentValue()
sTitle = oForm.getByName("TitleBox").getCurrentValue()
Stmt = Conn.createStatement()
strSQL = "UPDATE ""Songs"" SET ""Played"" = " + dCheckBox + " WHERE ""Title"" = '" _
+ sTitle + "' AND ""Author"" = '" + sAuthor + "'"
Stmt.executeUpdate(strSQL)
Conn.close()
End Sub
One more suggestion: Although it works to read from controls, the preferred way is to directly access the form's underlying row set instead. For example:
lAuthorCol = oForm.findColumn('Author')
sAuthor = oForm.getString(lAuthorCol)
来源:https://stackoverflow.com/questions/41909662/updating-database-table-with-macro-using-data-from-form