I would like to use Access 03 fe and MySQL be. I can connect when I manually create an ODBC connection. Can I use ADO to create a DSN connection in ADO to create the connection on the fly?
I found the following code somewhere on thiw site. I tried to create the DSN with the following command,
blnDum = CreateDSN("localhost", "C:\ProgramData\MySQL\MySQL Server 5.5\data\jp2", "root", "1234")
The db can be found at
C:\ProgramData\MySQL\MySQL Server 5.5\data\jp2
If can automatically create the DSN with ADO (within Access 03) is my syntax OK? I put a breakpoint in Public Function CreateDSN and variable CreateDSN is set to false.
Option Compare Database
Option Explicit
Public g_sServerName As String
Public g_sDatabaseName As String
Public g_sUID As String
Public g_sPWD As String
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_ADD_SYS_DSN = 4 ' Add System DSN
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0 ' NULL Pointer
Public Const DSNName = "MySQLExportDSN"
'Function Declare
#If Win32 Then
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
#Else
Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
#End If
Private Declare Function SQLGetInstalledDriver Lib "ODBCCP32.DLL" (ByVal lDrvList As String, ByVal lpszDriver As Long, ByVal lpszAttributes As Long) As Long
Public Function CreateDSN(ByVal sServerName_IN As String, ByVal sDSNDBName_IN As String, ByVal sUserName_IN As String, ByVal sPWD_IN As String) As Boolean
On Error GoTo errHandlerSection
#If Win32 Then
Dim lRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
Dim sDriverList As String
Dim bRetValue As Boolean
' bRetValue = SQLGetInstalledDriver(sDriverList, Len(sDriverList), Len(sDriverList))
strDriver = ""
strAttributes = ""
strDriver = "MySQL"
strAttributes = "Server=" & sServerName_IN
strAttributes = strAttributes & ";Database=" & sDSNDBName_IN & ";DSN=" & DSNName & ";uid=" & sUserName_IN & ";Password=" & sPWD_IN
lRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If lRet = 0 Then
CreateDSN = False
Else
CreateDSN = True
End If
Exit Function
errHandlerSection:
MsgBox "Cannot create DSN" & vbCrLf & "Error description : " & Err.Description, vbCritical, "Internal Error"
CreateDSN = False
End Function
Public Function RemoveDSN() As Boolean
On Error GoTo errHandlerSection
#If Win32 Then
Dim lRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
Dim sDriverList As String
Dim bRetValue As Boolean
lRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, "MySQL", DSNName)
If lRet = 0 Then
RemoveDSN = False
Else
RemoveDSN = True
End If
Exit Function
errHandlerSection:
MsgBox "Cannot delete DSN" & vbCrLf & "Error description : " & Err.Description, vbCritical, "Internal Error"
RemoveDSN = False
End Function
I found the following code somewhere on thiw site. I tried to create the DSN with the following command,
blnDum = CreateDSN("localhost", "C:\ProgramData\MySQL\MySQL Server 5.5\data\jp2", "root", "1234")
The db can be found at
C:\ProgramData\MySQL\MySQL Server 5.5\data\jp2
If can automatically create the DSN with ADO (within Access 03) is my syntax OK? I put a breakpoint in Public Function CreateDSN and variable CreateDSN is set to false.
Code:
Option Explicit
Public g_sServerName As String
Public g_sDatabaseName As String
Public g_sUID As String
Public g_sPWD As String
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_ADD_SYS_DSN = 4 ' Add System DSN
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0 ' NULL Pointer
Public Const DSNName = "MySQLExportDSN"
'Function Declare
#If Win32 Then
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
#Else
Private Declare Function SQLConfigDataSource Lib "ODBCINST.DLL" (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
#End If
Private Declare Function SQLGetInstalledDriver Lib "ODBCCP32.DLL" (ByVal lDrvList As String, ByVal lpszDriver As Long, ByVal lpszAttributes As Long) As Long
Public Function CreateDSN(ByVal sServerName_IN As String, ByVal sDSNDBName_IN As String, ByVal sUserName_IN As String, ByVal sPWD_IN As String) As Boolean
On Error GoTo errHandlerSection
#If Win32 Then
Dim lRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
Dim sDriverList As String
Dim bRetValue As Boolean
' bRetValue = SQLGetInstalledDriver(sDriverList, Len(sDriverList), Len(sDriverList))
strDriver = ""
strAttributes = ""
strDriver = "MySQL"
strAttributes = "Server=" & sServerName_IN
strAttributes = strAttributes & ";Database=" & sDSNDBName_IN & ";DSN=" & DSNName & ";uid=" & sUserName_IN & ";Password=" & sPWD_IN
lRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If lRet = 0 Then
CreateDSN = False
Else
CreateDSN = True
End If
Exit Function
errHandlerSection:
MsgBox "Cannot create DSN" & vbCrLf & "Error description : " & Err.Description, vbCritical, "Internal Error"
CreateDSN = False
End Function
Public Function RemoveDSN() As Boolean
On Error GoTo errHandlerSection
#If Win32 Then
Dim lRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
Dim sDriverList As String
Dim bRetValue As Boolean
lRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, "MySQL", DSNName)
If lRet = 0 Then
RemoveDSN = False
Else
RemoveDSN = True
End If
Exit Function
errHandlerSection:
MsgBox "Cannot delete DSN" & vbCrLf & "Error description : " & Err.Description, vbCritical, "Internal Error"
RemoveDSN = False
End Function
Code: