A simple macro with “debug mode” to be called from a push button on a form.
- Write one or more SQL statements, separated by semicolon, into a text editor.
- Create a push button and copy the statement(s) to the “Additional info” property of your push button.
- Assign the execute event to the following macro.
- Set
Const cMsgbox = True
in order to show a message box that allows you to cancel the SQL execution. If you are sure that you want to execute silently, set Const cMsgbox = False
After executing the SQL statement(s), the macro reloads the form it is attached to.
Sub RunSQLButton(e)
Const cMsgbox = True
Const cMaxLen = 1000
Const cTitle = "Command "
oModel = e.Source.Model
frm = oModel.getParent()
oCon = frm.ActiveConnection
aTags() = split(oModel.Tag, ";")
n = uBound(aTags)
for i = 0 to n
s = trim(aTags(i))
sMsg = s
if len(s) > cMaxLen then sMsg = Left(s, cMaxLen) & Chr(10) &" [...]"
if len(s)>0 and cMsgbox then
x = Msgbox(sMsg, 33, cTitle & i +1 &"/"& n +1 )
else
x = 1
end if
if x = 1 then
oStmt = oCon.prepareStatement(s)
on error goto errMsg
r = oStmt.executeUpdate()
if cMsgbox then Msgbox r &" records affected", 64, cTitle
end if
next
frm.reload()
exit sub
errMsg:
error(err)
End Sub