Solved

How to execute MySql query and pass to VBscript Variable?

Posted on 2011-02-27
7
2,963 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

816 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

9 Experts available now in Live!

Get 1:1 Help Now