Option Explicit
Private Const REG_SZ = 1 'Constant for a string variable type.
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
"RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal _
cbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Sub Command1_Click()
On Error GoTo err
Dim DataSourceName As String
Dim DatabaseName As String
Dim Description As String
Dim DriverPath As String
Dim DriverName As String
Dim LastUser As String
Dim Regional As String
Dim Server As String
Dim AutoReconnect As String
Dim PromptAuth As String
Dim Port As String
Dim PWD As String
Dim lResult As Long
Dim hKeyHandle As Long
'Specify the DSN parameters.
DataSourceName = "DIKA"
DatabaseName = "Nama Database"
Description = "DSN Description"
DriverPath = "C:\Program Files\MySQL\Connector ODBC 5.2\Unicode\myodbc5w.dll"
LastUser = "root"
Server = "127.0.0.1"
DriverName = "MySQL ODBC 5.2w Driver"
AutoReconnect = "1"
PromptAuth = "1"
Port = "3306"
PWD = "password mysql"
'Create the new DSN key.
lResult = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\" & _
DataSourceName, hKeyHandle)
'Set the values of the new DSN key.
lResult = RegSetValueEx(hKeyHandle, "Database", 0&, REG_SZ, _
ByVal DatabaseName, Len(DatabaseName))
lResult = RegSetValueEx(hKeyHandle, "Description", 0&, REG_SZ, _
ByVal Description, Len(Description))
lResult = RegSetValueEx(hKeyHandle, "Driver", 0&, REG_SZ, _
ByVal DriverPath, Len(DriverPath))
lResult = RegSetValueEx(hKeyHandle, "UID", 0&, REG_SZ, _
ByVal LastUser, Len(LastUser))
lResult = RegSetValueEx(hKeyHandle, "Server", 0&, REG_SZ, _
ByVal Server, Len(Server))
lResult = RegSetValueEx(hKeyHandle, "AUTO_RECONNECT", 0&, REG_SZ, _
ByVal AutoReconnect, Len(AutoReconnect))
lResult = RegSetValueEx(hKeyHandle, "NO_PROMPT", 0&, REG_SZ, _
ByVal PromptAuth, Len(PromptAuth))
lResult = RegSetValueEx(hKeyHandle, "PORT", 0&, REG_SZ, _
ByVal Port, Len(Port))
lResult = RegSetValueEx(hKeyHandle, "PWD", 0&, REG_SZ, _
ByVal PWD, Len(PWD))
'Close the new DSN key.
lResult = RegCloseKey(hKeyHandle)
'Open ODBC Data Sources key to list the new DSN in the ODBC Manager.
'Specify the new value.
'Close the key.
lResult = RegCreateKey(HKEY_LOCAL_MACHINE, _
"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", hKeyHandle)
lResult = RegSetValueEx(hKeyHandle, DataSourceName, 0&, REG_SZ, _
ByVal DriverName, Len(DriverName))
lResult = RegCloseKey(hKeyHandle)
MsgBox "DSN Baru Berhasil Di Buat.", vbInformation, "#Created"
Exit Sub
err:
MsgBox err.Description, vbExclamation, err.Number
Exit Sub
End Sub
0 comments:
Post a Comment