I am trying to validate input in this VB program that calculates football stats. If both boxes are empty when a user clicks Update Stats, it should display a message box. However, with this code it shows the message whether both boxes are empty or if they have values entered. Any idea what I am doing wrong?
'validate user input (ensure textBoxes aren't blank)
If passYdTextBox.Text = "" And rushYdTextBox.Text = "" Then
MessageBox.Show("Please enter values into pass yards box or rush yards box")
End If
A better way to check this would be to use the IsNullOrWhiteSpace() method or even doing something like passYdTextBox.Text.Length > 0 as your problem could be due to data types and casting.
I'm sure you have already considered this as well, but this validation alone is not enough to avoid an exception. You will also need to ensure that the Text in the field is numeric before trying to cast the String.
Related
When I enter a non-numeric value into a numeric field, vb auto validates the field and the system does not allow me to proceed to fill the next text box.
How can I work around this? I want to write code to perform the validation on the numeric.
This is what happened as shown in picture. I cannot continue to input other fields.
field
There is probably an event handler that is limiting the input to numeric fields and possibly setting the focus on the textbox. This could be in Keydown, Leave, TextChanged, or some other events. You can replace this validation code with your own.
I'm building a form in Adobe Livecycle and would like all negative numbers to display in red. I can accomplish that on individual elements by attaching this code to the exit event:
form1.#subform[0].Table3[0].HeaderRow.concessions1::exit - (FormCalc, client)
if ($.rawValue <0) then
$.font.fill.color.value = "255,0,0"
else
$.font.fill.color.value = "0,0,0"
endif
Currently I have to add this code to every single field, is there a way to make this script run on multiple fields without retyping it for each one?
Thanks
your options are:
Externalize the code in a script object and call the method on the exit event. You can pass the field as an input parameter so that your code is generic.
If you want to do this before a form is submitted, you can create a method that loops through your form fields and reference this code to highlight the negative values.
I hope this helps. Let me know if you have any more questions.
Thanks,
Armaghan.
I've reviewed dozens of options/solutions on this and I just can't get it to work.
Simply put, I have a VB.Net Winform that has a textbox where a user can manually type in text or they can use a USB connected barcode scanner (that simulates a keyboard) to capture a UPC.
What I'm trying to do is get the barcode input to get entered into the textbox regardless of which control has the current focus.
I have set the KeyPreview property of the form to True.
I then added some code to the frmMain_Keypress event as follows:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
End If
Very simple...and it works, sort of...
If txtSearch already has the focus, the entire barcode/UPC gets entered into the text box.
However, if another control has the focus, every character of the barcode/UPC EXCEPT THE FIRST CHARACTER gets entered into the text box. It always strips off the first character.
I placed some debug statements in the above code to see if the initial character was being read at all and it is being read...just not sent to the text box.
I've seen so many other REALLY complicated solutions to barcode scanning and it seems like I'm really close with something really simple, but, obviously it won't work if it strips the leading character.
Hopefully I'm missing something very obvious.
Change the code in your KeyPress event to:
If Me.txtSearch.Focused = False Then
txtSearch.Focus()
txtSearch.Text = e.KeyChar.ToString
txtSearch.SelectionStart = txtSearch.Text.Length
e.Handled = True
End If
That way you capture the first key that comes in.
I am making a database with access 2007. I have a form for the call center to enter customer info; Name, Address, Phone Number, ect.
There is a field for credit card numbers and while we are supposed to enter them as first 4 numbers and last for number ie.1234xxxxxxxx4321
I want to make sure if they do enter them in that it keeps the first and last 4 numbers but changes other characters to "x" when the field loses focus. Could anyone point me in the right direction of how to do this?
Thanks in advance for all help in this matter.
If you are only storing the first 4 and last 4 digits then the following works. Modify the function validCreditCardNumber() to have whatever checks you want to apply.
Function validCreditCardNumber(creditCardNumber) As Boolean
If Len(creditCardNumber) = 12 Then
validCreditCardNumber = True
Else
validCreditCardNumber = False
End If
End Function
Private Sub cbxCreditCardNumber_LostFocus()
If validCreditCardNumber(cbxCreditCardNumber) Then
cbxCreditCardNumber.Text = Left(cbxCreditCardNumber.Text, 4) & "xxxxxxxx" & Right(cbxCreditCardNumber.Text, 4)
End If
End Sub
If you want to store the entire number but only hide the digits from the screen then I think a input masks are what you are looking for.
All you need is something like this in your form code.
Private Sub txtCC_LostFocus()
txtCC.Text = Left(txtCC, 4) & String(8, "x") & Right(txtCC, 4)
End Sub
Then what you see is what will get stored in the DB. ie.1234xxxxxxxx4321
I'm going to assume you don't want to actually keep the whole CC# in your DB. That is a huge no-no unless you spend massive time & money to meet PCI compliance. Here's some info on PCI: http://www.pcicomplianceguide.org/pcifaqs.php
There is no input mask available for partial masking of a string entry. You will need to use a custom function to change the data entered. The best way to do this would probably be to link the field in the table to a hidden textbox. You can then have a visible textbox apply some function to the value in the hidden textbox to change the characters to "X" in the LostFocus event of the visible textbox. However, it's also going to have to write the data to the hidden textbox when you change it.
I have to make a "Do - while" input from a TextBox in Visual Basic. I mean I get some value for the amount of the numbers the user should type, and then I have to get this quantity of numbers from a textbox. I suppose the event is on TextBox1_KeyPress() but then the event is true even when I type in 0 for example and this way I won't be able to write the whole number, but the first digit. I want to be able to type in the whole number and then when I press Enter the value from the txtBox goes in some variable and cleans itself to write the next one.
Anyone who has any ideas? I'm really new in VB, sorry :(
dont use key_press. use key_up. in event args you will find keycode. for enter the code is 13. so when keycode = 13, extract textbox1.text to a variable. then clear the text box like textbox1.text = "". when ever you do this, increment a member variable by one. by that variable you will know how many values have been entered.