home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / PVb5.0 / VB / SAMPLES / VISDATA / ODBCLOGN.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-13  |  12.8 KB  |  427 lines

  1. VERSION 5.00
  2. Begin VB.Form frmODBCLogon 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "ODBC 
  5.    ClientHeight    =   3180
  6.    ClientLeft      =   2850
  7.    ClientTop       =   1755
  8.    ClientWidth     =   4470
  9.    ControlBox      =   0   'False
  10.    HelpContextID   =   2016138
  11.    Icon            =   "ODBCLogn.frx":0000
  12.    LinkTopic       =   "Form1"
  13.    LockControls    =   -1  'True
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   3180
  17.    ScaleWidth      =   4470
  18.    ShowInTaskbar   =   0   'False
  19.    StartUpPosition =   2  'CenterScreen
  20.    Begin VB.CommandButton cmdRegister 
  21.       Caption         =   "
  22. (&R)"
  23.       Height          =   450
  24.       Left            =   120
  25.       MaskColor       =   &H00000000&
  26.       TabIndex        =   15
  27.       Top             =   2655
  28.       Width           =   1440
  29.    End
  30.    Begin VB.CommandButton cmdCancel 
  31.       Cancel          =   -1  'True
  32.       Caption         =   "
  33.       Height          =   450
  34.       Left            =   3075
  35.       MaskColor       =   &H00000000&
  36.       TabIndex        =   13
  37.       Top             =   2655
  38.       Width           =   1260
  39.    End
  40.    Begin VB.CommandButton cmdOK 
  41.       Caption         =   "
  42. (&O)"
  43.       Default         =   -1  'True
  44.       Height          =   450
  45.       Left            =   1740
  46.       MaskColor       =   &H00000000&
  47.       TabIndex        =   12
  48.       Top             =   2655
  49.       Width           =   1260
  50.    End
  51.    Begin VB.Frame fraConnection 
  52.       Caption         =   "
  53.       Height          =   2415
  54.       Left            =   120
  55.       TabIndex        =   14
  56.       Top             =   120
  57.       Width           =   4230
  58.       Begin VB.TextBox txtUID 
  59.          Height          =   300
  60.          Left            =   1300
  61.          TabIndex        =   3
  62.          Top             =   600
  63.          Width           =   2840
  64.       End
  65.       Begin VB.TextBox txtPWD 
  66.          Height          =   300
  67.          IMEMode         =   3  'DISABLE
  68.          Left            =   1300
  69.          PasswordChar    =   "*"
  70.          TabIndex        =   5
  71.          Top             =   930
  72.          Width           =   2840
  73.       End
  74.       Begin VB.TextBox txtDatabase 
  75.          Height          =   300
  76.          Left            =   1300
  77.          TabIndex        =   7
  78.          Top             =   1260
  79.          Width           =   2840
  80.       End
  81.       Begin VB.ComboBox cboDSNList 
  82.          Height          =   300
  83.          ItemData        =   "ODBCLogn.frx":000C
  84.          Left            =   1300
  85.          List            =   "ODBCLogn.frx":000E
  86.          Sorted          =   -1  'True
  87.          TabIndex        =   1
  88.          Text            =   "*"
  89.          Top             =   240
  90.          Width           =   2825
  91.       End
  92.       Begin VB.TextBox txtServer 
  93.          Enabled         =   0   'False
  94.          Height          =   330
  95.          Left            =   1300
  96.          TabIndex        =   11
  97.          Top             =   1935
  98.          Width           =   2840
  99.       End
  100.       Begin VB.ComboBox cboDrivers 
  101.          Enabled         =   0   'False
  102.          Height          =   300
  103.          ItemData        =   "ODBCLogn.frx":0010
  104.          Left            =   1300
  105.          List            =   "ODBCLogn.frx":0012
  106.          Sorted          =   -1  'True
  107.          Style           =   2  'Dropdown List
  108.          TabIndex        =   9
  109.          Top             =   1590
  110.          Width           =   2825
  111.       End
  112.       Begin VB.Label lblLabels 
  113.          AutoSize        =   -1  'True
  114.          Caption         =   "&DSN
  115.          Height          =   195
  116.          Index           =   0
  117.          Left            =   135
  118.          TabIndex        =   0
  119.          Top             =   285
  120.          Width           =   1160
  121.       End
  122.       Begin VB.Label lblLabels 
  123.          AutoSize        =   -1  'True
  124.          Caption         =   "&UID
  125.          Height          =   195
  126.          Index           =   1
  127.          Left            =   135
  128.          TabIndex        =   2
  129.          Top             =   630
  130.          Width           =   1160
  131.       End
  132.       Begin VB.Label lblLabels 
  133.          AutoSize        =   -1  'True
  134.          Caption         =   "
  135.          Height          =   195
  136.          Index           =   2
  137.          Left            =   135
  138.          TabIndex        =   4
  139.          Top             =   975
  140.          Width           =   1160
  141.       End
  142.       Begin VB.Label lblLabels 
  143.          AutoSize        =   -1  'True
  144.          Caption         =   "
  145.          Height          =   195
  146.          Index           =   3
  147.          Left            =   135
  148.          TabIndex        =   6
  149.          Top             =   1320
  150.          Width           =   1160
  151.       End
  152.       Begin VB.Label lblLabels 
  153.          AutoSize        =   -1  'True
  154.          Caption         =   "
  155.          Enabled         =   0   'False
  156.          Height          =   195
  157.          Index           =   4
  158.          Left            =   135
  159.          TabIndex        =   8
  160.          Top             =   1665
  161.          Width           =   1160
  162.       End
  163.       Begin VB.Label lblLabels 
  164.          AutoSize        =   -1  'True
  165.          Caption         =   "
  166.          Enabled         =   0   'False
  167.          Height          =   195
  168.          Index           =   5
  169.          Left            =   135
  170.          TabIndex        =   10
  171.          Top             =   2010
  172.          Width           =   1160
  173.       End
  174.    End
  175. Attribute VB_Name = "frmODBCLogon"
  176. Attribute VB_GlobalNameSpace = False
  177. Attribute VB_Creatable = False
  178. Attribute VB_PredeclaredId = True
  179. Attribute VB_Exposed = False
  180. Option Explicit
  181. '>>>>>>>>>>>>>>>>>>>>>>>>
  182. Const FORMCAPTION = "ODBC 
  183. Const BUTTON1 = "
  184. (&O)"
  185. Const BUTTON2 = "
  186. (&C)"
  187. Const BUTTON3 = "
  188. (&R)"
  189. Const FRAME1 = "
  190. Const Label1 = "&DSN:"
  191. Const Label2 = "&UID:"
  192. Const LABEL3 = "
  193. Const LABEL4 = "
  194. Const LABEL5 = "
  195. Const LABEL6 = "
  196. Const MSG1 = "
  197.  ODBC 
  198. Const MSG2 = "
  199.  ODBC 
  200. Const MSG3 = "
  201.  ODBCINST.INI 
  202. Const MSG4 = "
  203. Const MSG5 = "
  204. Const MSG7 = "
  205. Const MSG8 = "
  206. Const MSG9 = "
  207. Const MSG10 = "
  208. Const MSG11 = "
  209. Const MSG12 = "
  210. '>>>>>>>>>>>>>>>>>>>>>>>>
  211. Dim mbBeenLoaded As Integer
  212. Public DBOpened As Boolean
  213. Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv&, ByVal fDirection%, ByVal szDSN$, ByVal cbDSNMax%, pcbDSN%, ByVal szDescription$, ByVal cbDescriptionMax%, pcbDescription%) As Integer
  214. Private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
  215. Const SQL_SUCCESS As Long = 0
  216. Const SQL_FETCH_NEXT As Long = 1
  217. Private Sub cboDSNList_Change()
  218.   If Len(cboDSNList.Text) = 0 Or cboDSNList.Text = "
  219. " Then
  220.     txtServer.Enabled = True
  221.     cboDrivers.Enabled = True
  222.     lblLabels(4).Enabled = True
  223.     lblLabels(5).Enabled = True
  224.   Else
  225.     txtServer.Enabled = False
  226.     cboDrivers.Enabled = False
  227.     lblLabels(4).Enabled = False
  228.     lblLabels(5).Enabled = False
  229.   End If
  230. End Sub
  231. Private Sub cmdCancel_Click()
  232.   gbDBOpenFlag = False
  233.   gsDBName = vbNullString
  234.   DBOpened = False
  235.   Me.Hide
  236. End Sub
  237. Private Sub cmdOK_Click()
  238.   On Error GoTo cmdOK_ClickErr
  239.   Dim sConnect As String
  240.   Dim dbTemp As Database
  241.   MsgBar MSG2, True
  242.   If frmMDI.mnuPOpenOnStartup.Checked Then
  243.     Me.Refresh
  244.   End If
  245.   Screen.MousePointer = vbHourglass
  246.   If Len(cboDSNList.Text) > 0 Then
  247.     sConnect = "ODBC;DSN=" & cboDSNList.Text & ";"
  248.   Else
  249.     sConnect = "ODBC;Driver={" & cboDrivers.Text & "};"
  250.     sConnect = sConnect & "Server=" & txtServer.Text & ";"
  251.   End If
  252.   sConnect = sConnect & "UID=" & txtUID.Text & ";"
  253.   sConnect = sConnect & "PWD=" & txtPWD.Text & ";"
  254.   If Len(txtDatabase.Text) > 0 Then
  255.     sConnect = sConnect & "Database=" & txtDatabase.Text & ";"
  256.   End If
  257.   Set dbTemp = gwsMainWS.OpenDatabase("", 0, 0, sConnect)
  258.   If gbDBOpenFlag Then
  259.     CloseCurrentDB
  260.     If gbDBOpenFlag Then
  261.       Beep
  262.       MsgBox MSG12, 48
  263.       Me.Hide
  264.       Exit Sub
  265.     End If
  266.   End If
  267.   DBOpened = True
  268.   gsODBCDatasource = cboDSNList.Text
  269.   gsDBName = gsODBCDatasource
  270.   gsODBCDatabase = txtDatabase.Text
  271.   gsODBCUserName = txtUID.Text
  272.   gsODBCPassword = txtPWD.Text
  273.   gsODBCDriver = cboDrivers.Text
  274.   gsODBCServer = txtServer.Text
  275.   gsDataType = gsSQLDB
  276.   Set gdbCurrentDB = dbTemp
  277.   GetODBCConnectParts gdbCurrentDB.Connect
  278.   cboDSNList.Text = gsODBCDatasource
  279.   txtDatabase.Text = gsODBCDatabase
  280.   txtUID.Text = gsODBCUserName
  281.   txtPWD.Text = gsODBCPassword
  282.   frmMDI.Caption = "VisData:" & gsDBName & "." & gsODBCDatabase
  283.   gdbCurrentDB.QueryTimeout = glQueryTimeout
  284.   gbDBOpenFlag = True
  285.   AddMRU
  286.   Screen.MousePointer = vbDefault
  287.   Me.Hide
  288.   Exit Sub
  289. cmdOK_ClickErr:
  290.   Screen.MousePointer = vbDefault
  291.   gbDBOpenFlag = False
  292.   If Len(cboDSNList.Text) > 0 Then
  293.     If InStr(1, Error, "ODBC--connection to '" & cboDSNList.Text & "' failed") > 0 Then
  294.       Beep
  295.       MsgBox MSG5, 48
  296.       txtDatabase.Text = vbNullString
  297.       txtUID.Text = vbNullString
  298.       txtPWD.Text = vbNullString
  299.       If RegisterDB((cboDSNList.Text)) Then
  300.         MsgBox MSG9, 48
  301.       End If
  302.     ElseIf InStr(1, Error, "Login failed") > 0 Then
  303.       Beep
  304.       MsgBox MSG7, 48
  305.     ElseIf InStr(1, Error, "QueryTimeout property") > 0 Then
  306.       If glQueryTimeout <> 5 Then
  307.         Beep
  308.         MsgBox MSG8, 48
  309.       End If
  310.       Resume Next
  311.     Else
  312.       ShowError
  313.     End If
  314.   End If
  315.   MsgBar MSG1, False
  316.   If Err = 3059 Then
  317.     Unload Me
  318.   End If
  319. End Sub
  320. Private Sub cmdRegister_Click()
  321.   On Error GoTo cmdRegister_ClickErr
  322.   If Len(cboDSNList.Text) = 0 Then
  323.     MsgBox MSG10, vbInformation, Me.Caption
  324.     Exit Sub
  325.   End If
  326.   If Len(cboDrivers.Text) = 0 Then
  327.     MsgBox MSG11, vbInformation, Me.Caption
  328.     Exit Sub
  329.   End If
  330.   DBEngine.RegisterDatabase cboDSNList.Text, cboDrivers.Text, False, vbNullString
  331.   MsgBox MSG9, vbInformation
  332.   Exit Sub
  333. cmdRegister_ClickErr:
  334.   ShowError
  335. End Sub
  336. Private Sub Form_Load()
  337.   Dim i As Integer
  338.   Me.Caption = FORMCAPTION
  339.   cmdOK.Caption = BUTTON1
  340.   cmdCancel.Caption = BUTTON2
  341.   cmdRegister.Caption = BUTTON3
  342.   fraConnection.Caption = FRAME1
  343.   lblLabels(0).Caption = Label1
  344.   lblLabels(1).Caption = Label2
  345.   lblLabels(2).Caption = LABEL3
  346.   lblLabels(3).Caption = LABEL4
  347.   lblLabels(4).Caption = LABEL5
  348.   lblLabels(5).Caption = LABEL6
  349.   GetDSNsAndDrivers
  350.   MsgBar MSG1, False
  351.   cboDSNList.Text = gsODBCDatasource
  352.   txtDatabase.Text = gsODBCDatabase
  353.   txtUID.Text = gsODBCUserName
  354.   txtPWD.Text = gsODBCPassword
  355.   If Len(gsODBCDriver) > 0 Then
  356.     For i = 0 To cboDrivers.ListCount - 1
  357.       If cboDrivers.List(i) = gsODBCDriver Then
  358.         cboDrivers.ListIndex = i
  359.         Exit For
  360.       End If
  361.     Next
  362.   End If
  363.   txtServer.Text = gsODBCServer
  364.   mbBeenLoaded = True
  365. End Sub
  366. Private Sub cboDSNList_Click()
  367.   cboDSNList_Change
  368. End Sub
  369. Sub GetDSNsAndDrivers()
  370.   On Error Resume Next
  371.   Dim i As Integer
  372.   Dim sDSNItem As String * 1024
  373.   Dim sDRVItem As String * 1024
  374.   Dim sDSN As String
  375.   Dim sDRV As String
  376.   Dim iDSNLen As Integer
  377.   Dim iDRVLen As Integer
  378.   Dim lHenv As Long     '
  379.   cboDSNList.AddItem "
  380.   If SQLAllocEnv(lHenv) <> -1 Then
  381.     Do Until i <> SQL_SUCCESS
  382.       sDSNItem = Space(1024)
  383.       sDRVItem = Space(1024)
  384.       i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, iDSNLen, sDRVItem, 1024, iDRVLen)
  385.       sDSN = VBA.Left(sDSNItem, iDSNLen)
  386.       sDRV = VBA.Left(sDRVItem, iDRVLen)
  387.         
  388.       If sDSN <> Space(iDSNLen) Then
  389.         cboDSNList.AddItem sDSN
  390.         cboDrivers.AddItem sDRV
  391.       End If
  392.     Loop
  393.   End If
  394.   If cboDSNList.ListCount > 0 Then
  395.     With cboDrivers
  396.       If .ListCount > 1 Then
  397.         i = 0
  398.         While i < .ListCount
  399.           If .List(i) = .List(i + 1) Then
  400.             .RemoveItem (i)
  401.           Else
  402.             i = i + 1
  403.           End If
  404.         Wend
  405.       End If
  406.     End With
  407.   End If
  408.   cboDSNList.ListIndex = 0
  409. End Sub
  410. Private Sub Form_Unload(Cancel As Integer)
  411.   MsgBar vbNullString, False
  412. End Sub
  413. Private Function RegisterDB(rsDatasource As String) As Integer
  414.    On Error GoTo RDBErr
  415.    Dim sDriver As String
  416.    sDriver = InputBox(MSG3, MSG4, gsDEFAULT_DRIVER)
  417.    If sDriver <> gsDEFAULT_DRIVER Then
  418.      DBEngine.RegisterDatabase rsDatasource, sDriver, False, vbNullString
  419.    Else
  420.      DBEngine.RegisterDatabase rsDatasource, sDriver, True, vbNullString
  421.    End If
  422.    RegisterDB = True
  423.    Exit Function
  424. RDBErr:
  425.    RegisterDB = False
  426. End Function
  427.