VB6 & Access 2007 connection problems - ms-access

OK, so I've read this identical question, but the answers don't work for me:
Connecting VB6 and MS Access 2007
I've removed the reference to Microsoft DAO 3.6 Object Library, and replaced it with a reference to Microsoft Office 12.0 Access database engine Object.
Here's my code:
Dim rcdSetData As ADODB.Recordset
Set rcdSetData = New ADODB.Recordset
rcdSetData.CursorType = adOpenStatic
rcdSetData.ActiveConnection = _
"Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=" & DBName & "; "
I can open Access 2003 *.mdb files just fine, but when I try to open an Access 2007 *.accdb file, I get:
Error #blahblah, Unrecognized database format 'C:\path\foo.accdb'
foo.accdb is a valid Access 2007 file, as far as Access 2007 cares. No password, BTW, and it isn't open in Access when I run the program.
I'm stumped. EVERY answer I can find on the web says I'm doing this right.... :\

The DAO reference (be it to the pre- or post-Access 2007 version of that library) is irrelevant, since your VB code is using ADO instead. Your problem lies rather with the connection string. Try replacing
"Provider=Microsoft.Jet.OLEDB.4.0; " _
with
"Provider=Microsoft.ACE.OLEDB.12.0; " _

If you have recently re-installed VB6, make sure you download and install the service pack SP6 for it.

Related

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

Connect to online mySQL database using VBA

Objective:
I'm trying to connect to a database (e.g. associated with a website hosted by goDaddy) via VBA; using MS Word. I would like to distribute the VBA code via a word template so that others can also connect to my database.
Current Understanding - Is it correct?
In order to connect to a remote mySQL database I MUST configure a ODBC Data Source using (for example) mySQL Connector/ODBC (available here)?
There seems to be a way to connect without using a DSN as suggested here.
Issue:
I have been trying to use the mySQL Connector tool and am attempting to configure it with the information I have at hand. Steps taken:
download connector tool from dev.mysql.com
Control Panel > System & Security > Administrative Tools > ODBC Data Sources 64 Bit
add host: www.mywebsite.com
add user: NameOfDataBaseUser
add pwrd: PWForUser
I get the impression that I am using the wrong credentials... I found some documentation that said a list of DataBases would be displayed if connection is successful. That suggests to me that I should be using credentials for a master user - which user would that be?
Disclaimer
I do plan to connect to an online DB via VBA, but suspect that it might be better to connect indirectly via a php web-page.
If anyone has thoughts on this (security, ease of deployment, other) please let me know, it would probably be a new question. Other disclaimer, I am highly INexperienced with databases but keen to learn - slowly ;-)
I am currently working on a project with Excel where I am successfully connecting to a remote MySQL database.
I am using the DSN-less approach and this could probably work well for you, too:
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=myhomepage.com;PORT=3306;DATABASE=mydb;" & _
"UID=username;PWD=secret"
remoteCon.Open conStr
remoteCon.Execute ("USE mydb;")
In order for this to work, you also have to add a reference (in VBA backend): Tools > References > Check "Microsoft ActiveX Data Objects x.x Library".
You also need to have the MySQL ODBC Driver (in my case "MySQL ODBC 5.3 Unicode Driver") installed on your computer.
Queries can then be executed like this:
Dim rs As ADODB.Recordset
Set rs = remoteCon.Execute("SELECT * FROM table")
If Not rs.BOF And Not rs.EOF Then
result = rs.GetRows
End If
How to connect VBA to a Remote mySQL DataBase using ODBC
Thanks to #EVilliger & #tobifasc for your help with this, there are many 'how to configure mySQL questions' floating around but none that solved my (larger) issue.
Basic problem - my host does NOT allow remote connections to the database, except for from a single white-listed IP (this seems fairly common).
Questions Answered:
It turns out that you do NOT need to configure the mySQL connection using the connector, however you DO NEED to have an appropriate ODBC driver installed. The connector (with the driver) can be found here: https://dev.mysql.com/downloads/connector/odbc/
I uninstalled the mySQL Connector and everything seemed to continue working, until it didn't. Conclusion don't uninstall the mySQL Connector unless you have something to replace it with.
The credentials to use can be for a database user, not some elevated user.
For anyone interested in setting up and experimenting with mySQL from VBA here is a way forward:
Download & install the drivers (see above) - if the install fails you may need to install vcredist_x64.exe
Set-up a free mySQL database, I used HelioHost: https://www.heliohost.org/ (they will also give you a domain)
Create a database & user in HelioHost cPanel
Configure remote access - use wildcard % to allow all IPs
Add user to database
Use the following code to connect...
Code from accepted answer:
Sub connect2OnlineSQL()
' Note: add referecne to Microsoft ActiveX Data Objects #.# Library
' Tools > References > (scroll down...)
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=something.heliohost.org;PORT=3306;DATABASE=db_name;" & _
"UID=db_user;PWD=yourPassWordHere"
remoteCon.Open conStr
remoteCon.Execute ("USE db_name;")
End Sub
Thanks again :)
Following solution worked for me
Prerequisite:
Under Developer ->Tools->Reference Add the relevant Plug in for Oracle
Download and install ODBC Driver version for Oracle which you are using (32bit/64 bit)
Windows search -> ODBC Data Source -> Add Oracle driver under user DSN and System DSN
Copy paste below code which I used for MySQL and change the parameter based on your requirement
Sub ConnectToDB()
dbName = InputBox("Enter DB Name")
'Connection To MySQL
Dim oConn As ADODB.Connection
Dim str As String
str = "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=Employeeportal;PORT=3306;UID=root;PWD=root;"
Set oConn = New ADODB.Connection
oConn.Open str
MsgBox "Connected to MySQL DB"
'Exporting result set to Excel
Dim query As String
query = "select * from " & dbName
Dim recordSet As New ADODB.recordSet
recordSet.Open query, oConn
Sheet1.Range("A1:D1").CopyFromRecordset recordSet
oConn.Close
End Sub

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.

Excel-Vba Connect Mysql localhost failed

Trying to implement Excel VBA: writing to mysql database.
Following code runs into a run-time error [MIcrosoft][ODBC Driver Manager] Data source name not found and no default driver specified
Dim cn As Object
Sub Connect()
Dim strCon as string
Set cn = CreateObject("ADODB.connection")
strCon = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=dbname;" & _
"USER=root;" & _
"PASSWORD=mypass;" & _
"Port=3306;" & _
"Option=3"
cn.Open strCon
cn.Close
End Sub
checked connection parameters with the following php code
$mysqli = new mysqli('localhost','root','mypass','dbname');
and all works just fine.
I do have Microsoft ActiveX Data Objects 2.8 Library ticked in my VBA Project References.
Any help is welcomed.
For whatever it's worth at this point (since I just ran into it and it took me a day to figure it out):
You may have MySQL (or other Database/Application) in either 32 bit or 64 bit form.
You may have Excel (or other application) in either 32 or 64 bit form.
You may have the ODBC "connection" between the two in either 32 or 64 bit form.
Typically, the ODBC driver will be installed when you install the DB product, so it will be the same as the database.
Unfortunately, that's not important.
What IS important is that the ODBC driver match the application (e.g., Excel) properly.
If you have 32 bit Excel, and install 64 bit MySQL, that's fine, but Excel won't be able to connect to MySQL (except through the MySQL Connector, but that's not the goal here).
You won't be able to write an Excel app to read, via ODBC, from the database.
The solution is simple. Download the MySQL ODBC driver for 32 bit (same place as MySQL), and install it.
(It will tell you 'already installed' - because it will see the 64 bit version - and ask if you want to uninstall. Say Yes.)
Now, 32bit Excel can talk to 32bit ODBC driver, which can talk to 64bit MySQL, to exchange data.
Note: Far as I know, you can only install one type of ODBC driver (per DB App). So, if you are using the 64bit version, perhaps for some other app, you'll have to uninstall it (which breaks access to that App) to install the 32bit version so you can use Excel.
I have a 64 Win machine with 32bit Excel. I experimented with different MySQl ODBC drivers (5.1, 5.2, 5.3). The 64bit drivers did not work fro me, but the 32bit odbc driver did work.
Somewhere in the Microsoft knowledgebase it mentioned that Excel does not work with the 64bit drivers.
I recommend using a more recent one than the one you mentioned in your post:
http://dev.mysql.com/downloads/connector/odbc/5.2.html
This vba code worked for me
Set oConn = New ADODB.Connection
With oConn
.ConnectionString = "Driver={MySQL ODBC 5.2 Unicode Driver};" & _
"Server=" & strServer & ";Port=3306;" & _
"Database=" & strDBName & ";" & _
"Uid=" & strUserID & ";" & _
"Pwd=" & strPasswd & ";Option=3;"
.open
end with

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