db_connected = False
' Get database connection settings
Dsn_name = Trim (Worksheets ("Load Policy"). Cells (2, 5). Value)------(data SOURCE NAME in the ODBC configuration)
user_name = Trim (Worksheets ("Load Policy"). Cells (3, 5). Value)
Pass_word = Trim (Worksheets ("Load Policy"). Cells (4, 5). Value)
' Get Database Settings
db_name = Trim (Worksheets ("Load Policy"). Cells (8, 5). Value)--------Database name
Set cn = New Connection
Set rs = New Recordset
Set rs1 = New Recordset
Set cmd = New Command
Set A_app = New Application
If Dsn_name = "Then
MsgBox ("Please set ODBC name")
Else
cn. ConnectionString = "Driver={mysql ODBC 5.1 DRIVER}; User id= "& User_name &"; Password= "& Pass_word &";D ata source= "& Dsn_name
ODBC configuration:
MySQL version: MySQL5.5
ODBC version: Mysql-connector-odbc-5.1.13-win32.msi
Mysql-connector-odbc-5.1.13-winx64.msi
After installing these two versions
Configuration:
1. Open the Control Panel
2. Search the data source and open
3. User dsn: Click Add, fill it out as required, make sure it's all right.
Note the driver there to click Apply.
VBA connection to MySQL database and ODBC configuration (ODBC version and MySQL version if mismatch can occur driver and application errors)