Solved

auto create a connect to odbc system dsn

Posted on 2009-06-30
5
397 Views
Last Modified: 2013-12-25
Hi,

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

thanks

'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"
              Else
                      MsgBox "Create   Failed"
              End If
  End Sub

Open in new window

0
Comment
Question by:Simon Chen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24745202
0
 
LVL 31

Accepted Solution

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

http://support.microsoft.com/kb/171146
0
 

Author Comment

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


this script had error
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24745398
which script? mine or mohan's?
0
 

Author Comment

by:Simon Chen
ID: 24745418
Hi, RiteshShah

Yours

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
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 53
SQL Job Failed 6 31
Group ordersum by orderdate 3 21
Create a list of Worksheets based on Cell Value 14 33
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

752 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