[Javascript] While statement array losing data

Nick Fitzsimons nick at nickfitz.co.uk
Thu May 17 08:16:12 CDT 2007


On 17 May 2007, at 13:21:51, Tim Lewis wrote:

> This list is great!  I come from a VB background, and learning JS.   
> I have
> a routine in which I load data through DAO into an array.  My  
> problem is
> that as soon as the program leaves my While statement, the data in the
> array disappears.  I have studied and studied my code, but I cannot  
> find a
> reason.  I am very thankful for any help on this.
>
> Tim

Hi Tim,

I think the problem is that you are storing  a _reference_ to the  
field containing the result into the array; what you need is to store  
the _value_. IIRC, VB will automatically use the default property,  
Value in this case, of a COM object (which is what DAO objects are),  
but JScript does not do this. As you have stored a reference to the  
field, it will break when the field it refers to goes away as you  
iterate over the RecordSet.

It's a while since I've done any COM work, but I think what you need  
is to change

         sTransCount = SEARCHRESULT("TransactionCount");

to

         sTransCount = SEARCHRESULT("TransactionCount").Value;

and it should be OK.

Note that setting things to Nothing will cause an error, as JScript  
does not have that keyword. Also, you must have the brackets to  
invoke a function; you can't leave them off as in VB. Again, it's  
been a while, but I think that if you replace

objRecordSet.Close;
objRecordSet = Nothing;
objConnection = Nothing;

with

objRecordSet.Close();
delete objRecordSet;
delete objConnection;

then it should work.

Finally, you define your results array in the function:

var sLineIn = new Array();

This means it is local to the function, so the results array itself  
will disappear when the function ends. If you need to keep this for  
processing elsewhere, you have two options: you can move that line  
outside of the function, making it a global variable, or you can  
return the array from the function:

function blah() {
    var results = [ ]; // this is a better way than "new Array()"
    // do stuff to put things into the "results" array
    return results;
}

and then assign the result of the function to another array:

var firstResults = blah();
doSomethingWith(firstResults);
var secondResults = blah();
doSomethingWith(secondResults);

HTH,

Nick.


> My code is:
> <html><head>
> <script language="JavaScript">
> <!--
> function Main() {
> var sDB="C:\\Temp\\LOGDB.MDB";
> var objConnection = new ActiveXObject("DAO.DBEngine.36");
> var objRecordSet = objConnection.workspaces(0).OpenDatabase(sDB);
> var iResultCount=0;iCount=0;
> var sLineIn = new Array();
> var sTransCount="";
> var SEARCHRESULT;
> sSearchCommand = "SELECT * FROM HL7_Segments WHERE SegmentLineData  
> LIKE
> '*MSH*'";
> SEARCHRESULT = objRecordSet.OpenRecordset(sSearchCommand)
> SEARCHRESULT.MoveFirst();
> while (! SEARCHRESULT.EOF) {
>         sTransCount = SEARCHRESULT("TransactionCount");
>         sLineIn[iResultCount] = sTransCount;
>         var msg=confirm("iResultLine for "+iResultCount+" is
> "+sLineIn[iResultCount]); if (msg==false) {return;};
>         iResultCount++;
>         SEARCHRESULT.MoveNext(); }
> for (var iCount=0;iCount<iResultCount;iCount++) {
> var msg=confirm("Final iResultLine for "+iCount+" is "+sLineIn 
> [iCount]);
> if (msg==false) {return;}; }
> objRecordSet.Close;
> objRecordSet = Nothing;
> objConnection = Nothing;
> }
> -->
> </script>
> </head><body><form>
> <input type="button" value="Click me!" onclick="Main()">
> </form></body></html>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript

-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/






More information about the Javascript mailing list