home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 August / Chip_1999-08_cd.bin / msdn / VSTUDIO / MSDE / msde_vb6.exe / frmLogin.frm (.txt) next >
Visual Basic Form  |  1999-05-12  |  6KB  |  182 lines

  1. VERSION 5.00
  2. Begin VB.Form frmLogin 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Login"
  5.    ClientHeight    =   1875
  6.    ClientLeft      =   1575
  7.    ClientTop       =   3420
  8.    ClientWidth     =   5505
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   1107.812
  13.    ScaleMode       =   0  'User
  14.    ScaleWidth      =   5168.896
  15.    ShowInTaskbar   =   0   'False
  16.    Begin VB.OptionButton optConnectionType 
  17.       Caption         =   "DAO"
  18.       Height          =   255
  19.       Index           =   1
  20.       Left            =   360
  21.       TabIndex        =   7
  22.       Top             =   720
  23.       Width           =   735
  24.    End
  25.    Begin VB.OptionButton optConnectionType 
  26.       Caption         =   "ADO"
  27.       Height          =   255
  28.       Index           =   0
  29.       Left            =   360
  30.       TabIndex        =   6
  31.       Top             =   360
  32.       Value           =   -1  'True
  33.       Width           =   735
  34.    End
  35.    Begin VB.TextBox txtUserName 
  36.       Height          =   345
  37.       Left            =   2850
  38.       TabIndex        =   1
  39.       Top             =   240
  40.       Width           =   2325
  41.    End
  42.    Begin VB.CommandButton cmdOK 
  43.       Caption         =   "OK"
  44.       Default         =   -1  'True
  45.       Height          =   390
  46.       Left            =   1560
  47.       TabIndex        =   4
  48.       Top             =   1320
  49.       Width           =   1140
  50.    End
  51.    Begin VB.CommandButton cmdCancel 
  52.       Cancel          =   -1  'True
  53.       Caption         =   "Cancel"
  54.       Height          =   390
  55.       Left            =   3000
  56.       TabIndex        =   5
  57.       Top             =   1320
  58.       Width           =   1140
  59.    End
  60.    Begin VB.TextBox txtPassword 
  61.       Height          =   345
  62.       IMEMode         =   3  'DISABLE
  63.       Left            =   2850
  64.       PasswordChar    =   "*"
  65.       TabIndex        =   3
  66.       Top             =   705
  67.       Width           =   2325
  68.    End
  69.    Begin VB.Frame Frame1 
  70.       Caption         =   "Connection Type"
  71.       Height          =   975
  72.       Left            =   120
  73.       TabIndex        =   8
  74.       Top             =   120
  75.       Width           =   1575
  76.    End
  77.    Begin VB.Label lblLabels 
  78.       Alignment       =   1  'Right Justify
  79.       Caption         =   "&User Name:"
  80.       Height          =   270
  81.       Index           =   0
  82.       Left            =   1890
  83.       TabIndex        =   0
  84.       Top             =   255
  85.       Width           =   855
  86.    End
  87.    Begin VB.Label lblLabels 
  88.       Alignment       =   1  'Right Justify
  89.       Caption         =   "&Password:"
  90.       Height          =   270
  91.       Index           =   1
  92.       Left            =   1905
  93.       TabIndex        =   2
  94.       Top             =   705
  95.       Width           =   840
  96.    End
  97. Attribute VB_Name = "frmLogin"
  98. Attribute VB_GlobalNameSpace = False
  99. Attribute VB_Creatable = False
  100. Attribute VB_PredeclaredId = True
  101. Attribute VB_Exposed = False
  102. Option Explicit
  103. Private Sub cmdCancel_Click()
  104.     'Set the global variable to false
  105.     'to denote a failed login.
  106.     LoginSucceeded = False
  107.     Me.Hide
  108. End Sub
  109. Private Sub cmdOK_Click()
  110.     SelectedConnectionType = Not optConnectionType(ConnectionType.ADO).Value
  111.     'Check for correct password.
  112.     If AttemptConnection Then
  113.         'Place code to here to pass the success to the calling sub.
  114.         'Setting a global variable is the easiest method.
  115.         LoginSucceeded = True
  116.         Me.Hide
  117.     Else
  118.         MsgBox "Login falied. Make sure user name and password are correct.", vbOKOnly + vbCritical, "Login"
  119.         txtPassword.SetFocus
  120.         SendKeys "{Home}+{End}"
  121.     End If
  122. End Sub
  123. Private Function AttemptConnection() As Boolean
  124.     ' This function attempts to login to database to make sure
  125.     ' that connection credential are correct.  This function also
  126.     ' demonstrates use of an ADO connection object.
  127.     On Error GoTo ErrorTrap
  128.     ' Build ADO and DAO conneciton strings.
  129.     ' ADO will use OLE-DB provider; DAO uses ODBCDirect.
  130.     ' NOTE:
  131.     ' This connection assumes that you have set up and ODBC data source
  132.     ' called "NorthwindMSDE" and are connecting to a database called "NorthwindCS."
  133.     ' NorthwindCS is sample MSDE database included with Microsoft Access/2000.
  134.     ADOConnectionString = "Provider=MSDASQL;Persist Security Info=False;User ID=" & txtUserName.Text & ";Password=" & txtPassword.Text & ";Initial Catalog=NorthwindCS;Data Source=NorthwindMSDE;Connect Timeout=15"
  135.     DAOConnectionString = "ODBC;DATABASE=NorthwindCS;UID=" & txtUserName.Text & ";PWD=" & txtPassword.Text & ";DSN=NorthwindMSDE"
  136.     Select Case SelectedConnectionType
  137.         
  138.         Case ConnectionType.ADO
  139.             Dim adoConnection As ADODB.Connection
  140.             
  141.             Set adoConnection = CreateObject("ADODB.Connection")
  142.             
  143.             ' Build a connection object. If no errors
  144.             ' result, we'll assume that the connection worked
  145.             ' and user credentials are valid.
  146.             With adoConnection
  147.                     .ConnectionString = ADOConnectionString
  148.                     .Open
  149.                     AttemptConnection = True
  150.                     .Close
  151.             End With
  152.             
  153.             ' Clean up.
  154.             Set adoConnection = Nothing
  155.             
  156.         Case ConnectionType.DAO
  157.         
  158.             Dim wrkODBC As DAO.Workspace
  159.             Dim daoConnection As DAO.Connection
  160.             
  161.             ' Build a workspace object and then build a connection object.
  162.             ' If no errors result, we'll assume that the connection worked
  163.             ' and user credentials are valid.
  164.             Set wrkODBC = CreateWorkspace("NewODBCWorkspace", "admin", "", dbUseODBC)
  165.             Set daoConnection = wrkODBC.OpenConnection("Northwind", , False, DAOConnectionString)
  166.             
  167.             AttemptConnection = True
  168.             
  169.             ' Clean up.
  170.             daoConnection.Close
  171.             wrkODBC.Close
  172.             
  173.             Set daoConnection = Nothing
  174.             Set wrkODBC = Nothing
  175.         
  176.     End Select
  177. Exit Function
  178. ErrorTrap:
  179. ' Account for errors here.
  180. AttemptConnection = False
  181. End Function
  182.