使用表单中的数据使用宏更新数据库表

时间:2017-01-28 12:13:37

标签: libreoffice-base libreoffice-basic

我在Libreoffice Base中有一个表单连接到“Songs”表(音乐的基本数据库),我想要做的是每次我检查/取消选中该表单上的CheckBox我想要“播放”字段每个记录具有相同的名称和作者,也就是我当前在表单上检查/取消选中的记录。我已经读过,这样做的唯一方法是使用宏(因为我不想使用关系,因为我现在需要很多记录)。我写过这样一个宏:

    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和TitleBox是文本框,CheckBox是CheckBox,检查设置为1,未选中为0)但宏执行时没有任何反应(绑定为鼠标按钮事件到复选框本身)
我确信执行SQL查询的方式是正确的,因为在另一个宏中我也使用它没有任何问题所以问题必须是设置变量dcheckbox,dauthorbox和dtitlebox或者使用strSQL。 (宏本身正在运行,因为当我更改控件名称时出现错误)。所以问题是:它有什么问题?..

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

没有发生任何事情的原因是变量dTitledAuthor为空。请注意,变量名称不匹配。因此,更新标题和作者为空的位置会影响0行。

这是工作代码:

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

还有一个建议:虽然它可以从控件中读取,但首选方法是直接访问表单的基础row set。例如:

lAuthorCol = oForm.findColumn('Author')
sAuthor = oForm.getString(lAuthorCol)
相关问题