How to read .csv file with VBScript and ADO - csv

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?

Related

How to INSERT/SELECT data from MySQL to Excel/VBA?

I'm trying to extract data from MySQL to Excel. I saw a lot of tutorials but I get the same error all the time.
Here is my code:
Sub consultdate()
Dim conn As New ADODB.Connection
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String
Dim i As Long
Dim sqlstr As String
Dim table1 As String, table2 As String
Dim field1 As String, field2 As String
Dim rs As ADODB.Recordset
Dim vtype As Variant
server_name = "localhost"
database_name = "controle_fin"
user_id = "root"
password = "userpassword"
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"
table1 = "classe"
GoTo skipextract
Set rs = New ADODB.Recordset
sqlstr = "SELECT * FROM " & table1
rs.Open sqlstr, conn, adOpenStatic
Planilha1.Range("A1").CopyFromRecordset rs
skipextract:
On Error Resume Next
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
On Error GoTo 0
End Sub
Error that I got:
"ODBC Error. Data Source Name Not Found and No Default Driver Specified"
Typically, this error is due to the following reasons:
Simple Misspelling: ODBC driver names must be exactly spelled correctly and fully including special characters. To see all ODBC drivers, open up PowerShell window and type: Get-OdbcDriver | Format-Table name, platform -AutoSize to output a table similar to below:
name platform
---- --------
Microsoft Access Driver (*.mdb) 32-bit
Microsoft dBase Driver (*.dbf) 32-bit
Microsoft Excel Driver (*.xls) 32-bit
Microsoft ODBC for Oracle 32-bit
SQL Server 32-bit
SQL Server Native Client 11.0 32-bit
ODBC Driver 13 for SQL Server 32-bit
SQL Server 64-bit
Microsoft Access Driver (*.mdb, *.accdb) 64-bit
Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) 64-bit
Microsoft Access Text Driver (*.txt, *.csv) 64-bit
SQL Server Native Client 11.0 64-bit
PostgreSQL ANSI(x64) 64-bit
PostgreSQL Unicode(x64) 64-bit
ODBC Driver 13 for SQL Server 64-bit
Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx) 64-bit
...
Incompatible 32/64 bit architectures: Whatever ODBC driver you connect to must maintain same bit architecture as your MS Office version. If you run 32-bit Office, only 32-bit drivers are available (indicated under platform in step 1 output). Similarly goes for 64-bit. To verify which Office bit version you have installed on Windows and Mac, see here.
Permissions issue: While drivers are available, they may not be accessible to current user or group at file and/or folder level. This tends to be more an issue on *Unix systems (Linux/Mac) and not Windows which requires chmod or chown handling. For Windows, adjust properties of file or folder in Explorer for read/write/execute permissions.

Connecting to another access database in windows 365

We have upgraded from Windows 7 to windows 365.
We have a macro within an access database. The macro opens another database using the code below. However since moving across to windows 365 it doesn't like the cn.open line.
How do you connect to another database from within Access in windows 365?
dim cn As ADODB.Connection
set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & pDBPATH & pDBNAME
the error message is,
run-time error 3706: Provider cannot be found. it may not be properly installed.
Although after reading it looks like Microsoft Jet OLEDB 4.0 is not 64 bit. So is there another way?
There are many ways to connect between databases in Access.
The most usual provider for OLEDB connection is the Microsoft.ACE.OLEDB.12.0 provider:
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & pDBPATH & pDBNAME
You can view two different providers Access uses to connect to the current database, by executing the following code:
Debug.Print CurrentProject.Connection.ConnectionString 'Usual connection
Debug.Print CurrentProject.AccessConnection.ConnectionString 'Used for updateable forms

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;"

MySQL query via an HTA Application

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.

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