Solved

How to execute MySql query and pass to VBscript Variable?

Posted on 2011-02-27
7
2,922 Views
Last Modified: 2013-12-25
Dear EE,

I'm trying to write a VBS script to execute a simple Mysql query, and dump it off into a variable.... and I'm stuck with the pieces of the puzzle and no means by which to put them together. Here's what I have:

The MySQL query:
SELECT DISTINCT `Column` FROM `Database`.`table` WHERE ID=27 AND ExtraFieldID=53

This query is written in context, so that no more than one cell will be the result. I've figured out how to make the ODBC connection to MYSQL with MyODBC 3.51, but I'm stuck regarding how to execute the query, then dump the resulting record (one cell, string) into a VBS variable. Here's where I'm at:

'***** CONFIG PARAMETERS *******
strServer = "server"
strUser = "username"
strPass = "password"
strDBName = "database name"
'****** END OF CONFIG **********
 
Set dbConn = CreateObject("ADODB.Connection")
dbConn.ConnectionString = "driver={MySQL};server=" & strServer & ";uid=" & strUser & ";pwd=" & strPassword & ";database=" & strDBName
dbConn.Open

So I think i've got the database to open, but I can't find a good example on how to execute the query, then set a variable to the contents of the one cell result. If anyone could point me in the right direction or pick up the ball where I left off, I'm all ears!

Thanks in advance EE!
0
Comment
Question by:Cdavis316
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34993586
Hi, basically, after you open the connection with
dbConn.Open

just execute a query, and return the results to a recordset.

Set objRecordset = dbConn.Execute("SELECT DISTINCT 'Column' FROM 'Database'.'table' WHERE ID=27 AND ExtraFieldID=53")
While Not objRecordset.EOF
   strValue = objRecordset.Fields("Column").Value
Wend
MsgBox strValue


Regards,

Rob.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34993593
Ooops, forgot MoveNext  - ENDLESS LOOP in the above code....DOH!

Rob.
Set objRecordset = dbConn.Execute("SELECT DISTINCT 'Column' FROM 'Database'.'table' WHERE ID=27 AND ExtraFieldID=53")
While Not objRecordset.EOF
   strValue = objRecordset.Fields("Column").Value
   objRecordset.MoveNext
Wend
MsgBox strValue

Open in new window

0
 

Author Closing Comment

by:Cdavis316
ID: 34993687
Thanks for the assist, I ended up with the following:
Set objRs = dbconn.Execute("SELECT DISTINCT `Value` FROM `labtech`.`extrafielddata` WHERE ID=27 AND ExtraFieldID=53")
strResult=objRs(0)
MsgBox strResult
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 65

Expert Comment

by:RobSampson
ID: 34993732
Sure, that works too, but you will get an error if you don't check for EOF.
0
 

Author Comment

by:Cdavis316
ID: 34993807
Why? For my own education, I'm definitely no expert...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34993825
When you execute a query, it returns a recordset object, but if no records were found, then any attempt to access any data from that recordset will result in an error, stating that the recordset is already at End Of File (EOF).

Therefore, to overcome that potential error, you just need to check for it:
If Not objRS.EOF Then
   strResult=objRs(0)
   MsgBox strResult
Else
   MsgBox "No records found"
End If
0
 

Author Comment

by:Cdavis316
ID: 34993959
Thanks for the tip!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now