Expand my Community achievements bar.

Click button to populate data into ListBox

Avatar

Former Community Member
When I add Data List Box from Custom Library pallette in to my form. There are some predefined code in initialize event of the List Box which will populate its data.



When I adjust the code, set the connection and field information I want to populate and preview. It works fine. (Connect to SQL Server 2000 DB)



Then, I want to click some button to populate the data instead of doing in initialize event. I add the button and move the code from initialize event of the List Box to click event of the button. It doesn't work and throw exception "Operation failed." when open connection.



Why? Anybody knows, please helpppp...



Thanks
5 Replies

Avatar

Former Community Member
You can not just simply copy the code from the list box and paste it onto the button and expect it to work without making any changes.

I think you need to make one change by located the line where it said



this.addItem(oTextNode.value, oValueNode.value);



and change it to



ListBox.addItem(oTextNode.value, oValueNode.value);



where ListBox is the name of your list box.



Also, change it to "javascript" instead of formcalc.

Avatar

Former Community Member
Yeah, I know what you mean. I didn't metion that I did it. I changed the control name and I set default language of document to javascript for all event handlers.

I mean, even if I follow the same code as what it provides for initializing data to List Box. It doesn't work for button click...



Thanks

Avatar

Former Community Member
I tried to populate the drop down list with the button on click event with data from DB. It seems to work fine for me.

What is the name of your list box and what is the script that you put on the click event of the button?

Avatar

Former Community Member
Oh.. It works for you?<br />My list box's name is DataListBox. So, I use <br /><br />form1.DataListBox.clearItems();<br />form1.DataListBox.addItem(oTextNode.value, oValueNode.value);<br /><br />Here is my pdf file : http://www.neoland.net/ByMyHands0/Test.pdf<br />(I change to connect to NWind.mdb on my desktop, same result.)<br /><br />Thanks for your help.<br /><br />And here is my code on click event : <br /><br />/* This listbox object will populate two columns with data from a data connection.<br /><br /> sDataConnectionName - name of the data connection to get the data from. Note the data connection will appear in the Data View.<br /> sColHiddenValue - this is the hidden value column of the listbox. Specify the table column name used for populating.<br /> sColDisplayText - this is the display text column of the listbox. Specify the table column name used for populating.<br /><br /> These variables must be assigned for this script to run correctly. Replace <value> with the correct value.<br />*/ <br /><br />var sDataConnectionName = "con"; // example - var sDataConnectionName = "MyDataConnection";<br />var sColHiddenValue = "ProductID"; // example - var sColHiddenValue = "MyIndexValue";<br />var sColDisplayText = "ProductName"; // example - var sColDisplayText = "MyDescription"<br /><br />// Search for sourceSet node which matchs the DataConnection name<br />var nIndex = 0;<br />while(xfa.sourceSet.nodes.item(nIndex).name != sDataConnectionName)<br />{<br /> nIndex++;<br />}<br /><br />var oDB = xfa.sourceSet.nodes.item(nIndex);<br />oDB.open();<br />oDB.first();<br /><br />// Search node with the class name "command"<br />var nDBIndex = 0;<br />while(oDB.nodes.item(nDBIndex).className != "command")<br />{<br /> nDBIndex++;<br />}<br /><br />// Backup the original settings before assigning BOF and EOF to stay<br />var sBOFBackup = oDB.nodes.item(nDBIndex).query.recordSet.getAttribute("bofAction");<br />var sEOFBackup = oDB.nodes.item(nDBIndex).query.recordSet.getAttribute("eofAction");<br /><br />oDB.nodes.item(nDBIndex).query.recordSet.setAttribute("stayBOF", "bofAction");<br />oDB.nodes.item(nDBIndex).query.recordSet.setAttribute("stayEOF", "eofAction");<br /><br />// Clear the list<br />form1.DataListBox.clearItems();<br /><br />// Search for the record node with the matching Data Connection name<br />nIndex = 0;<br />while(xfa.record.nodes.item(nIndex).name != sDataConnectionName)<br />{<br /> nIndex++;<br />}<br />var oRecord = xfa.record.nodes.item(nIndex);<br /><br />// Find the value node<br />var oValueNode = null;<br />var oTextNode = null;<br />for(var nColIndex = 0; nColIndex < oRecord.nodes.length; nColIndex++)<br />{<br /> if(oRecord.nodes.item(nColIndex).name == sColHiddenValue)<br /> {<br /> oValueNode = oRecord.nodes.item(nColIndex);<br /> }<br /> else if(oRecord.nodes.item(nColIndex).name == sColDisplayText)<br /> {<br /> oTextNode = oRecord.nodes.item(nColIndex);<br /> }<br />}<br /><br />while(!oDB.isEOF())<br />{<br /> form1.DataListBox.addItem(oTextNode.value, oValueNode.value);<br /> oDB.next();<br />}<br /><br />// Restore the original settings<br />oDB.nodes.item(nDBIndex).query.recordSet.setAttribute(sBOFBackup, "bofAction");<br />oDB.nodes.item(nDBIndex).query.recordSet.setAttribute(sEOFBackup, "eofAction");<br /><br />// Close connection<br />oDB.close();

Avatar

Former Community Member
I downloaded your form and create a new DSN to the MS Access that you use in the example from this post


jimmypham, "Data Connection - Search for record" #1, 29 Aug 2005 1:47 pm



and only change the following and it seems to work fine for me:



var sDataConnectionName = "DataConnection";

var sColHiddenValue = "ID";

var sColDisplayText = "PART_NO";