Solved

Form question

Posted on 2000-03-14
12
151 Views
Last Modified: 2010-05-02
I have a form that has 7 textboxes for the user to input data.  I have a button called "Save" that when the user has input the data in the textboxes will then save it to a database.  I have that part done, now I want to be able to verify that data was put in the textboxes before it adds the record.  How is this done.

Thanks
0
Comment
Question by:tdove
  • 2
  • 2
  • 2
  • +6
12 Comments
 
LVL 1

Expert Comment

by:Catouch
ID: 2615821
Maybe you can just put a check on the boxes at the end; something like
if textbox1.text = "" then
'send a messagebox that says that all boxes should be filled.
Repeat the check for the other boxes
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2615827
You can use code like this (example)...

  If (Text1.Text = "") Then
    Call Msgbox("No data input in Text1")
  ElseIf (Text2.Text = "") Then
    Call Msgbox("No data input in Text2")
  ElseIf (Text3.Text = "") Then
    Call Msgbox("No data input in Text3")
  .
  .
  .
  Else
    'All data input okay, do your saving here
  End if
0
 
LVL 20

Accepted Solution

by:
hes earned 100 total points
ID: 2615841
In your sub to save the data add:
Sub save_data()
if text1.text = "" then
 msgbox "must fill in data"
 text1.setfocus
 exit sub
end if
' repeat for all 7 boxes
'add the save data code here
End Sub
0
 
LVL 4

Expert Comment

by:Jeremy_D
ID: 2615842
You need to check the values from the textboxes in the Save_Click event. Check them to see if they are filled in:

If Text1 = "" Then
    MsgBox "You need to fill in a value in the first box!", , "Error"
    Exit Sub
End If
If Text2 = "" Then
    MsgBox "You need to fill in a value in the second box!", , "Error"
    Exit Sub
End If
etc.

(Obviously it would be better to make the textboxes in a control-array, that way it would require less code to check the values)

You can also add other checks there, for instance to check if a telephone number or credit card number is valid, or if a date is valid, etc.

Only perform the save to the database if all values are validated and checked out OK.

0
 
LVL 4

Expert Comment

by:Jeremy_D
ID: 2615860
Since when do you get an email if you add a comment to a question yourself? Happened to me twice today:

>> Jeremy_D has added a new comment to the question.
>>
>> If you'd like to read Jeremy_D's comments, enter Experts Exchange at
the following URL:
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2615882
I agree with everyone's solutions except I would suggest checking the length of the textbox rather comparing the string to an empty string.  It is 15%-25% faster to compare the length of a string to zero than to compare the string to an empty string.  

Using one of the examples above it would appear like this:
If Len(Text1.Text) = 0 Then
    MsgBox "You need to fill in a value in the first box!", , "Error"
    Exit Sub
End If
If Len(Text2.Text) = 0 Then
    MsgBox "You need to fill in a value in the second box!", , "Error"
    Exit Sub
End If

:>)

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:Catouch
ID: 2615934
Got my comment on mail too!!
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 2616032
I suggest using the Validate event at that is what it is for:

Validate Event, CausesValidation Property Example
The example uses three controls to demonstrate the use of the Validate event and CausesValidation property. By default, the CausesValidation property of the two TextBox controls are set to True. Thus when you try to shift the focus from one TextBox to the other, the Validate event occurs. If Text1 doesn't contain a date, or if Text2 doesn't contain a number larger than 10, the shift of focus is prevented. Because the CausesValidation property of the Command1 control is set to False, however, you can always click the Help button.

To try the example, place one CommandButton and two TextBox controls on a form. Paste the code into the Declarations section of the form and run the project. Attempt to shift the focus by pressing the Tab key.

Private Sub Form_Load()
   ' Set the button's CausesValidation property to False. When the user
   ' clicks the button, the Validate event does not occur.
   ' Set the Caption property of the button to "Help".
   With Command1
      .CausesValidation = False
      .Caption = "Help"
   End With

   Show
   With Text1 ' Select text of Text1 and set focus to it.
      .SelLength = Len(Text1.Text)
      .SetFocus
   End With
End Sub

Private Sub Command1_Click()
   ' Give the user help when the button is clicked.
   MsgBox _
   "Text1 must be set to a date." & VbCrLF & _
   "Text2 must be a number less than 10."
End Sub

Private Sub Text1_Validate(KeepFocus As Boolean)
   ' If the value is not a date, keep the focus, unless the user
   ' clicks Help.
   If Not IsDate(Text1.Text) Then
      KeepFocus = True
      MsgBox "Please insert a date in this field.", ,   "Text1"
   End if
End Sub
   
Private Sub Text2_Validate(KeepFocus As Boolean)
   ' If the value is a number larger than 10, keep the focus.
   If Not IsNumeric(Text2.Text) Or Val(Text2.Text) > 10 Then
      KeepFocus = True
MsgBox _
"Please insert a number less than or equal to 10.", , "Text2"
   End If
End Sub

0
 
LVL 3

Expert Comment

by:pacificdb
ID: 2616072
I often create subs to handle validations. You have to create a control array, though.

'Put the following line in whatever event procedure you're trapping:
   ValidateField(Index)

'Then...
Private Sub ValidateField(iFld as Integer)
   Dim bProb as Boolean
   Select Case iFld
      Case 1
         If txtName(iFld).Text = "" then CustMsg(1):bProb = True
         If txtName(iFld).Text = "whatever" then CustMsg(2):bProb = True
      Case 2
      Case 3
   End Select
   If bProb = True then ValidateField(iFld).SetFocus
End Sub

Private Sub CustMsg(iMsgNo as Integer)
   MsgBox "Message text"
End Sub
0
 
LVL 3

Expert Comment

by:pacificdb
ID: 2616090
I meant...

Private Sub CustMsg(iMsgNo as Integer)
   Select Case iMsgNo
      Case 1:strMsgTxt = "Message1"
      Case 2:strMsgTxt = "Message2"
   End Select
   MsgBox strMsgTxt
End Sub
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2616779
There are two problems with using only the Validate event.

First, there are a number of problems with Validate listed in Microsoft's KB.  Documented problems with validate include these:

BUG: CausesValidation Property Does Not Trigger Validate Event ID: Q197128

BUG: Constituent Control Validate Event Can Fail on UserControl ID: Q191912

BUG: Validate Event Is Not Fired When Placing a FlexGrid Control on a Tab ID: Q232268  

PRB: No Validate Event When Activating CommandButton ID: Q189923

PRB: Validate Event Does Not Fire When Changing Focus with Mouse to ATL Control ID: Q230326

PRB: IPF from UserControl with a User-Defined Validate Event ID: Q217026

There may be others.

Second, your validate event is of no use if focus is never recieved by a control.  If, for example, you had a form with 5 required text fields and a 'save' button, entered data into 3 of the controls, and clicked 'save', the 4th and 5th controls Validate events would not be triggered.

You should really validate all of your data on a save/commit.
0
 

Author Comment

by:tdove
ID: 2617300
Thnaks Hes!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now