{"id":28,"date":"2010-03-11T20:20:42","date_gmt":"2010-03-12T01:20:42","guid":{"rendered":"http:\/\/anvil-of-time.com\/2010\/03\/11\/powerbuilder-populate-a-dropdownlistbox-from-a-sql-statement\/"},"modified":"2021-03-08T18:46:53","modified_gmt":"2021-03-08T23:46:53","slug":"powerbuilder-populate-a-dropdownlistbox-from-a-sql-statement","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-populate-a-dropdownlistbox-from-a-sql-statement\/","title":{"rendered":"PowerBuilder &#8211; Populate a dropdownlistbox from a SQL statement"},"content":{"rendered":"<p>Here is a method to populate a dropdownlistbox control with values from a sql statement.\u00a0 Since it is based on a datastore you can easily apply filters, sorts, etc. prior to populating the dropdownlistbox with values.\u00a0 The code assumes the first column in the SQL is going to be used to populate the dropdownlistbox .<\/p>\n<p>When you call the function you send the dropdownlistbox and the sql string.<\/p>\n<pre name=\"code\" class=\"VB\">public function integer uf_populate_ddlb (dropdownlistbox ddlb_param, string sql_param);\n\/\/ populate a listbox with results from a sql statement\n\/\/ returns number of rows from datastore retrieve\nlong ll_ddlb_length\nlong\u00a0\u00a0\u00a0 ll_rc\nlong\u00a0\u00a0\u00a0 ll_i\nstring ls_ds_sql\nstring ls_error\nstring\u00a0\u00a0\u00a0 ls_val\ndatastore lds\nlds = CREATE datastore\nls_ds_sql = SQLCA.syntaxfromsql( sql_param, &#039;Style(Type=Form)&#039;, ls_error)\nIF (Len(ls_error) &gt; 0 ) THEN\n   MessageBox (&quot;DataBase Error!&quot;, ls_error)\n   return sqlca.sqlcode\nELSE\n   lds.create(ls_ds_sql, ls_error)\n   IF (Len(ls_error) &gt; 0 ) THEN\n      MessageBox (&quot;Datastore create Error!&quot;, ls_error)\n      return -1\n   END IF\n   lds.settransobject(SQLCA)\n   ll_rc = lds.retrieve()\n\/\/now if you need to filter or change the rows in the datastore do it here - just remember to update ll_rc (the rowcount) if you changed it.\nEND IF\nSetRedraw (ddlb_param, false)\nReset (ddlb_param)\nFOR ll_i = 1 to ll_rc\n   \/\/ if column 1 in sql is not a string you would need to convert it\n   ls_val = lds.getitemstring(ll_i,1)\n   Additem (ddlb_param, ls_val)\nNEXT\nSetRedraw (ddlb_param, true)\n\/************************************************************\n*\u00a0\u00a0\u00a0 size of ddlb is calculated to show the count of the items.\n*\u00a0\u00a0\u00a0 note:\u00a0 117 is the space to show ddlb.text\n************************************************************\/\nif ll_rc &lt; 12 then\n   ll_ddlb_length = ll_rc * 63 + 117\n   ddlb_param.height = ll_ddlb_length\nelse\n   \/\/ size will be set to show 12 items if count is over 12\n   ddlb_param.height = 873\nend if\nDESTROY lds\nreturn ll_rc\n\nend function<\/pre>\n\n\n<p>Updated March 2021<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a method to populate a dropdownlistbox control with values from a sql statement.\u00a0 Since it is based on a datastore you can easily apply filters, sorts, etc. prior to populating the dropdownlistbox with values.\u00a0 The code assumes the first column in the SQL is going to be used to populate the dropdownlistbox .&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[12,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/28"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":10,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":1727,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/28\/revisions\/1727"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}