Written by

Software Engineer at InterSystems
Question Robbie Luman · Jun 13, 2017

Is it possible to execute a custom class query using MS Excel VBA?

Is it possible using MS Excel VBA to execute and retrieve data from a Cache custom class query? If so, how?

Comments

Eduard Lebedyuk · Jun 13, 2017

Excel can import data from any ODBC data source.

0
Robbie Luman  Jun 14, 2017 to Vitaliy Serdtsev

I ended up changing over to use the CacheActiveX method for connecting and running the query which works really well. I do have a question, though: Is there any way to use a defined ODBC DSN in the connection string using CacheActiveX?

0
Robbie Luman  Jun 14, 2017 to Vitaliy Serdtsev

Thanks! I figured out my issue, I had set up my ODBC connection incorrectly and was referencing the wrong namespace. Thank you for the assistance!

0
Vitaliy Serdtsev · Jun 14, 2017

If you know how to work with ActiveX from MS Excel VBA, then there is no problem.

E.g. (demo.vbs):
Set f = CreateObject("CacheActiveX.Factory")
Set rs = CreateObject("CacheActiveX.ResultSet")
If Not f.IsConnected() Then

  </FONT><FONT COLOR="#000000">f.</FONT><FONT COLOR="#0000ff">Connect</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"cn_iptcp:127.0.0.1[1972]:SAMPLES:_SYSTEM:SYS"</FONT><FONT COLOR="#000000">)

  </FONT><FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#000000">rs=f.</FONT><FONT COLOR="#0000ff">DynamicSQL</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"select TOP 3 * from Sample.Person"</FONT><FONT COLOR="#000000">)   rs.</FONT><FONT COLOR="#0000ff">Execute</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">while </FONT><FONT COLOR="#000000">rs.</FONT><FONT COLOR="#0000ff">Next     </FONT><FONT COLOR="#000000">WScript.</FONT><FONT COLOR="#0000ff">Echo</FONT><FONT COLOR="#000000"> rs.</FONT><FONT COLOR="#0000ff">Get</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"SSN"</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#008000">'print of field SSN for first three rows from the table Sample.Person   </FONT><FONT COLOR="#0000ff">wend

  </FONT><FONT COLOR="#000000">rs.</FONT><FONT COLOR="#0000ff">Close</FONT><FONT COLOR="#000000">()   </FONT><FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#000000">person = f.</FONT><FONT COLOR="#0000ff">Static</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"Sample.Person"</FONT><FONT COLOR="#000000">)   age=person.</FONT><FONT COLOR="#0000ff">CurrentAge</FONT><FONT COLOR="#000000">(45678) </FONT><FONT COLOR="#008000">'call of method of class Sample.Person

  </FONT><FONT COLOR="#000000">WScript.</FONT><FONT COLOR="#0000ff">Echo</FONT><FONT COLOR="#000000"> age </FONT><FONT COLOR="#0000ff">End If</FONT>

Running a Query in Visual Basic

0
Robbie Luman · Jun 14, 2017

Thank you both for your responses, I greatly appreciate the help!
I tried going down the ODBC route using ADO based on some other information I have found and I seem to be getting hung up by something not getting specified in the call to Cache through ODBC. Here's what I have in the Excel VBA:

When I try to run this, I get this error:

The Query "GETSQLLBIMA7" exists in the USER namespace under the USER package. I'm not sure how to specify to look under USER rather than SQLUSER when calling Cache.

0
Robbie Luman · Jun 14, 2017

Thank you for the responses so far, I greatly appreciate them!
I found some information online about using ADO and ODBC to make the connection which has gotten me close. Here is what I have in Excel VBA:

and here is the error I get when I run this code:

The "GETSQLLBIMA7" query exists in the USER namespace under the USER package. I'm not sure where "SQLUSER" is coming from or how to specify a different namespace or package.

0