VB and VBA Users Source Code: Creating a Sybase DSN (without using DSEDIT)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Creating a Sybase DSN (without using DSEDIT)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, August 01, 2002
Hits:
1944
Category:
Windows API
Article:
The following code demonstrates how to create a Sybase DSN (by placing entries in the sybase.ini file and writing to the ODBC.INI key in the registry). Option Explicit Public Enum eProtocol epNLWNSCK epTCP epNAMEPIPE epSPX epDECNET epVINESIP End Enum Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpSection As String, ByVal lpSetting As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function SetPrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpSection As String, ByVal lpSetting As String, ByVal lpValue As String, ByVal lpFileName As String) As Long Private Declare Function SQLConfigDataSource Lib "odbccp32.dll" (ByVal hwndParent As Long, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Boolean 'Purpose : Creates a Sybase DSN (code equivilant to using DSEDIT and the ODBC Adminstrator) 'Inputs : sDSN The name of the DSN ' sServerName The server name or IP address. ' lPort The port number of the server. ' [Protocol] The connection protocol (defaults to TCP). ' [sDefaultDatabaseName] The default database name. ' [sDescription] The DSN description. ' [sDefaultLogin] The default login. 'Outputs : Returns the handle of parent window owned by the specified process 'Author : Andrew Baker (copyright www.vbusers.com) 'Date : 01/11/2000 13:17 'Notes : 'Revisions : 'Assumptions : Function SybaseCreateDSN(sDSN As String, sServerName As String, lPort As Long, Optional Protocol As eProtocol = epTCP, Optional sDefaultDatabaseName As String = "", Optional sDescription As String, Optional sDefaultLogin As String = "") As Boolean Dim sSybaseIni As String, sProtocol As String Const ODBC_ADD_SYS_DSN = 4 On Error GoTo ErrFailed sSybaseIni = Environ$("Sybase") & "\INI\sql.ini" If Len(Dir$(sSybaseIni)) > 0 Then Select Case Protocol Case epDECNET sProtocol = "DECNET" Case epNAMEPIPE sProtocol = "NAMEPIPE" Case epNLWNSCK sProtocol = "NLWNSCK" Case epSPX sProtocol = "SPX" Case epTCP sProtocol = "TCP" Case epVINESIP sProtocol = "VINESIP" End Select 'Save settings to Sybase Ini File IniSettingSave sSybaseIni, sDSN, "master", sProtocol & "," & sServerName & "," & CStr(lPort) IniSettingSave sSybaseIni, sDSN, "query", sProtocol & "," & sServerName & "," & CStr(lPort) 'Save setting to registry SybaseCreateDSN = SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, "Sybase System 11", "DSN=" & sDSN & vbNullChar & "ServerName=" & sServerName & vbNullChar & "Database=" & sDefaultDatabaseName & vbNullChar & "UseProcForPrepare=Yes" & vbNullChar & "Description=" & sDescription & vbNullChar & "DefaultLogonID=" & sDefaultLogin & vbNullChar & vbNullChar) SybaseCreateDSN = True End If Exit Function ErrFailed: SybaseCreateDSN = False Debug.Print "SybaseCreateDSN: " & Err.Description Debug.Assert False End Function 'Reads a value from an ini file Public Function IniSettingLoad(ByRef sFilename As String, ByRef sSection As String, ByRef sSetting As String) As String Dim lLenValue As Long Dim sBuffer As String * 256 lLenValue = GetPrivateProfileString(sSection, sSetting, "", sBuffer, 255, sFilename) IniSettingLoad = Left$(sBuffer, lLenValue) End Function 'Saves a value to an ini file Public Sub IniSettingSave(ByRef sFilename As String, ByRef sSection As String, ByRef sSetting As String, ByRef sValue As String) SetPrivateProfileString sSection, sSetting, sValue, sFilename End Sub 'Demonstration code Sub Test() If SybaseCreateDSN("aMyDSN", "aMyServer", 12000, epNLWNSCK, "aDefaultDatabase", "aDescription", "aDefaultLogin") Then MsgBox "Created DSN" End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder