Results 1 to 2 of 2

Thread: creating a DSN thru a VB form

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    11

    creating a DSN thru a VB form

    is it possible to create a DSN , thru a VB form.....using API calls..

    and not have to go thru the Control Panel > 32-bit ODBC Data Source - to create a DSN ?

    if someone could suggest some code, itd be helpful

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Found this thru www.codehound.com.

    -------------------------------------------------------
    The following routines show how to add and delete ODBC DSNsprogramatically. Note, further information on DSNs can be found in the following download:

    http://www.vbusers.com/downloads/download.asp#item12

    VB Code:
    1. '----DSN Declarations--------
    2. Option Explicit
    3.  
    4. Public Enum eDBType
    5.     FileBased
    6.     ServerBased
    7. End Enum
    8.  
    9. Public Type tDSNAttrib
    10.     Type As eDBType                 'FileBased (eg Access) or ServerBased (eg. SQL Server)
    11.     Server As String                'Database Server
    12.     Description As String           'Database description
    13.     DSN As String                   'The DSN Name
    14.     Driver As String                'The Drive name
    15.     Database As String              'Name or path of database
    16.     UserID As String                'The UserID
    17.     Password As String              'The User Password
    18.     TrustedConnection As Boolean    'If True ignore the UserID and Password as will us NT
    19.     SystemDSN As Boolean            'If True creates a system DSN, else creates a user DSN.
    20. End Type
    21.  
    22. Private Const ODBC_ADD_DSN = 1
    23. Private Const ODBC_CONFIG_DSN = 2
    24. Private Const ODBC_REMOVE_DSN = 3
    25. Private Const ODBC_ADD_SYS_DSN = 4
    26. Private Const ODBC_CONFIG_SYS_DSN = 5
    27. Private Const ODBC_REMOVE_SYS_DSN = 6
    28. 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
    29.  
    30. 'Purpose     :  Creates a new DSN
    31. 'Inputs      :  tAttributes             A type containing the input parameters for the DSN.
    32. '                                       Look in either "C:\WINNT\Odbc.ini" or the registry under "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"
    33. '                                       for typical details.
    34. 'Outputs     :  Returns True if successful
    35. 'Author      :  Andrew Baker
    36. 'Date        :  15/01/2001 11:55
    37. 'Notes       :  If TrustedConnection is set to False, then you must supply a valid UID
    38. '               and PWD (username and password), otherwise the DSN will not be created.
    39. 'Revisions   :
    40. 'Assumptions :
    41.  
    42. Public Function DSNCreate(tAttributes As tDSNAttrib) As Boolean
    43.     Dim lRet As Long
    44.     Dim sAttributes As String
    45.    
    46.     On Error Resume Next
    47.     If tAttributes.Type = FileBased Then
    48.         'File based database
    49.         sAttributes = "DBQ=" & tAttributes.Database & vbNullChar
    50.     Else
    51.         'Server based database
    52.         sAttributes = "Server=" & tAttributes.Server & vbNullChar
    53.         sAttributes = sAttributes & "DATABASE=" & tAttributes.Database & vbNullChar
    54.     End If
    55.    
    56.     'Name of DSN
    57.     sAttributes = sAttributes & "DSN=" & tAttributes.DSN & vbNullChar
    58.    
    59.     If Len(tAttributes.Description) Then
    60.         'Description
    61.         sAttributes = sAttributes & "DESCRIPTION=" & tAttributes.Description & vbNullChar
    62.     End If
    63.    
    64.     If tAttributes.TrustedConnection Then
    65.         'Use Windows NT Authentication
    66.         '(will only validate the username and password when connection to the database)
    67.         sAttributes = sAttributes & "Trusted_Connection=Yes" & vbNullChar
    68.     Else
    69.         'Specify a username and password (must specify a valid username and password)
    70.         If Len(tAttributes.UserID) Then
    71.             sAttributes = sAttributes & "UID=" & tAttributes.UserID & vbNullChar
    72.         End If
    73.        
    74.         If Len(tAttributes.Password) Then
    75.             sAttributes = sAttributes & "PWD=" & tAttributes.Password & vbNullChar
    76.         End If
    77.     End If
    78.     If tAttributes.SystemDSN Then
    79.         'Create a system DSN (visible to all users and services)
    80.         DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, tAttributes.Driver, sAttributes)
    81.     Else
    82.         'Create a user DSN (visible to the current users)
    83.         DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_DSN, tAttributes.Driver, sAttributes)
    84.     End If
    85. End Function
    86.  
    87. 'Purpose     :  Deletes an existing DSN
    88. 'Inputs      :  tAttributes             A type containing the input parameters of the DSN.
    89. '                                       Look in either "C:\WINNT\Odbc.ini" or the registry under "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI"
    90. '                                       for typical details.
    91. '               [bSystemDSN]            If True deletes as system DSN, else deletes a user DSN.
    92. 'Outputs     :  Returns True if successful
    93. 'Author      :  Andrew Baker
    94. 'Date        :  15/01/2001 11:55
    95. 'Notes       :
    96. 'Revisions   :
    97. 'Assumptions :
    98.  
    99. Public Function DSNDelete(sDSN As String, sDriver As String, Optional bSystemDSN As Boolean = False) As Boolean
    100.     Dim lRet As Long
    101.     Dim sAttributes As String
    102.    
    103.     On Error Resume Next
    104.     sAttributes = "DSN=" & sDSN & vbNullChar
    105.     If bSystemDSN Then
    106.         DSNDelete = SQLConfigDataSource(0&, ODBC_REMOVE_DSN, sDriver, sAttributes)
    107.     Else
    108.         DSNDelete = SQLConfigDataSource(0&, ODBC_REMOVE_SYS_DSN, sDriver, sAttributes)
    109.     End If
    110. End Function
    111.  
    112. 'Demonstration routine
    113. Sub Test()
    114.     Dim tDSNDetails As tDSNAttrib
    115.    
    116. '---Add an Access DSN
    117.     With tDSNDetails
    118.         .Database = "C:\vbusers.mdb"
    119.         .Driver = "Microsoft Access Driver (*.mdb)"
    120.         .Password = ""
    121.         .UserID = "Admin"
    122.         .DSN = "TestDSN"
    123.         .Description = "A Test Database"
    124.         .Type = FileBased
    125.     End With
    126.     If DSNCreate(tDSNDetails) Then
    127.         MsgBox "Created user DSN"
    128.         'Delete the new DSN
    129.         If DSNDelete(tDSNDetails.DSN, tDSNDetails.Driver) Then
    130.             MsgBox "Deleted New DSN"
    131.         Else
    132.             MsgBox "Failed to Delete New DSN"
    133.         End If
    134.     Else
    135.         MsgBox "Failed to Create DSN"
    136.     End If
    137.    
    138. '---Add an SQL Server DSN
    139.     With tDSNDetails
    140.         .Database = "Pubs"
    141.         .Driver = "SQL Server"
    142.         .Server = "MyServer"
    143.         .TrustedConnection = True    'Use NT authentication
    144.         .Password = ""
    145.         .UserID = ""
    146.         .DSN = "TestDSN2"
    147.         .Description = "A Test Database2"
    148.         .Type = ServerBased
    149.         .SystemDSN = True           'Create a System DSN
    150.     End With
    151.     If DSNCreate(tDSNDetails) Then
    152.         MsgBox "Created system DSN"
    153.         'Delete the new DSN
    154.         If DSNDelete(tDSNDetails.DSN, tDSNDetails.Driver) Then
    155.             MsgBox "Deleted New DSN"
    156.         Else
    157.             MsgBox "Failed to Delete New DSN"
    158.         End If
    159.     Else
    160.         MsgBox "Failed to Create DSN"
    161.     End If
    162. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width