I am checking a couple of textboxes in my datarows. If the first textbox (event association 1) is not blank and it does not contain the word business, but the date of birth mtb is empty, then the statement is true and a messagebox will be displayed. Right now, I just don't understand the error, while debugging, regarding string conversion to boolean is not valid?

 For Each Personrow As DataRow In PFC_Xpress.Tables("Person").Rows
                If EVENT_ASSOC1.Text <> "" & EVENT_ASSOC1.Text <> "Business" Then
                    For Each Personrow1 As DataRow In PFC_Xpress.Tables("Person").Rows
                        DOB.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
                        If DOB.Text = "" Then
                            Not_Business = True & MessageBox.Show("missing dob")
                        End If
                    Next
                End If
            Next

Dani AI

Generated

Short answer: the bug comes from using the wrong operators and mixing expressions. In your original code you have two separate issues that interact: a string-concatenation/assignment mistake and some fragile validation logic. correctly steered you away from trying to combine a flag set and a dialog call into a single expression, and and both raised the short-circuiting point. Build the test in clear steps: determine whether the association requires a DOB, check the DOB cleanly, then set your flag and show the message.

A safe, readable pattern (showing the checks and using the MaskedTextBox helper) is:

Dim assoc = EVENT_ASSOC1.Text.Trim()
If Not String.IsNullOrWhiteSpace(assoc) AndAlso Not assoc.Equals("business", StringComparison.OrdinalIgnoreCase) Then
    DOB.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
    If Not DOB.MaskCompleted Then
        Not_Business = True
        MessageBox.Show("Missing date of birth")
    End If
End If

Notes and practical tips:

  • Use AndAlso/OrElse for short-circuit evaluation so later checks won't run when earlier ones already fail.
  • Prefer String.IsNullOrWhiteSpace (or Trim + Length) and a case-insensitive string comparison for the "business" test.
  • For MaskedTextBox DOB fields, MaskCompleted (or MaskFull) is a more reliable indicator than raw Text, and set TextMaskFormat before reading.
  • Do not assign the result of MessageBox.Show to a Boolean; set the Boolean explicitly and then call the dialog on the next line (as suggested).

If you are validating rows from a DataTable, validate the DataRow fields directly instead of repeatedly reading UI controls and avoid nesting two identical For Each loops over the same table. Consider accumulating row-level errors and showing a single consolidated message or using an ErrorProvider rather than popping a message box for every missing value.

Recommended Answers

All 8 Replies

The statement

Not_Business = True & MessageBox.Show("missing dob")

is invalid. MessageBox returns type DialogResult. You have to compare that result to something to get a boolean. Example

If MessageBox.Show("missing dob") = DialogResult.yes

What are you trying to accomplish with that statement?

When a button is clicked, some conditional statments are run through. If the user has entered anything besides "business" in the event association box, but forgets to add the date of birth, a messagebox will be displayed, informing them they have forgotten to add the date of birth.

If DOB.Text = "" Then
    Not_Business = True
    MessageBox.Show("missing dob")
End If

Thank you for help Rev. I'll give that a try when I get home. More importantly, thanks for the explanation as to the error!

Hey Rev, I see how simply this code works, but I'm also trying to take into consideration that if the user selects business, the dob field does not need to be completed.

What is the problem?

Try this
if Not_Business or isempty(DOB.text) then
..The rest of your code here

the second part of the statement - isempty(DOB>text) will only be evaluated if Not_Business is true

hope this helps

That's not quite true. To get the short-circuit form you have to use

If Not_Business OrElse IsEmpty(DOB.Text) Then

If you use Or or And instead of OrElse or AndAlso then the entire expression is evaluated. This can be a problem if the second part of the expression can return an error under some conditions. For example, checking the value of the first item in a recordset will cause an indexing error if rec.EOF is true. So

If Not rec.EOF And rec(0).Value = 5 Then

could cause an error whereas

If Not rec.EOF AndAlso rec(0).Value = 5 Then

will not because the second part is evaluated only if EOF is False.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.