auto create a connect to odbc system dsn

Posted on 2009-06-30
Last Modified: 2013-12-25

I want to use VB to auto create a ODBC system DSN connect tion to my SQL server, can somebody help me?


'Constant   Declaration

              Private Const ODBC_ADD_DSN = 1                         '   Add   data   source

              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

              Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer

              'Function   Declare

                      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



  Option Explicit


  Private Sub Command1_Click()

  Dim intRet     As Long

              Dim strDriver     As String

              Dim strAttributes     As String

              Dim rc     As Integer     'ODBC   ?   ?   ?   ?   ?   ?

              Dim henv     As Long     'ODBC   ?   ?   ?   ?

                'rc   =   SQLAllocEnv(henv)   '   ?   ?ODBC   ?   ?   ?   ?


              'Set   the   driver   to   SQL   Server   because   IT   is   most   common.

              strDriver = "SQL Server"

              'Set   the   attributes   delimITed   by   null.

              'See   driver   documentation   for   a   complete

              'list   of   supported   attributes.

              strAttributes = "SERVER=TOMATO-PC" & Chr$(0)

              strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)

              strAttributes = strAttributes & "DSN=Action" & Chr$(0)

              strAttributes = strAttributes & "database=actionreport" & Chr$(0)

              strAttributes = strAttributes & "UID=sa" & Chr$(0)

              strAttributes = strAttributes & "PWD=1234" & Chr$(0)

              'To   show   dialog,   use   Form1.Hwnd   instead   of   VBAPINull.

              intRet = SQLConfigDataSource(VBAPINull, ODBC_ADD_DSN, _

              strDriver, strAttributes)

              If intRet Then

                      MsgBox "DSN   Created"


                      MsgBox "Create   Failed"

              End If

  End Sub

Open in new window

Question by:irietek
  • 2
  • 2
LVL 15

Expert Comment

ID: 24745202
LVL 31

Accepted Solution

RiteshShah earned 500 total points
ID: 24745237
very good example in microsoft site.

Author Comment

ID: 24745345
Private Const vbAPINull As Long = 0&  ' NULL Pointer

this script had error
LVL 31

Expert Comment

ID: 24745398
which script? mine or mohan's?

Author Comment

ID: 24745418
Hi, RiteshShah


Private Const vbAPINull As Long = 0&  ' NULL Pointer

in the Microsoft example

and I change  into :Private Const vbAPINull As Long = 0

it show create successfully, but I didn't see any new DSn in the ODBC

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 46
SQL index creation taking an abnormally long time 2 19
sql server query? 6 28
Add and format columns in vb6 7 22
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now