Author Topic: Code Help  (Read 1872 times)

Cowboy1

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Code Help
« on: July 27, 2015, 09:33:55 AM »
I'm working on a batch set-up screen and need to validate data before continuing into the process.
I want the following if possible.
If checkbox1.Value = True then BasicLabel1 and BasicLabel2 need to be greater than ""
I can get it to work for 1 basic label just not sure how to get the other label into the code?
This is the code in VBA I have working for 1 label

Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
        Call Data_Validate1
    Else
    End If
   

End Sub

Public Sub Data_Validate1()
If TextBox1.Text = "" Then
MsgBox "The Temperature field must be between 122-168.", _
 vbExclamation + vbOKOnly, _
 "Temperature"
 TextBox1.SetFocus


Else
End If

End Sub


« Last Edit: July 27, 2015, 09:38:46 AM by Cowboy1 »

dmroeder

  • Administrator
  • Full Member
  • *****
  • Posts: 211
    • View Profile
Re: Code Help
« Reply #1 on: July 27, 2015, 10:26:23 AM »
In your Data_Validate1 you could do the following:

Code: [Select]
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            'Success!
        Else
            'Anti-Success!
            MsgBox("The Temperature field must be between 122-168.", _
                vbExclamation + vbOKOnly, _
                "Temperature")
            If TextBox2.Text = "" Then TextBox2.Focus()
            If TextBox1.Text = "" Then TextBox1.Focus()
        End If

Keep in mind that you are telling the user that they have to enter specific values, but you are not checking for that.  So if they entered "X" and "Y" in the text boxes, it would be happy.

Edit:  You may need to change .Focus() to .SetFocus if you are really using VBA, I was using VB.NET
« Last Edit: July 27, 2015, 10:29:34 AM by dmroeder »

Cowboy1

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Code Help
« Reply #2 on: July 27, 2015, 11:45:01 AM »
In your Data_Validate1 you could do the following:

Code: [Select]
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            'Success!
        Else
            'Anti-Success!
            MsgBox("The Temperature field must be between 122-168.", _
                vbExclamation + vbOKOnly, _
                "Temperature")
            If TextBox2.Text = "" Then TextBox2.Focus()
            If TextBox1.Text = "" Then TextBox1.Focus()
        End If

Keep in mind that you are telling the user that they have to enter specific values, but you are not checking for that.  So if they entered "X" and "Y" in the text boxes, it would be happy.

Edit:  You may need to change .Focus() to .SetFocus if you are really using VBA, I was using VB.NET

Thanks, for the code it works, but after taking into consideration the temperature range I tried this and it failed.
Code: [Select]
Public Sub Data_Validate1()
    Dim TextBox14 As Double
    Dim TextBox15 As Double

    If CdblTextBox14.Text <= 168 And CdblTextBox14.Text >= 122 And CDblTextBox15.Text <> "" Then
            'Success!
        Else
            'Anti-Success!
            MsgBox "The Temperature field must be between 122-168.", _
 vbExclamation + vbOKOnly, _
 "Temperature"
            If TextBox15.Text = "" Then TextBox15.SetFocus
            If TextBox14.Text <= 168 And TextBox14.Text >= 122 Then TextBox14.SetFocus
       

    End If

End Sub

I don't have Visual Studio at work so I'm using VBA to test logic of the code to see if I can get it to work. I will be using VB.NET for the application.


Thanks

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5311
    • View Profile
    • AdvancedHMI
Re: Code Help
« Reply #3 on: July 27, 2015, 12:16:17 PM »
Instead of just checking for an empty string, here is a slightly better method that will also catch nulls:

if String.IsNullOrEmpty (TextBox1.Text) then
     'Nothing in the text box
end if

Cowboy1

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Code Help
« Reply #4 on: July 27, 2015, 02:00:43 PM »
I got this to work by using a validate data button:

Code: [Select]
Private Sub CommandButton2_Click()

   If CheckBox1.Value = True Then
        Call Data_Validate1
    Else
    End If

End Sub

And used these for validation. I will change code for VB.NET when I get home

Code: [Select]
Private Sub Data_Validate1()
   

    If TextBox14.Value <= 168 And TextBox14.Value >= 122 Then
            'Success!
        Else
            'Anti-Success!
            MsgBox "The Temperature field must be between 122-168. ", _
 vbExclamation + vbOKOnly, _
 "Temperature"
 
                    If TextBox14.Value > 168 And TextBox14.Value < 122 Then TextBox14.SetFocus
       

        End If
       
        If CheckBox1.Value = True Then
        Call Data_Validate2
    Else
    End If
   
   

End Sub

Private Sub Data_Validate2()

    If TextBox15.Value <> "" Then
            'Success!
        Else
            'Anti-Success!
            MsgBox "The Time field must be greater than Zero. ", _
 vbExclamation + vbOKOnly, _
 "Time"
 
                    If TextBox15.Value = "" Then TextBox15.SetFocus
       

    End If
   

End Sub


I will probably use the null code for the time textbox I don't think it will run in VBA so will have to try that later:

Code: [Select]
if String.IsNullOrEmpty (TextBox1.Text) then
     'Nothing in the text box
end if


Thanks

Godra

  • Hero Member
  • *****
  • Posts: 1444
    • View Profile
Re: Code Help
« Reply #5 on: July 27, 2015, 10:20:22 PM »
If you use NumericUpDown control instead of a textbox and set its minimum value to 122 and maximum value to 168, with an option to select initial set value, then your operators wouldn't be able to set it outside the desired range.

You wouldn't really need to create almost any code behind it.

If still in need of using checkbox then you could associate it with enable/disable of the NumericUpDown control, double click CheckBox1 to access the following sub and enter code similar to this:

Code: [Select]
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If Me.CheckBox1.Checked Then
            Me.NumericUpDown1.Enabled = True
        Else
            Me.NumericUpDown1.Enabled = False
        End If
    End Sub

and if the initial state of the checkbox is unchecked then you should initially disable the NumericUpDown control.
« Last Edit: July 27, 2015, 10:41:47 PM by Godra »