MySQL query via an HTA Application - mysql

I am running into a weird situation, hoping someone here can help.
I am communicating to a MySQL database using 'MySQL ODBC 5.1 Driver', although the below code works fine when run separately on a .vbs file, when i put the same in HTA i get Error
Error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I put the same code in a .vbs file and run the VBS file via an HTA using objShell.run command still the same problem.
When the VBS file is run independently or via CMD it runs fine displaying the contents. Any ideas ?
Call Query
Sub Query
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
adOpenStatic = 3
adLockOptimistic = 3
objConnection.Open _
"Driver={MySQL ODBC 5.1 Driver};Server=[Some Address];Database=bldb;User=usr;Password=pass;"
objRecordSet.Open "SELECT * FROM Clients Where Machine Like 00000" & , _
objConnection, adOpenStatic, adLockOptimistic
Do While Not objRecordSet.EOF
Company = objRecordSet (1)
Contact = objRecordSet (2)
Phone = objRecordSet (3)
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
Set objRecordSet=Nothing
Set objConnection=Nothing
End Sub

I was able to figure this on, the problem was the 32/64 Bit nature of HTA and the ODBC Driver.
I had installed a 64Bit ODBC driver and my HTA was running as a 32Bit Application.
Either run %Windrir%\System32\mshta.exe or install the the 32Bit Driver.
I chose to install the 32Bit variant of the MySQL ODBC Driver which resolved the problem.

Related

MariaDB 2.0.13 vs MySQL 5.3 ODBC Drivers (VBA Connection)

In Excel-VBA We used to have a sql database connection using MySQL drivers. Since this wasn't working for everyone we decided to install MariaDB drivers at every pc and use these drivers. The speed of opening an connection however, has decreased dramatically. Here is the code of the new and old vba script.
p_dbConn.ConnectionString = _
"DRIVER={MariaDB ODBC 2.0 Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
p_dbConn.ConnectionString = _
"DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
I ran the script in debug mode and the .open statement takes way longer with the mariadb driver. Anyone has any idea why?
Thanks!
I know it's not the driver that you are using ... but I use the following connection in my VBA code to connect to SQL Servers:
Reference File: Microsoft DAO 3.6 Object Library
'*************************************
'* SQL Server database connection *
'*************************************
Dim db As ADODB.Connection
Set db = New ADODB.Connection
db.Open "Provider=sqloledb; Data Source = 192.168.0.10; Database = [your DB Name];User Id = xxxxxx; Password = xxxxxxxx"
if a command takes longer than 45 seconds to execute ... use this to extend the execution default time
db.CommandTimeout = 1200 '(1200/60 = 20 minutes)
Sample Simple Commands
Set rst = db.execute("Select * from [your table name]")
db.execute("Delete from [your table name] where [your criteria]")

How to read .csv file with VBScript and ADO

The Goal
Write a program that reads .csv files and uploads them to a database. It must run on Windows 10 with no special installations necessary.
What I've Tried
I'm familiar with VBScript and ADO connections (I've actually already written and tested the file upload portion of the program). Now all I'm trying to do is read in the .csv file with ADO (see why to use ADO instead of split() here: https://msdn.microsoft.com/en-us/library/ms974559.aspx)
The article above creates the .csv connection using the Microsoft Jet Database Engine, which is not installed by default, so I tried with Microsoft Text Driver, as recommended by other sources.
The Problem
I'm encountering an error upon attempting a connection. The error says:
Microsoft OLE DB Provider for ODBC
Drivers: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
That error occurs whether run from 32-bit or 64-bit cmd (this is where I got the 32-bit process: How do I run a VBScript in 32-bit mode on a 64-bit machine?). The Microsoft Text Driver can be found in the 32-bit ODBC Drivers.
I do have a file called test.csv on my desktop and it contains the text "Cell1","Cell2","Cell3"
The Code
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
set WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
Set fileConn = CreateObject("ADODB.Connection")
Set fileRS = CreateObject("ADODB.Recordset")
strPathtoTextFile = strDesktop & "\"
' Wscript.Echo(strPathtoTextFile)
fileConn.ConnectionString = "Driver={Microsoft Text Driver (*.txt;*.csv)};DBQ=" & strPathtoTextFile & ";"
fileConn.Open
fileRS.Open "SELECT * FROM test.csv", _
fileConn, adOpenStatic, adLockOptimistic, adCmdText
The error occurs on the line fileConn.Open. The code is a modified snippet from the Microsoft article above.
Any ideas why I'm getting this error?

Unable to connect to MySQL database - no default driver specified

I'm trying to connect to a MySQL database from within a VBS script, but I can't get passes a specific error -
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I'm running 64-bit Windows 7 (from where the script is running), and the MySQL DB is running on 32-bit Linux. I've tried both the 32- and 64-bit drivers on Windows 7, downloaded from the Download Connector/ODBC page on the MySQL website, but the error persists.
I have found a couple of pages about this issue (including this one), but I've been unable to resolve my issue. How can I make the DB connection I require?
Dim Connection : Set Connection = CreateObject("ADODB.Connection")
Dim RS : Set RS = CreateObject("ADODB.Recordset")
Dim dbConStr : dbConStr = "Driver={MySQL ODBC 5.3.6 Driver};Server=https://mysqlserver.mydomain.com;Data Source=dsn_hb; Database=MyDatabase; User=MyUser; Password=MyPassword;"
Connection.Open dbConStr
RS.open "SELECT * FROM apklibrary.djg_local_archive_scans", Connection, 3
RS.MoveFirst
While Not RS.EOF
Call MsgBox (RS.Fields(0), vbOkOnly, "POW!")
RS.MoveNext
Wend
Connection.close
Set Connection = Nothing
Set RS = Nothing
Call MsgBox ("No more records to show you.", vbOkOnly, "Job done")
The specified driver name is invalid. Valid MyODBC 5.3 driver names:
{MySQL ODBC 5.3 ANSI Driver}
{MySQL ODBC 5.3 Unicode Driver}
Another problem is Server. You should specify the server's address without https://.
Also, since you have user name and password Data Source=dsn_hb; looks redundant, remove it. If not please give us more detail.
So, give a try this:
dbConStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};Server=mysqlserver.mydomain.com;Database=MyDatabase;User=MyUser;Password=MyPassword;"

Unable to reconnect to linked tables ODBC VBA

I'm using MS Access 2007 with Win7 Professional x64.
I have to connect to an external MySQL database passing by the ODBC v5.1 local driver.
From VBA I'm able to connect to my database succesfully and from Access I can open my linked tables and see the data.
You can see below the code that I'm using to create the linked table. I dont want to use DSN way; I prefer the driver way.
Dim CnnString As String
Dim tdf As DAO.TableDef
CnnString = "ODBC;DRIVER=MySQL ODBC 5.1 Driver;DATABASE=xxxx;OPTION=2048;PORT=0;SERVER=xxxxx;UID=xxxxx;PWD=xxxx;Connect Timeout=45; Command Timeout=90;"
Set tdf = CurrentDb.CreateTableDef("table1")
tdf.Connect = CnnString
tdf.SourceTableName = "table1"
CurrentDb.TableDefs.Append tdf
Set tdf = Nothing
When I quit my VBA/Access application and I reopen it, the linked table appears in the tables list but if I double click on it, the ODBC connector windows appears asking me a DSN connection, I cannot see the data and my application doesn't work.
How can I make the connection to my linked table permanent?
Thanks in advance
Now I've had the chance to check how I did this...
I have successfully (and permanently) linked DSN-less ODBC tables with VBA using the DoCmd.TransferDatabase command:
' CnnString as above
DoCmd.TransferDatabase _
TransferType:=acLink, _
DatabaseType:="ODBC", _
DatabaseName:=CnnString, _
ObjectType:=acTable, _
Source:="table1", _
Destination:="table1", _
StructureOnly:=False, _
StoreLogin:=True
The important part is StoreLogin:=True -- I don't think you can specify this when using the CreateTableDef method.

VBA/MySQL issue using ODBC connector

I have been struggling with this for a few days now. Any help much appreciated.
Trying to connect to MySQL database using Excel VBA, on a PC with the following:
Excel 2007
Windows 7 x64 Home Premium
MySQL 5.5
MySQL ODBC Connector 5.1, 64 bit
In the Excel VBA I have referenced Microsoft ActiveX Objects 2.8 Library.
The VBA I am using to connect is:
Dim oConn As ADODB.Connection
Public Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & "SERVER=localhost;" & "DATABASE=test;" & "USER=root;" & "PASSWORD=PWhere;" & "Option=3"
End Sub
Every time I run this I get the error dialog: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
MySQL service is definitely running.
I have used Windows Data Source Administrator to check that MySQL ODBC Connector 5.1 is present and correct: it is, checks out OK when I try to create a DSN in this way.
Looking through the VBA project reference options, I note the options to reference a whole host of different ADO Libraries, including (Multi-dimensional) options and library versions 2.0,2.1,2.5,2.6,2.7, and 6.0 - maybe the answer lies in one of these?
Any more details required, let me know.
You need to use either the 32 or 64 bits version depending on the version of Excel, not Windows. So even if you run Windows 7 64 bits, I believe Excel 2007 only comes in 32 bits so you would need to use the 32 bits mysql connector.
See also this bug report that is similar to your issue.
I got similar message when moved my application to other system with different version of driver - it looks like misspelled driver name causes identical message. To find correct driver name and make application driver version independent I use the following code:
Public Function Get_Driver() As String
Const HKEY_LOCAL_MACHINE = &H80000002
Dim l_Registry As Object
Dim l_RegStr As Variant
Dim l_RegArr As Variant
Dim l_RegValue As Variant
Get_Driver = ""
Set l_Registry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
l_Registry.enumvalues HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers", l_RegStr, l_RegArr
For Each l_RegValue In l_RegStr
If InStr(1, l_RegValue, "MySQL ODBC", vbTextCompare) > 0 Then
Get_Driver = l_RegValue
Exit For
End If
Next
Set l_Registry = Nothing
End Function