by Kaustubh Zoal (10 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 21st June 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
This code snippet creates a DSN at runtime. The parameters are read through an INI file.
API Declarations
'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
'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
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim dbPath As String
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Dim iniPath As String
dbPath = String(255, " ")
iniPath = App.Path & "\DSN.ini"
Call GetPrivateProfileString("DSN", "ServerPath", "Default", dbPath, 255, iniPath)
createDSN
End Sub
Private Sub createDSN()
readINI (App.Path & "\DSN.INI")
#If Win32 Then
Dim intRet As Long
#Else
Dim intRet As Integer
#End If
Dim strDriver As String
Dim strAttributes As String
strDriver = "SQL Server"
sDBPath = ""
sDBPath = "DSN=dScanTemp;" '& Chr$(0)
sDBPath = sDBPath & "DRIVER={SQL Server};"
sDBPath = sDBPath & "SERVER={COMP163};" '& Chr$(0)
sDBPath = sDBPath & "DATABASE=Scan;"
sDBPath = sDBPath & "UID=sa;"
strAttributes = sDBPath
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)
If intRet Then
MsgBox "DSN Created "
Else
MsgBox "DSN Creation Failed please Check ... ", vbCritical, "DSN Creation"
End If
End Sub
Private Sub Form_Load()
getINIDBPath
End Sub
No comments have been posted about This code snippet creates a DSN at runtime. The parameters are read through an INI file.. Why not be the first to post a comment about This code snippet creates a DSN at runtime. The parameters are read through an INI file..