home *** CD-ROM | disk | FTP | other *** search
/ distrib.akp.su/Programming/Vb-6+Rus/ / distrib.akp.su.tar / distrib.akp.su / Programming / Vb-6+Rus / VB98 / TEMPLATE / FORMS / ODBCLGIN.FRM (.txt) < prev    next >
Visual Basic Form  |  1998-06-18  |  9KB  |  257 lines

  1. VERSION 5.00
  2. Begin VB.Form frmODBCLogon 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "ODBC Logon"
  5.    ClientHeight    =   3180
  6.    ClientLeft      =   2850
  7.    ClientTop       =   1755
  8.    ClientWidth     =   4470
  9.    ControlBox      =   0   'False
  10.    Icon            =   "ODBC Log In.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3180
  15.    ScaleWidth      =   4470
  16.    ShowInTaskbar   =   0   'False
  17.    StartUpPosition =   2  'CenterScreen
  18.    Begin VB.CommandButton cmdCancel 
  19.       Cancel          =   -1  'True
  20.       Caption         =   "Cancel"
  21.       Height          =   450
  22.       Left            =   2520
  23.       TabIndex        =   13
  24.       Top             =   2655
  25.       Width           =   1440
  26.    End
  27.    Begin VB.CommandButton cmdOK 
  28.       Caption         =   "&OK"
  29.       Height          =   450
  30.       Left            =   915
  31.       TabIndex        =   12
  32.       Top             =   2655
  33.       Width           =   1440
  34.    End
  35.    Begin VB.Frame fraStep3 
  36.       Caption         =   "Connection Values"
  37.       Height          =   2415
  38.       Index           =   0
  39.       Left            =   120
  40.       TabIndex        =   14
  41.       Top             =   120
  42.       Width           =   4230
  43.       Begin VB.TextBox txtUID 
  44.          Height          =   300
  45.          Left            =   1125
  46.          TabIndex        =   3
  47.          Top             =   600
  48.          Width           =   3015
  49.       End
  50.       Begin VB.TextBox txtPWD 
  51.          Height          =   300
  52.          Left            =   1125
  53.          TabIndex        =   5
  54.          Top             =   930
  55.          Width           =   3015
  56.       End
  57.       Begin VB.TextBox txtDatabase 
  58.          Height          =   300
  59.          Left            =   1125
  60.          TabIndex        =   7
  61.          Top             =   1260
  62.          Width           =   3015
  63.       End
  64.       Begin VB.ComboBox cboDSNList 
  65.          Height          =   315
  66.          ItemData        =   "ODBC Log In.frx":000C
  67.          Left            =   1125
  68.          List            =   "ODBC Log In.frx":000E
  69.          Sorted          =   -1  'True
  70.          Style           =   2  'Dropdown List
  71.          TabIndex        =   1
  72.          Top             =   240
  73.          Width           =   3000
  74.       End
  75.       Begin VB.TextBox txtServer 
  76.          Enabled         =   0   'False
  77.          Height          =   330
  78.          Left            =   1125
  79.          TabIndex        =   11
  80.          Top             =   1935
  81.          Width           =   3015
  82.       End
  83.       Begin VB.ComboBox cboDrivers 
  84.          Enabled         =   0   'False
  85.          Height          =   315
  86.          Left            =   1125
  87.          Sorted          =   -1  'True
  88.          Style           =   2  'Dropdown List
  89.          TabIndex        =   9
  90.          Top             =   1590
  91.          Width           =   3015
  92.       End
  93.       Begin VB.Label lblStep3 
  94.          AutoSize        =   -1  'True
  95.          Caption         =   "&DSN:"
  96.          Height          =   195
  97.          Index           =   1
  98.          Left            =   135
  99.          TabIndex        =   0
  100.          Top             =   285
  101.          Width           =   390
  102.       End
  103.       Begin VB.Label lblStep3 
  104.          AutoSize        =   -1  'True
  105.          Caption         =   "&UID:"
  106.          Height          =   195
  107.          Index           =   2
  108.          Left            =   135
  109.          TabIndex        =   2
  110.          Top             =   630
  111.          Width           =   330
  112.       End
  113.       Begin VB.Label lblStep3 
  114.          AutoSize        =   -1  'True
  115.          Caption         =   "&Password:"
  116.          Height          =   195
  117.          Index           =   3
  118.          Left            =   135
  119.          TabIndex        =   4
  120.          Top             =   975
  121.          Width           =   735
  122.       End
  123.       Begin VB.Label lblStep3 
  124.          AutoSize        =   -1  'True
  125.          Caption         =   "Data&base:"
  126.          Height          =   195
  127.          Index           =   4
  128.          Left            =   135
  129.          TabIndex        =   6
  130.          Top             =   1320
  131.          Width           =   735
  132.       End
  133.       Begin VB.Label lblStep3 
  134.          AutoSize        =   -1  'True
  135.          Caption         =   "Dri&ver:"
  136.          Height          =   195
  137.          Index           =   5
  138.          Left            =   135
  139.          TabIndex        =   8
  140.          Top             =   1665
  141.          Width           =   465
  142.       End
  143.       Begin VB.Label lblStep3 
  144.          AutoSize        =   -1  'True
  145.          Caption         =   "&Server:"
  146.          Height          =   195
  147.          Index           =   6
  148.          Left            =   135
  149.          TabIndex        =   10
  150.          Top             =   2010
  151.          Width           =   510
  152.       End
  153.    End
  154. Attribute VB_Name = "frmODBCLogon"
  155. Attribute VB_GlobalNameSpace = False
  156. Attribute VB_Creatable = False
  157. Attribute VB_PredeclaredId = True
  158. Attribute VB_Exposed = False
  159. Option Explicit
  160. Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
  161. Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
  162. Const SQL_SUCCESS As Long = 0
  163. Const SQL_FETCH_NEXT As Long = 1
  164. Private Sub cmdCancel_Click()
  165.     Unload Me
  166. End Sub
  167. Private Sub cmdOK_Click()
  168.     Dim sConnect    As String
  169.     Dim sADOConnect As String
  170.     Dim sDAOConnect As String
  171.     Dim sDSN        As String
  172.     If cboDSNList.ListIndex > 0 Then
  173.         sDSN = "DSN=" & cboDSNList.Text & ";"
  174.     Else
  175.         sConnect = sConnect & "Driver=" & cboDrivers.Text & ";"
  176.         sConnect = sConnect & "Server=" & txtServer.Text & ";"
  177.     End If
  178.     sConnect = sConnect & "UID=" & txtUID.Text & ";"
  179.     sConnect = sConnect & "PWD=" & txtPWD.Text & ";"
  180.     If Len(txtDatabase.Text) > 0 Then
  181.         sConnect = sConnect & "Database=" & txtDatabase.Text & ";"
  182.     End If
  183.     sADOConnect = "PROVIDER=MSDASQL;" & sDSN & sConnect
  184.     sDAOConnect = "ODBC;" & sDSN & sConnect
  185.     MsgBox _
  186.     "To open an ADO Connection, use:" & vbCrLf & _
  187.     "Set gConnection = New Connection" & vbCrLf & _
  188.     "gConnection.Open """ & sADOConnect & """" & vbCrLf & vbCrLf & _
  189.     "To open a DAO database object, use:" & vbCrLf & _
  190.     "Set gDatabase = OpenDatabase(vbNullString, 0, 0, sDAOConnect)" & vbCrLf & vbCrLf & _
  191.     "Or to open an RDO Connection, use:" & vbCrLf & _
  192.     "Set gRDOConnection = rdoEnvironments(0).OpenConnection(sDSN, rdDriverNoPrompt, 0, sConnect)"
  193.     'ADO:
  194.     'Set gConnection = New Connection
  195.     'gConnection.Open sADOConnect
  196.     'DAO:
  197.     'Set gDatabase = OpenDatabase(vbNullString, 0, 0, sDAOConnect)
  198.     'RDO:
  199.     'Set gRDOConnection = rdoEnvironments(0).OpenConnection(sDSN, rdDriverNoPrompt, 0, sConnect)
  200. End Sub
  201. Private Sub Form_Load()
  202.     GetDSNsAndDrivers
  203. End Sub
  204. Private Sub cboDSNList_Click()
  205.     On Error Resume Next
  206.     If cboDSNList.Text = "(None)" Then
  207.         txtServer.Enabled = True
  208.         cboDrivers.Enabled = True
  209.     Else
  210.         txtServer.Enabled = False
  211.         cboDrivers.Enabled = False
  212.     End If
  213. End Sub
  214. Sub GetDSNsAndDrivers()
  215.     Dim i As Integer
  216.     Dim sDSNItem As String * 1024
  217.     Dim sDRVItem As String * 1024
  218.     Dim sDSN As String
  219.     Dim sDRV As String
  220.     Dim iDSNLen As Integer
  221.     Dim iDRVLen As Integer
  222.     Dim lHenv As Long         'handle to the environment
  223.     On Error Resume Next
  224.     cboDSNList.AddItem "(None)"
  225.     'get the DSNs
  226.     If SQLAllocEnv(lHenv) <> -1 Then
  227.         Do Until i <> SQL_SUCCESS
  228.             sDSNItem = Space$(1024)
  229.             sDRVItem = Space$(1024)
  230.             i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
  231.             sDSN = Left$(sDSNItem, iDSNLen)
  232.             sDRV = Left$(sDRVItem, iDRVLen)
  233.                 
  234.             If sDSN <> Space(iDSNLen) Then
  235.                 cboDSNList.AddItem sDSN
  236.                 cboDrivers.AddItem sDRV
  237.             End If
  238.         Loop
  239.     End If
  240.     'remove the dupes
  241.     If cboDSNList.ListCount > 0 Then
  242.         With cboDrivers
  243.             If .ListCount > 1 Then
  244.                 i = 0
  245.                 While i < .ListCount
  246.                     If .List(i) = .List(i + 1) Then
  247.                         .RemoveItem (i)
  248.                     Else
  249.                         i = i + 1
  250.                     End If
  251.                 Wend
  252.             End If
  253.         End With
  254.     End If
  255.     cboDSNList.ListIndex = 0
  256. End Sub
  257.