home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / lstfocus / lostfocu.frm next >
Text File  |  1994-03-09  |  7KB  |  197 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. End
  84. Option Explicit
  85.  
  86. '---------------------------------------------------------------------------
  87. 'This example form was uploaded by Nigel Price 100063,3363 for anyone to view
  88. 'and use/modify etc. It shows a method for validating fields using the LostFocus
  89. 'event. This has been the cause of a lot of problems and discussion on MSBASIC.
  90.  
  91. 'This solution allows a Cancel button to work either by clicking, pressing
  92. 'the default Esc key, or by using the Alt-C accelerator key. The validation routine
  93. 'is very basic just to demonstrate the technique. The user is able to move
  94. 'from field to field if the field validation is ok. If it is not the user
  95. 'is shown a message and then they are returned to that field. The user can
  96. 'choose to Cancel at any time using the Cancel button. I have tested the
  97. 'technique with many different controls including 3rd party ones. I hope it
  98. 'helps anyone trying to do field level validation. I have extended this basic
  99. 'technique to include a form level validation routine which calls this for
  100. 'each field and builds up a 'List of Field problems' which the user can view.
  101. 'They can then deal with each problem. Basically that Form level validation loops
  102. 'through the Controls collection and calls this (or rather a modified version!)
  103. 'procedure. I have not uploaded that version because it requires the use of
  104. 'the CtlName() function which comes with Jonathan Zucks VBZ Electronic magazine
  105.  
  106. 'If anyone has any comments (constructive ones!) or problems feel free to
  107. 'send me a message.
  108. '---------------------------------------------------------------------------
  109.  
  110. Dim sm_LostFocus_Control As String   'Holds name of Current Control which has a validation error
  111.  
  112. Sub Cbo_Title_LostFocus ()
  113.  
  114. Field_LostFocus Cbo_Title, "Cbo_Title"
  115.  
  116. End Sub
  117.  
  118. Sub Cmd_Cancel_Click ()
  119.  
  120. Unload Me
  121.  
  122. End Sub
  123.  
  124. Sub Cmd_Save_Click ()
  125.  
  126. MsgBox "Save the Record Here"
  127.  
  128. End Sub
  129.  
  130. Sub Field_LostFocus (Current_Control As Control, Control_Name As String)
  131. 'Note : If you subscribe to VBZ an Electronic magazine for VB there is a function
  132. '       called CtlName() which can save having to pass the Control_Name parameter.
  133. '       You can then use CtlName(Current_Control) to get the name of the current
  134. '       control. This saves one parameter.
  135.  
  136. If LOSTFOCUS.ActiveControl <> LOSTFOCUS!Cmd_Cancel Then     'User clicked on a control other than the Cmd_Cancel button
  137.     Control_Name = UCase$(Control_Name)                     'Upper case the Control Name which called this Sub
  138.  
  139.     If Control_Name = UCase$(sm_LostFocus_Control) Or sm_LostFocus_Control = "" Then   'Is a LostFocus Validation already occuring?
  140.         Select Case Control_Name
  141.             Case "TXT_SURNAME"      'Validate Txt_Surname Here
  142.  
  143.                 If LOSTFOCUS!Txt_Surname.Text = "" Then
  144.                     sm_LostFocus_Control = Control_Name
  145.                     MsgBox "The Surname cannot be Blank, Please re-enter it"
  146.                     Current_Control.SetFocus
  147.                 Else                    'All Validation OK for Txt_Surname
  148.                     sm_LostFocus_Control = ""
  149.                 End If
  150.  
  151.             Case "TXT_FORENAME"     'Validate Txt_Forename Here
  152.  
  153.                 If LOSTFOCUS!Txt_Forename.Text = "" Then
  154.                     sm_LostFocus_Control = Control_Name
  155.                     MsgBox "The Forename cannot be Blank, Please re-enter it"
  156.                     Current_Control.SetFocus
  157.                 Else                    'All Validation OK for Txt_Forename
  158.                     sm_LostFocus_Control = ""
  159.                 End If
  160.  
  161.             Case "CBO_TITLE"
  162.             
  163.                 If LOSTFOCUS!Cbo_Title.Text = "" Then
  164.                     sm_LostFocus_Control = Control_Name
  165.                     MsgBox "The Title must be entered / selected from Combo Box List"
  166.                     Current_Control.SetFocus
  167.                 Else                    'All Validation OK for Cbo_Title
  168.                     sm_LostFocus_Control = ""
  169.                 End If
  170.  
  171.         End Select
  172.     End If
  173. Else
  174.     sm_LostFocus_Control = ""           'Clear the LostFocus Control Name as were Cancelling
  175. End If
  176.  
  177. End Sub
  178.  
  179. Sub Form_Load ()
  180.  
  181. sm_LostFocus_Control = ""
  182.  
  183. End Sub
  184.  
  185. Sub Txt_Forename_LostFocus ()
  186.  
  187. Field_LostFocus Txt_Forename, "Txt_Forename"
  188.  
  189. End Sub
  190.  
  191. Sub Txt_Surname_LostFocus ()
  192.  
  193. Field_LostFocus Txt_Surname, "Txt_Surname"
  194.  
  195. End Sub
  196.  
  197.