Interfacing with Access object using WSC written in JScript 
Author Message
 Interfacing with Access object using WSC written in JScript

Hallo,

I am now testing out how to make windows scripting components. I have a
question regarding interfacing with Access. There seems no way to pass an
object reference to a WSC method written in JScript. Here is my VB code I
use for testing in my Access database:

Sub myTest()
    Dim t As Object, M As Object

    Set t = CreateObject("myLib.tryout")

    Set M = CurrentDb()

    a = t.meth1(M)

    Debug.Print a

End Sub

And here is the JScript code in component:

function meth1(M)
{
 var aa = [];
 for (var itm in M) aa.push(itm + '=' + M[itm]);
 return typeof(M) + '>'+aa.length+'>' + aa.join('; ');

Quote:
}

My VB code returns this output:

object>0>

So, JScript doesn't see any of properties or methods of the passed object
referece, although it considers it an 'object'.

Please, if someone could shed some light in here...

Thanks,

-- Pavils



Mon, 04 Apr 2005 22:57:07 GMT  
 Interfacing with Access object using WSC written in JScript
for (var itm in M) ...

works with JavaScript objects and arrays, not COM objects.

There is no way to enumerate the properties of a COM object.  

If the object is a collection, you would use an Enumerator() object to walk the items of the collection.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Tue, 05 Apr 2005 00:53:56 GMT  
 Interfacing with Access object using WSC written in JScript
Ok, I see,

But how I can read at least name property of the passed reference of
database object? How can I set some property of this object? Can I call its
methods?

-- Pavils



for (var itm in M) ...

works with JavaScript objects and arrays, not COM objects.

There is no way to enumerate the properties of a COM object.

If the object is a collection, you would use an Enumerator() object to walk
the items of the collection.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Tue, 05 Apr 2005 14:36:53 GMT  
 Interfacing with Access object using WSC written in JScript
I remove my question; I just discovered that I have to include reference to
"Microsoft DAO 3.6 Object Library" for WSC to be able to access database
object properties and methods.

However, some gliches occour: WSC doesn't seem to be able to hold the passed
reference in private variable:

This code works:

// WSC
function meth1(db, qryName, qrySQL) {
  db.CreateQueryDef(qryName, qrySQL);
  return true;

Quote:
}

//Access VB
Set t = CreateObject("PavilsLib.tryout")
Debug.print t.meth1(CurrentDb(), "ABC", "SELECT * FROM TestData")

But this code doesn't work:

// WSC
var currentDataSource = null;
function setDataSource(dataSource) {
 currentDataSource = dataSource;

Quote:
}

function meth1(qryName, qrySQL) {
  if (currentDataSource == null) return;
  currentDataSource.CreateQueryDef(qryName, qrySQL);
  return true;

Quote:
}

//Access VB
Set t = CreateObject("PavilsLib.tryout")
t.setDataSource (CurrentDb())
Debug.print t.meth1("ABC", "SELECT * FROM TestData")

Also, I have one more question: is it possible for method to return a VB
array? A date object? My tests show that this code

function meth1(A, B) {
 return [A, B];

Quote:
}

returns to Viual Basic a string, actually a result of Array.toString().
Would that be somehow possible to return a VB array? The same goes for Date
object:

function meth1() {
 return new Date();

Quote:
}

Returns a string.

Of course, I can make some workarouds and pass an array concatenated with
some control character, and then split it in the VB, using VB Split
function, or use cDate to convert a date string to Date object. However, it
would be clearer to be able to pass the arrays and dates so that VB can use
them right away.

Thanks for help,

-- Pavils


Quote:
> Ok, I see,

> But how I can read at least name property of the passed reference of
> database object? How can I set some property of this object? Can I call
its
> methods?

> -- Pavils



> for (var itm in M) ...

> works with JavaScript objects and arrays, not COM objects.

> There is no way to enumerate the properties of a COM object.

> If the object is a collection, you would use an Enumerator() object to
walk
> the items of the collection.

> --
> Michael Harris
> Microsoft.MVP.Scripting
> Seattle WA US



Tue, 05 Apr 2005 15:27:35 GMT  
 Interfacing with Access object using WSC written in JScript


Quote:

> //Access VB
> Set t = CreateObject("PavilsLib.tryout")
> t.setDataSource (CurrentDb())
> Debug.print t.meth1("ABC", "SELECT * FROM TestData")

The CurrentDB() function is a funny thing in Access, and what it returns
seems to change with every version. If what you really need is a pointer to
the actual database, it might be worth trying something like

  t.setDataSource DBEngine.Workspaces(0).Databases(0)

which is the explicit version of the same thing.

HTH

Tim F



Wed, 06 Apr 2005 02:42:02 GMT  
 Interfacing with Access object using WSC written in JScript
Thanks Tim,

tha't s true. It works now! I just happened to start testing with a wrong
object, heh. Do you have ideas about returning a VB array or VB Date or,
perhaps, VB Object from a method coded in JScript WSC?

Thanks,

-- Pavils Jurjans


Quote:


> > //Access VB
> > Set t = CreateObject("PavilsLib.tryout")
> > t.setDataSource (CurrentDb())
> > Debug.print t.meth1("ABC", "SELECT * FROM TestData")

> The CurrentDB() function is a funny thing in Access, and what it returns
> seems to change with every version. If what you really need is a pointer
to
> the actual database, it might be worth trying something like

>   t.setDataSource DBEngine.Workspaces(0).Databases(0)

> which is the explicit version of the same thing.

> HTH

> Tim F



Fri, 08 Apr 2005 16:13:59 GMT  
 Interfacing with Access object using WSC written in JScript

Quote:
> ... Do you have ideas about returning a VB array

Create a Scripting.Dictionary and load it with items and return the dictionary's .Items()...

Quote:
> ... or VB Date

A JScript Date() has a .getVarDate() method that does this...

Quote:
> ... or, perhaps, VB Object from a method coded in JScript WSC?

Not sure what you mean by this one...

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Fri, 08 Apr 2005 22:38:43 GMT  
 Interfacing with Access object using WSC written in JScript
Hallo Michael, thanks for these, they work!

Quote:
> Create a Scripting.Dictionary and load it with items and return the

dictionary's .Items()...

Quote:
> A JScript Date() has a .getVarDate() method that does this...
> ... or, perhaps, VB Object from a method coded in JScript WSC?

> Not sure what you mean by this one...

I meant something like JScript object, but I see there is no such thing in
VBScript... Maybe that would be VB Collection object, I must see how I can
return this one from JScript method...

Meanwhile, I am looking for some way how to call JSCript method in WSC using
named arguments syntax. In JScript, it's very convinient to pass an object
with different properties as a parameter to function. So, I am looking for
something to imitate that.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Sat, 09 Apr 2005 00:24:02 GMT  
 Interfacing with Access object using WSC written in JScript

Quote:
> I meant something like JScript object, but I see there is no such
> thing in VBScript... Maybe that would be VB Collection object, I must
> see how I can return this one from JScript method...

Still not clear to me...  A JScript function (as a WSC public method) can return a JScript object to a non-JScript client.

VBScript has support (since v5.0) for Class...End Class to create VBScri[pt based classes.

But I don't understand what kind of object you want to return from the JScript function and if you want to do that via an argument or the function's return valeu...

Quote:
> Meanwhile, I am looking for some way how to call JSCript method in
> WSC using named arguments syntax. In JScript, it's very convinient to
> pass an object with different properties as a parameter to function.
> So, I am looking for something to imitate that.

Define what you mean by "named arguments"...  

Neither VBScript nor JScript support "named arguments", at least not in the VB/VBA sense where you can specify ParamName:="paramValue" as an argument to a COM method.

Both VBScript and JScript let you define a procedure (function/sub) with a formal parameter list.  In fact, VBScript requires it but JScript does not since JScript has the built-in arguments object within every function.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Sat, 09 Apr 2005 00:59:17 GMT  
 Interfacing with Access object using WSC written in JScript
My testing environment is Access 2000 with VBA (I am not sure what version
of VBA corresponds to what version of VB), and could not find any reference
there on how to make Class in it.

What I was actually looking for, was how to read simple JScript object:

Code in WSC:

function meth1(A) {
    return {a:1, b:2, c:}

Quote:
}

Code in Access VBA:

Sub myTest()
    Dim t As Object, a

    Set t = CreateObject("PavilsLib.tryout")

    a = t.meth1 ()
    Debug.Print TypeName(a)
    If InStr(TypeName(a), "()") > 0 Then
        Debug.Print "LBound=" & LBound(a)
        Debug.Print "UBound=" & UBound(a)
        Debug.Print "Contents=[" & Join(a, ", ") & "]"
    Else
        Debug.Print a
    End If

End Sub

This code returns "String" and "[object Object]". I would like to have a way
to get the contents of returned object properly. Something like:

Sub myTest()
    Dim t As Object, a

    Set t = CreateObject("PavilsLib.tryout")

    a = t.meth1 ()

    Debug.Print a.a
    Debug.Print a.b
    Debug.Print a.c

End Sub

Quote:
> Define what you mean by "named arguments"...

> Neither VBScript nor JScript support "named arguments", at least not in

the VB/VBA sense where you can > specify ParamName:="paramValue" as an
argument to a COM method.

That's right. Because I am testing from Access VBA. I want to know wether it
is possible to set up JScript WSC where method would take named arguments.

Buy the way, is it possible for JScript code in WSC to know from what
language platform it is called? I mean, all this thread is almost
unnecessary, if this component is called from JScript.

Thanks for your time,

-- Pavils Jurjans



Sat, 09 Apr 2005 14:56:10 GMT  
 Interfacing with Access object using WSC written in JScript

Quote:

> My testing environment is Access 2000 with VBA (I am not sure what
> version of VBA corresponds to what version of VB), and could not find
> any reference there on how to make Class in it.

In VBA (and full VB) classes are created as separate *.cls files.  VBScript (and now VB.NET) supports Class..End Class blocks to define classes.

Quote:
> What I was actually looking for, was how to read simple JScript
> object:

> Code in WSC:

> function meth1(A) {
>     return {a:1, b:2, c:}
> }

> Code in Access VBA:

> Sub myTest()
>     Dim t As Object, a

>     Set t = CreateObject("PavilsLib.tryout")

>     a = t.meth1 ()
>     Debug.Print TypeName(a)
>     If InStr(TypeName(a), "()") > 0 Then
>         Debug.Print "LBound=" & LBound(a)
>         Debug.Print "UBound=" & UBound(a)
>         Debug.Print "Contents=[" & Join(a, ", ") & "]"
>     Else
>         Debug.Print a
>     End If

> End Sub

> This code returns "String" and "[object Object]".

You can't use LBound() or UBound() or Join() or any other VB/VBA/VBScript array function on a returned JScript array.  These work on COM SAFEARRAYs (i.e., VB/VBA/VBScript style arrays).  A JScript array is not a COM SAFEARRAY - it's just a special case JScript object.

In your example above, the JScript meth1(A) function returns a simple JScript object (??? you don't actually use the A argument - you called it from your sample VBA code with no argument which does work even though one wouldn't expect it to ;-).

I don't get "String" from TypeName(a) (at least not from a VBScript client) - I get "JScriptTypeInfo".  "[object Object]" is the same thing you would get from a JScript client doing a WScript.echo(a) on a JScript object.

- Show quoted text -

Quote:
> ...I would like to
> have a way to get the contents of returned object properly. Something
> like:

> Sub myTest()
>     Dim t As Object, a

>     Set t = CreateObject("PavilsLib.tryout")

>     a = t.meth1 ()

>     Debug.Print a.a
>     Debug.Print a.b
>     Debug.Print a.c

> End Sub

Given that meth1() returns an object with .a, .b, and .c properties, that's valid code.

Quote:
>> Define what you mean by "named arguments"...

>> Neither VBScript nor JScript support "named arguments", at least not
>> in
> the VB/VBA sense where you can > specify ParamName:="paramValue" as an
> argument to a COM method.

> That's right. Because I am testing from Access VBA. I want to know
> wether it is possible to set up JScript WSC where method would take
> named arguments.

Nope - the ActiveX script languages don't support defining procedures with support for named arguments.  Nor can you use named arguments in calls to other COM methods that do otherwise support them.

Quote:
> Buy the way, is it possible for JScript code in WSC to know from what
> language platform it is called? I mean, all this thread is almost
> unnecessary, if this component is called from JScript.

There is no way for a WSC to know anything about the client language from which it is used.  You shouldn't design components that have any client language dependencies.

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Sat, 09 Apr 2005 23:36:41 GMT  
 Interfacing with Access object using WSC written in JScript
Ok, Michael,

thanks for all your tips. I feel I'd like to account for what are my final
results. I think I am now satiated and can go further. Regarding your
comment on writing platform-independent components, it would be foolish to
disaglee, however, returning an array, or collection, or normal date object
from component method can be very valuable thing. The code below helps to
write JScript components that *do* return proper variable type for Visual
Basic environments.

Thanks for all your help,

Regards,

-- Pavils Jurjans

****************************************************************************
*****************************

my VBA code in Access 2000:

Sub myTest(dataType)
    Dim t As Object, a

    Set t = CreateObject("PavilsLib.tryout")

    t.callerScript = "VBA"

    a = t.returnVar(dataType)  ' "boolean" "long", "double", "array",
"date", "object" <-- don't forget using "set"

    Debug.Print "Variable type: " & TypeName(a)
    If InStr(TypeName(a), "()") > 0 Then
        Debug.Print "LBound=" & LBound(a)
        Debug.Print "UBound=" & UBound(a)
        Debug.Print "Contents=[" & Join(a, ", ") & "]"
    Else
        Select Case TypeName(a)
        Case "Dictionary"
            For Each itm In a
                Debug.Print "Item: " & TypeName(a(itm)) & ", " & itm & "=" &
a(itm)
            Next
        Case Else
            Debug.Print "Value: " & a
        End Select
    End If

End Sub

WSC code:

<?xml version="1.0"?>
<component>

<?component error="true" debug="true"?>

<registration
 description="JScript component with code samples for VisualBasic-specific
variable types"
 progid="PavilsLib.tryout"
 version="1.00"
 classid="{d0c57f3d-19bd-4bd8-8a51-aa35c6830de7}"
</registration>

<public>
 <property name="callerScript">
  <get/>
  <put/>
 </property>
 <method name="returnVar">
  <PARAMETER name="dataType"/>
 </method>
</public>

<script language="JScript">
<![CDATA[

var callerScript = "JScript";

function get_callerScript() {
 return callerScript;

Quote:
}

function put_callerScript(newValue) {
 callerScript = newValue;

Quote:
}

function getType(variable) {
 if (variable==null) return "null";
 return typeof(variable)=='object' ?
'object.'+variable.constructor.toString().match(/\w+/g)[1] :
typeof(variable);

Quote:
}

function transferVar(input) {
 switch (callerScript) {
 case "JScript":
 case "JavaScript":
  return input;
 case "VBScript":
 case "VisualBasic":
 case "VB":
 case "VBA":
  switch (getType(input)) {
  case "boolean":
  case "object.Boolean":
  case "number":
  case "object.Number":
  case "string":
  case "object.String":
   return input;
  case "object.Array":
   var dict = new ActiveXObject("Scripting.Dictionary");
   for (var i = 0; i < input.length; i++) dict.add(i, input[i]);
   return dict.items();
  case "object.Date":
   return input.getVarDate();
  case "object.Object":
   var dict = new ActiveXObject("Scripting.Dictionary");
   for (var i in input) dict.add(i, input[i]);
   return dict;
  default:
   return "Unable to convert variable type";
  }
 default:
  return "Unsupported script interface";
 }

Quote:
}

function returnVar(dataType) {
 // Return different types of VB variable-type data
 switch (dataType) {
 case "boolean":
  return transferVar(true);
 case "long":
  return transferVar(1);
 case "double":
  return transferVar(1.23);
 case "string":
  return transferVar("ABCDEFG");
 case "array":
  return transferVar([1, 2.34, "ABC", true]); // Use only basic variable
types for array elements
 case "date":
  return transferVar(new Date);
 case "object":
  return transferVar({a:1, b:2.34, c:"ABC"});
 default:
  return "Unknown variable type";
 }

Quote:
}

]]>
</script>

</component>

****************************************************************************
*****************************



Sun, 10 Apr 2005 15:16:27 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Accessing COM objects and interfaces using JScript.

2. WSC - JScript - COM+ - Unable to access Error object after SetAbort

3. WSC - JScript - COM+ - Unable to access Error object after SetAbort

4. Using a WSC to access Jscript functions from VBScript

5. Can we access an object using Jscript?

6. using jscript to access an object outside an iframe

7. Using WScript object in WSC components

8. Using Wscript Objects in WSC

9. Using WSCRIPT object in a WSC

10. Access on a MS-Access-database with an ADO-Object in Jscript

11. Using secondary COM interfaces from jscript

12. Using secondary COM interfaces from jscript

 

 
Powered by phpBB® Forum Software