Page 1 of 1

[Solved] SQL update in macro

Posted: Wed Dec 14, 2011 8:19 am
by bandoro
Hi there,

i've been around this problem for 3 days now
i need to know how to proper way to write sql update with in macro?

UPDATE tablename SET tablefield = variable1 WHERE tablefield = variable2

Variable1 and variable2 is a value from form.

i seen many examples in the forum but i've still know idea where to put quote (") mark and what is _& stand for ??

Thanks

Re: sql update in macro

Posted: Wed Dec 14, 2011 11:58 am
by RPG
Hello

Before you start with macros test the SQL in the SQL tool. Then you can be sure you have no more problems in the SQL code.

Romke

Re: sql update in macro

Posted: Wed Dec 14, 2011 12:35 pm
by bandoro
already done that and success but without variable
in sql tool i write : UPDATE "Tablename" SET "Column1"='String1' WHERE "Column2"='String2'

where String1 and String2 is a string value not a variable value from form

Re: sql update in macro

Posted: Wed Dec 14, 2011 2:13 pm
by RPG
Hello

The next step you must make a macro for starting up The SQL code.

What code you have now and how do you start the macro. What I now see does not look as the orginal code you test in the SQL line for some reason I do want see now the SQL code.

Romke

Re: sql update in macro

Posted: Wed Dec 14, 2011 4:11 pm
by rudolfo
Maybe you have a look at this thread Need help with another tricky query. The last post has an example how to work with an update after a click on a button.

Re: sql update in macro

Posted: Thu Dec 15, 2011 10:46 am
by bandoro
Finally I manage to write the code and work perfectly

Here is the code :

Code: Select all

 
Sub Pushbuttontest (oEvent as object)
	Dim oForm, oCon, oCoba, oID, oSql as object
	Dim oSqlstring as string
	oForm = oEvent.Source.Model.Parent
  	oCon = oForm.ActiveConnection
  	oCoba = oForm.getbyname("Testnew") 'Textbox where i write new value
  	oID = oForm.getbyname("IDbox") 'Textbox where i Display Current ID from the Table Tabletest (ID is integer with autovalue)
  	msgbox oID.currentvalue ' optional 
  	msgbox oCoba.text 'optional
  	oSql = oCon.CreateStatement()
  	oSql.executeupdate("UPDATE ""Tabletest"" SET ""Test"" = '"& oTestnew.text &"' WHERE  ""ID"" = '"& oID.text &"'")
End sub
Now i know how write SQL in macro :D

Thanks for all your support