home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Run_a_SQL_400771272001.psc / EBDSNCombo.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-12-06  |  8.7 KB  |  237 lines

  1. VERSION 5.00
  2. Begin VB.UserControl EBDSNCombo 
  3.    ClientHeight    =   315
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   2430
  7.    ScaleHeight     =   315
  8.    ScaleWidth      =   2430
  9.    ToolboxBitmap   =   "EBDSNCombo.ctx":0000
  10.    Begin VB.ComboBox cboDSN 
  11.       Height          =   315
  12.       Left            =   0
  13.       Style           =   2  'Dropdown List
  14.       TabIndex        =   0
  15.       Top             =   0
  16.       Width           =   2235
  17.    End
  18. Attribute VB_Name = "EBDSNCombo"
  19. Attribute VB_GlobalNameSpace = False
  20. Attribute VB_Creatable = True
  21. Attribute VB_PredeclaredId = False
  22. Attribute VB_Exposed = False
  23. '[Description]
  24. '   This user control offers a pick list of User and System ODBC DSNs.
  25. '[Author]
  26. '   Richard Allsebrook  <RA>    RichardAllsebrook@earlybirdmarketing.com
  27. '[Updates and Additions to this control in this situation]
  28. '   Christopher Kesler <CK> chriskesler1@hotmail.com
  29. '[History]
  30. '   Version 1.0.0   06 Jun 2001
  31. '   Revised 1.0.1   13 Dec 2001 by <CK>
  32. '[Declarations]
  33. Option Explicit
  34. 'Property Storage
  35. Private strDriverFilter     As String           'DriverFilter
  36. Private prpEnable           As Boolean
  37. 'Mapped Properties
  38. 'DSN => cboDSN.Text
  39. '[ODBC API Declarations]
  40. Private Declare Function SQLGetDiagRec Lib "odbc32" ( _
  41.   ByVal HandleType As Integer, _
  42.   ByVal Handle As Long, _
  43.   ByVal RecNumber As Integer, _
  44.   ByVal SQLState As String, _
  45.   ByRef NativeErrorPtr As Long, _
  46.   ByVal MessageText As String, _
  47.   ByVal BufferLength As Integer, _
  48.   ByRef TextLengthPtr As Integer) _
  49.     As Integer
  50. Private Declare Function SQLAllocHandle Lib "odbc32" ( _
  51.   ByVal HandleType As Integer, _
  52.   ByVal InputHandle As Long, _
  53.   ByRef OutputHandle As Long _
  54.     ) As Integer
  55. Private Declare Function SQLFreeHandle Lib "odbc32" ( _
  56.   ByRef HandleType As Integer, _
  57.   ByRef Handle As Long _
  58.     ) As Integer
  59. Private Declare Function SQLSetEnvAttrInteger Lib "odbc32" Alias "SQLSetEnvAttr" ( _
  60.   ByVal EnvironmentHandle As Long, _
  61.   ByVal Attr As Integer, _
  62.   ByVal Value As Long, _
  63.   ByVal StringLength As Integer) _
  64.     As Integer
  65. Private Declare Function SQLDataSources Lib "odbc32" ( _
  66.   ByVal EnvironmentHandle As Long, _
  67.   ByVal Direction As Integer, _
  68.   ByVal ServerName As String, _
  69.   ByVal BufferLength1 As Integer, _
  70.   ByRef NameLength1 As Integer, _
  71.   ByVal Description As String, _
  72.   ByVal BufferLength2 As Integer, _
  73.   ByRef NameLength2 As Integer _
  74.     ) As Integer
  75. Private Const SQL_SUCCESS = 0
  76. Private Const SQL_ERROR = -1
  77. Private Const SQL_HANDLE_ENV = 1
  78. Private Const SQL_ATTR_ODBC_VERSION = 200
  79. Private Const SQL_OV_ODBC2 = 2
  80. Private Const SQL_FETCH_NEXT = 1
  81. Private Const SQL_FETCH_FIRST = 2
  82. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  83. 'MappingInfo=UserControl,UserControl,-1,Enabled
  84. Public Property Get Enabled() As Boolean
  85.   Enabled = UserControl.Enabled
  86. End Property
  87. Public Property Let Enabled(ByVal New_Enabled As Boolean)
  88.    UserControl.Enabled() = New_Enabled
  89.    cboDSN.Enabled() = New_Enabled
  90.    PropertyChanged "Enabled"
  91. End Property
  92. Private Sub UserControl_InitProperties()
  93.     cboDSN.ListIndex = -1
  94.     strDriverFilter = ""
  95. End Sub
  96. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  97.     With PropBag
  98.         DSN = PropBag.ReadProperty("DSN", "")
  99.         strDriverFilter = .ReadProperty("DriverFilter", "")
  100.         UserControl.Enabled = .ReadProperty("Enabled", True)
  101.         cboDSN.Enabled = .ReadProperty("Enabled", True)
  102.     End With
  103.     Refresh
  104. End Sub
  105. Private Sub UserControl_Resize()
  106. '[Description]
  107. '   Resize the constituent controls to fit the new control size
  108. '[Code]
  109.     With UserControl
  110.         .Height = 315
  111.         cboDSN.Width = .Width
  112.     End With
  113. End Sub
  114. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  115.     With PropBag
  116.         .WriteProperty "DSN", cboDSN.Text, ""
  117.         .WriteProperty "DriverFilter", strDriverFilter, ""
  118.         .WriteProperty "Enabled", UserControl.Enabled, True
  119.         .WriteProperty "Enabled", cboDSN.Enabled, True
  120.     End With
  121. End Sub
  122. Public Property Get DSN() As String
  123.     DSN = cboDSN.Text
  124. End Property
  125. Public Property Let DSN(NewValue As String)
  126. '[Description]
  127. '   Attempt to change the DSN
  128. '   If the DSN does not appear in the list do not set and raise an error
  129. '[Code]
  130.     On Error GoTo ErrorTrap
  131.     If Len(NewValue) = 0 Then
  132.         cboDSN.ListIndex = -1
  133.     Else
  134.         cboDSN.Text = NewValue
  135.     End If
  136.     PropertyChanged "DSN"
  137.     Exit Property
  138. ErrorTrap:
  139.     'item not found in collection
  140.     cboDSN.ListIndex = -1
  141. End Property
  142. Public Function Refresh()
  143. '[Declarations]
  144. Dim strCurrentDSN           As String   'Currently selected DSN
  145. Dim hEnv                    As Long     'ODBC Environment Handle
  146. Dim intSQLReturn            As Integer
  147. Dim strServerName           As String * 255
  148. Dim intServerNameLen        As Integer
  149. Dim strDescription          As String * 255
  150. Dim intDescriptionLen       As Integer
  151. '[Code]
  152.     'Store the currently selected DSN
  153.     strCurrentDSN = cboDSN.Text
  154.     'Build a new list of available DSN
  155.     cboDSN.Clear
  156.     If SQLAllocHandle(SQL_HANDLE_ENV, 0, hEnv) = SQL_ERROR Then
  157.         'Failed to allocate Environment Handle
  158.         Err.Raise vbObjectError + 1, "EBDSNCombo_Refresh", "Unable to allocate an ODBC Environment Handle"
  159.         
  160.     Else
  161.         'We have an Environment handle
  162.         '- Inform the Driver Manager that we need ODBC2 conformance
  163.         
  164.         If SQLSetEnvAttrInteger(hEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC2, 0) = -1 Then
  165.             'Failed to set conformance level
  166.             Err.Raise vbObjectError + 2, "EBDSNCombo_Refresh", "Unable to set ODBC2 conformance"
  167.             
  168.         Else
  169.         
  170.             'We have set the conformance level
  171.             '- Fetch a list of ODBC data sources
  172.             
  173.             'Attempt to fetch first DSN
  174.             intSQLReturn = SQLDataSources(hEnv, SQL_FETCH_FIRST, strServerName, Len(strServerName), intServerNameLen, strDescription, Len(strDescription), intDescriptionLen)
  175.             
  176.             Do While intSQLReturn = SQL_SUCCESS
  177.             
  178.                 If Len(strDriverFilter) = 0 _
  179.                   Or Left(strDescription, intDescriptionLen) = strDriverFilter Then
  180.                     'This data source matches the DriverFilter property (or
  181.                     'DriverFilter not set)
  182.                     '- Add it to the list
  183.                     cboDSN.AddItem Left(strServerName, intServerNameLen)
  184.                 End If
  185.             
  186.                 'Attempt to fetch the next DSN (if any)
  187.                 intSQLReturn = SQLDataSources(hEnv, SQL_FETCH_NEXT, strServerName, Len(strServerName), intServerNameLen, strDescription, Len(strDescription), intDescriptionLen)
  188.             Loop
  189.             
  190.         End If
  191.             
  192.         'Free the environment handle
  193.         SQLFreeHandle SQL_HANDLE_ENV, hEnv
  194.     End If
  195.     'Attempt to reselect the current DSN
  196.     '(it may not be in the list any more)
  197.     DSN = strCurrentDSN
  198. End Function
  199. Private Function RaiseODBCError(hEnv As Long)
  200. '[Description]
  201. '   Displays the first ODBC error (if any)
  202. '[Notes]
  203. '   Used only for debugging purposes (not exposed)
  204. '   As the ODBC API can result in more than one error being generated,
  205. '   it is usual to keep polling the stack to retrieve all the errors.
  206. '   As this function is used purely for debugging purposes, we are only
  207. '   interested in the first error generated.
  208. '[Declarations]
  209. Dim strSQLState             As String * 5       'SQLState at time of error
  210. Dim lngErrorNo              As Long             'ODBC Error No
  211. Dim strMessage              As String * 255     'Error message text
  212. Dim intMessageLen           As Integer          'Length of error message
  213. Dim intSQLReturn            As Integer          'Return state of API call
  214. '[Code]
  215.     'Fetch first Error
  216.     intSQLReturn = SQLGetDiagRec(SQL_HANDLE_ENV, hEnv, 1, strSQLState, lngErrorNo, strMessage, Len(strMessage), intMessageLen)
  217.     If intSQLReturn = SQL_SUCCESS Then
  218.         'Display error
  219.         MsgBox Left("ODBC Error " & lngErrorNo & vbCrLf _
  220.         & strSQLState & " : " & strMessage, intMessageLen)
  221.     End If
  222. End Function
  223. Public Property Get DriverFilter() As String
  224. '[Description]
  225. '   Return the DriverFilter Property
  226. '[Code]
  227.     DriverFilter = strDriverFilter
  228. End Property
  229. Public Property Let DriverFilter(NewValue As String)
  230. '[Description]
  231. '   Set the DriverFilter property and refresh the list of available DSN
  232. '[Code]
  233.     strDriverFilter = NewValue
  234.     PropertyChanged "DriverFilter"
  235.     Refresh
  236. End Property
  237.