Solved

How to execute MySql query and pass to VBscript Variable?

Posted on 2011-02-27
7
2,850 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

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

Author Comment

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

Expert Comment

by:RobSampson
Comment Utility
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
Comment Utility
Thanks for the tip!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

10 Experts available now in Live!

Get 1:1 Help Now