VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Dynamically generate an ODBC DSN

by Royce Powers (4 Submissions)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

Class object that can be compiled or copied and pasted into your application that will dynamically create ODBC DSN's for you.

Inputs
DSN Name, Server, Database, DSN Type, UserID and Password
Code Returns
0 if success, OSBC DSN is created
API Declarations
Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal henv _
As Long, phdbc As Long) As Integer
Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal hdbc As _
Long) As Integer
Declare Function SQLConnect Lib "odbc32.dll" (ByVal hdbc As _
Long, ByVal szDSN As String, ByVal cbDSN As Integer, ByVal szUID As _
String, ByVal cbUID As Integer, ByVal szAuthStr As String, ByVal _
cbAuthStr As Integer) As Integer
Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal henv As _
Long) As Integer
Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hdbc _
As Long) As Integer
Declare Function SQLError Lib "odbc32.dll" (ByVal henv As _
Long, ByVal hdbc As Long, ByVal hstmt As Long, ByVal szSqlState As _
String, pfNativeError As Long, ByVal szErrorMsg As String, ByVal _
cbErrorMsgMax As Integer, pcbErrorMsg As Integer) As Integer
Declare Function SQLConfigDataSource Lib "ODBCCP32" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long

Rate Dynamically generate an ODBC DSN

IN MODULE (.BAS)
Option Explicit
Public Const vbAPINull As Long = 0&
Private Const SQL_SUCCESS As Long = 0
Private Const SQL_SUCCESS_WITH_INFO As Long = 1
Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal henv _
 As Long, phdbc As Long) As Integer
Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal hdbc As _
 Long) As Integer
Declare Function SQLConnect Lib "odbc32.dll" (ByVal hdbc As _
 Long, ByVal szDSN As String, ByVal cbDSN As Integer, ByVal szUID As _
 String, ByVal cbUID As Integer, ByVal szAuthStr As String, ByVal _
 cbAuthStr As Integer) As Integer
Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal henv As _
 Long) As Integer
Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hdbc _
 As Long) As Integer
Declare Function SQLError Lib "odbc32.dll" (ByVal henv As _
 Long, ByVal hdbc As Long, ByVal hstmt As Long, ByVal szSqlState As _
 String, pfNativeError As Long, ByVal szErrorMsg As String, ByVal _
 cbErrorMsgMax As Integer, pcbErrorMsg As Integer) As Integer
Declare Function SQLConfigDataSource Lib "ODBCCP32" _
 (ByVal hwndParent As Long, ByVal fRequest As Long, _
 ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
In Class (.CLS)
Option Explicit
Public Enum peDSN_OPTIONS
 ODBC_ADD_DSN = 1
 ODBC_CONFIG_DSN = 2
 ODBC_ADD_SYS_DSN = 4
 ODBC_CONFIG_SYS_DSN = 5
End Enum
Public Function RegisterDataSource(iFunction As peDSN_OPTIONS, sDSNName As String, sServerName As String, sDatabasename As String, sUserID As String, sPassword As String) As Integer
 Dim sAttributes As String
 Dim iRetVal As Integer
  
 
 
 sAttributes = "DSN=" & sDSNName _
  & Chr$(0) & "Description=SQL Server on server " & sServerName _
  & Chr$(0) & "SERVER=" & sServerName _
  & Chr$(0) & "Database=" & sDatabasename _
  & Chr$(0) & Chr$(0)
 iRetVal = SQLConfigDataSource(vbAPINull, iFunction, "SQL Server", sAttributes)
End Function

Download this snippet    Add to My Saved Code

Dynamically generate an ODBC DSN Comments

No comments have been posted about Dynamically generate an ODBC DSN. Why not be the first to post a comment about Dynamically generate an ODBC DSN.

Post your comment

Subject:
Message:
0/1000 characters