• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Form question

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
tdove
Asked:
tdove
  • 2
  • 2
  • 2
  • +6
1 Solution
 
CatouchCommented:
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
 
VbmasterCommented:
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
 
hesCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jeremy_DCommented:
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
 
Jeremy_DCommented:
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
 
MTroutwineCommented:
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
 
CatouchCommented:
Got my comment on mail too!!
0
 
Joe_GriffithCommented:
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
 
pacificdbCommented:
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
 
pacificdbCommented:
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
 
Brendt HessSenior DBACommented:
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
 
tdoveAuthor Commented:
Thnaks Hes!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now