home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / lstfocus / lostfocu.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-03-09  |  6.7 KB  |  163 lines

  1. VERSION 2.00
  2. Begin Form LOSTFOCUS 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "LostFocus Solution / Field Validation Example"
  5.    ClientHeight    =   2415
  6.    ClientLeft      =   975
  7.    ClientTop       =   1530
  8.    ClientWidth     =   6525
  9.    Height          =   2790
  10.    Left            =   930
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   2415
  13.    ScaleWidth      =   6525
  14.    Top             =   1200
  15.    Width           =   6615
  16.    Begin CommandButton Cmd_Save 
  17.       Caption         =   "&Save"
  18.       Default         =   -1  'True
  19.       Height          =   510
  20.       Left            =   4635
  21.       TabIndex        =   7
  22.       Top             =   270
  23.       Width           =   1050
  24.    End
  25.    Begin CommandButton Cmd_Cancel 
  26.       Cancel          =   -1  'True
  27.       Caption         =   "&Cancel"
  28.       Height          =   510
  29.       Left            =   4635
  30.       TabIndex        =   6
  31.       Top             =   945
  32.       Width           =   1050
  33.    End
  34.    Begin ComboBox Cbo_Title 
  35.       Height          =   300
  36.       Left            =   1530
  37.       Sorted          =   -1  'True
  38.       TabIndex        =   2
  39.       Top             =   1575
  40.       Width           =   1770
  41.    End
  42.    Begin TextBox Txt_Forename 
  43.       Height          =   375
  44.       Left            =   1530
  45.       TabIndex        =   1
  46.       Top             =   945
  47.       Width           =   1770
  48.    End
  49.    Begin TextBox Txt_Surname 
  50.       Height          =   375
  51.       Left            =   1530
  52.       TabIndex        =   0
  53.       Top             =   270
  54.       Width           =   1770
  55.    End
  56.    Begin Label Lbl_Forename 
  57.       BackColor       =   &H00C0C0C0&
  58.       Caption         =   "Forename"
  59.       Height          =   195
  60.       Left            =   270
  61.       TabIndex        =   5
  62.       Top             =   1035
  63.       Width           =   1005
  64.    End
  65.    Begin Label Lbl_Surname 
  66.       BackColor       =   &H00C0C0C0&
  67.       Caption         =   "Surname"
  68.       Height          =   195
  69.       Left            =   270
  70.       TabIndex        =   4
  71.       Top             =   360
  72.       Width           =   1005
  73.    End
  74.    Begin Label Lbl_Title 
  75.       BackColor       =   &H00C0C0C0&
  76.       Caption         =   "Title"
  77.       Height          =   195
  78.       Left            =   270
  79.       TabIndex        =   3
  80.       Top             =   1620
  81.       Width           =   1005
  82.    End
  83. Option Explicit
  84. '---------------------------------------------------------------------------
  85. 'This example form was uploaded by Nigel Price 100063,3363 for anyone to view
  86. 'and use/modify etc. It shows a method for validating fields using the LostFocus
  87. 'event. This has been the cause of a lot of problems and discussion on MSBASIC.
  88. 'This solution allows a Cancel button to work either by clicking, pressing
  89. 'the default Esc key, or by using the Alt-C accelerator key. The validation routine
  90. 'is very basic just to demonstrate the technique. The user is able to move
  91. 'from field to field if the field validation is ok. If it is not the user
  92. 'is shown a message and then they are returned to that field. The user can
  93. 'choose to Cancel at any time using the Cancel button. I have tested the
  94. 'technique with many different controls including 3rd party ones. I hope it
  95. 'helps anyone trying to do field level validation. I have extended this basic
  96. 'technique to include a form level validation routine which calls this for
  97. 'each field and builds up a 'List of Field problems' which the user can view.
  98. 'They can then deal with each problem. Basically that Form level validation loops
  99. 'through the Controls collection and calls this (or rather a modified version!)
  100. 'procedure. I have not uploaded that version because it requires the use of
  101. 'the CtlName() function which comes with Jonathan Zucks VBZ Electronic magazine
  102. 'If anyone has any comments (constructive ones!) or problems feel free to
  103. 'send me a message.
  104. '---------------------------------------------------------------------------
  105. Dim sm_LostFocus_Control As String   'Holds name of Current Control which has a validation error
  106. Sub Cbo_Title_LostFocus ()
  107. Field_LostFocus Cbo_Title, "Cbo_Title"
  108. End Sub
  109. Sub Cmd_Cancel_Click ()
  110. Unload Me
  111. End Sub
  112. Sub Cmd_Save_Click ()
  113. MsgBox "Save the Record Here"
  114. End Sub
  115. Sub Field_LostFocus (Current_Control As Control, Control_Name As String)
  116. 'Note : If you subscribe to VBZ an Electronic magazine for VB there is a function
  117. '       called CtlName() which can save having to pass the Control_Name parameter.
  118. '       You can then use CtlName(Current_Control) to get the name of the current
  119. '       control. This saves one parameter.
  120. If LOSTFOCUS.ActiveControl <> LOSTFOCUS!Cmd_Cancel Then     'User clicked on a control other than the Cmd_Cancel button
  121.     Control_Name = UCase$(Control_Name)                     'Upper case the Control Name which called this Sub
  122.     If Control_Name = UCase$(sm_LostFocus_Control) Or sm_LostFocus_Control = "" Then   'Is a LostFocus Validation already occuring?
  123.         Select Case Control_Name
  124.             Case "TXT_SURNAME"      'Validate Txt_Surname Here
  125.                 If LOSTFOCUS!Txt_Surname.Text = "" Then
  126.                     sm_LostFocus_Control = Control_Name
  127.                     MsgBox "The Surname cannot be Blank, Please re-enter it"
  128.                     Current_Control.SetFocus
  129.                 Else                    'All Validation OK for Txt_Surname
  130.                     sm_LostFocus_Control = ""
  131.                 End If
  132.             Case "TXT_FORENAME"     'Validate Txt_Forename Here
  133.                 If LOSTFOCUS!Txt_Forename.Text = "" Then
  134.                     sm_LostFocus_Control = Control_Name
  135.                     MsgBox "The Forename cannot be Blank, Please re-enter it"
  136.                     Current_Control.SetFocus
  137.                 Else                    'All Validation OK for Txt_Forename
  138.                     sm_LostFocus_Control = ""
  139.                 End If
  140.             Case "CBO_TITLE"
  141.             
  142.                 If LOSTFOCUS!Cbo_Title.Text = "" Then
  143.                     sm_LostFocus_Control = Control_Name
  144.                     MsgBox "The Title must be entered / selected from Combo Box List"
  145.                     Current_Control.SetFocus
  146.                 Else                    'All Validation OK for Cbo_Title
  147.                     sm_LostFocus_Control = ""
  148.                 End If
  149.         End Select
  150.     End If
  151.     sm_LostFocus_Control = ""           'Clear the LostFocus Control Name as were Cancelling
  152. End If
  153. End Sub
  154. Sub Form_Load ()
  155. sm_LostFocus_Control = ""
  156. End Sub
  157. Sub Txt_Forename_LostFocus ()
  158. Field_LostFocus Txt_Forename, "Txt_Forename"
  159. End Sub
  160. Sub Txt_Surname_LostFocus ()
  161. Field_LostFocus Txt_Surname, "Txt_Surname"
  162. End Sub
  163.