使用来自表单的数据通过宏更新数据库 table
Updating database table with macro using data from form
我在 Libreoffice Base 中有一个表格,它连接到 "Songs" table(基本上是音乐数据库),我想做的是每次我 check/uncheck 上的 CheckBox该表格我希望每条记录的字段 "Played" 与我当前在表格上的记录具有相同的名称和作者,并且也是 checked/unchecked。我读过这样做的唯一方法是使用宏(因为我不想使用关系因为我现在有很多记录)。我写过这样一个宏:
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 是选中设置为 1 未选中设置为 0 的复选框)但是执行宏时没有任何反应(绑定为复选框本身的鼠标按钮按下事件)
我确定执行 SQL 查询的方式与在另一个宏中一样正确,我也使用它没有任何问题所以问题必须是设置变量 dcheckbox、dauthorbox 和 dtitlebox 或 strSQL。 (宏本身是 运行,因为当我更改控件名称时出现错误)。所以问题是:它有什么问题?..
在此先感谢您的帮助。
没有发生任何事情的原因是变量 dTitle
和 dAuthor
是空的。请注意变量名称不匹配。因此更新标题和作者为空的位置会影响 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)
我在 Libreoffice Base 中有一个表格,它连接到 "Songs" table(基本上是音乐数据库),我想做的是每次我 check/uncheck 上的 CheckBox该表格我希望每条记录的字段 "Played" 与我当前在表格上的记录具有相同的名称和作者,并且也是 checked/unchecked。我读过这样做的唯一方法是使用宏(因为我不想使用关系因为我现在有很多记录)。我写过这样一个宏:
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 是选中设置为 1 未选中设置为 0 的复选框)但是执行宏时没有任何反应(绑定为复选框本身的鼠标按钮按下事件)
我确定执行 SQL 查询的方式与在另一个宏中一样正确,我也使用它没有任何问题所以问题必须是设置变量 dcheckbox、dauthorbox 和 dtitlebox 或 strSQL。 (宏本身是 运行,因为当我更改控件名称时出现错误)。所以问题是:它有什么问题?..
在此先感谢您的帮助。
没有发生任何事情的原因是变量 dTitle
和 dAuthor
是空的。请注意变量名称不匹配。因此更新标题和作者为空的位置会影响 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)