Hello How can I update the value of a label with the value i get from a textbox? - vb.net

The code I use is the following:
' (is on top of the listing directly after Public class form1)
Dim Score as Integer
...
Score=Val(Txtbox5.text) "Txtbox5 is control where I put in the value."
Lbl2.text=score "Lbl2 is the label where the score must display."
Txtbox5.text="" "Makes the Textbox empty."
Then the problem occurs: when I enter a new value in Txtbox5.text the I want this value update
the value in the label with the value that is already in the label by the score.
The code I use therefor is Lbl2.text=score +score but whats happend is the score in the label is double???
So what I want is: when I have a value in the label say 2 and I insert a new value in the textbox
say 3 then I want to see in the label a value of 5.
But I have tried everything but nothing works
Is there somebody who can help me with this problem??

If I understand you correctly you want to sum the current value of the TextBox5 to the Score variable and then update the label
This could be simply resolved without any thinking with
Score += Val(Textbox5.Text)
Lbl2.text=Score
Txtbox5.Text=""
But you could meet some problems because in your textbox the user can type anything, not just a number. In that context the Val function from the Microsoft.VisualBasic compatibility assemble doesn't tell you anything, simply converts the non numeric string to zero. This could be acceptable or not depending on your requirements. If you want to have a message for your user when the input is not good you could write
Dim currentInput as Integer
if Not Int32.TryParse(Txtbox5.Text, currentInput) Then
MessageBox.Show("You should input a numeric value!")
Else
Score = Score + currentInput
Lbl2.text=Score
Txtbox5.Text=""
End If

I have tried everything but nothing works
That's not strictly true..
Lbl2.Text = (Convert.ToInt32("0"&Lbl2.Text) + Convert.ToInt32(Txtbox5.Text)).ToString()
ps;
Your life would be a lot simpler if you used a couple of numericupdown controls, one readonly and no border etc to look like a label. Then life would be like:
resultNumericUpDown.Value += inputNumericUpDown.Value
Whenever you're asking the user for numbers, using a NUD saves a lot of headaches

Related

How do I fix MS access option group problem?

I created many option groups in MS Access 2013 and I am trying to populate my table according to what is selected in the option group. So, if the user selects option 1, I want "the text" not its value ex: "1" stored in my table. I tried the following code in AfterUpdate() event and it works fine:
Private Sub Frame49_AfterUpdate()
Dim D As Integer
Select Case Me![Frame49]
Case 1
Me![Name] = "text"
D = 1
Case 2
Me![Name] = "text1"
D = 2
Case 3
Me![Name] = "text2"
D = 3
Case 4
Me![Name] = "text3"
D = 4
Case 5
Me![Name] = "text4"
D = 5
End Select
DoCmd.RunCommand acCmdSaveRecord
Rem D = Frame49.Value
End Sub
but when the end user answers the first question and tries to answer the next question, all options of the previous question get selected. How do I fix this?
Here is the file to see what I mean:
https://drive.google.com/open?id=1WjrAhXCnk961mxBuxS3RYqOUpPA_GsyL
Thanks in advance.
even though the option group only takes numeric values, you can achieve what you want by hard coding the values using if statements e.g
If Frame5 = 1 Then orukolook = "okay"
If Frame5 = 2 Then orukolook = "right"
If Frame5 = 3 Then orukolook = "fine"
orukolook is the textbox control that you want the texts to be inserted, so if the first option of the option group is selected,the text "okay" will be inserted into the textbox control, if second option then the text "right" will be inserted.
The values hard coded in the place holder oruko look,e.g okay,right, fine are the labels associated to each value in the option group.
OptionGroup frame and associated buttons/checkboxes must have a number value. Therefore OptionGroup frame must be bound to a number type field. If you want controls to reflect selection in a text field, then need code to set UNBOUND OptionGroup frame with corresponding number value. In other words, convert saved text back to number value. Code would most likely need to be in form Current event. Something like:
Me.Frame49 = Switch([Name]="text",1, [Name]="text1",2, [Name]="text2",3, [Name]="text3",4, [Name]="text4",5)
Alternatively, save number value to number fields. Text equivalent is provided by labels on form. Use lookup tables to provide text equivalent on reports or calculate the equivalents with expressions in query or textboxes. An expression like:
Choose([Name], "text", "text1", "text2", "text3", "text4")
BTY, Choose() expression can be used in place of Case structure in your original code.
Me![Name] = Choose(Me.Frame49, "text", "text1", "text2", "text3", "text4")
Also recommend using radio (option) buttons instead of checkboxes.
Other alternatives are comboboxes and listboxes instead of option groups.
Advise not to use reserved words as names for anything. Name is a reserved word. Also, avoid spaces and punctuation/special characters in naming convention.
Frame49 is bound to a database field.
When the user clicks a checkbox, the field's value (along with Frame49's value) is set to an integer.
You then change the database field's value to a string.
This causes Frame49's value to be set to that string.
Since that is an invalid value for an Option Group, all the related checkboxes show as a solid black square, representing an indeterminate state. That is not the same as a checkmark, so your observation "all options of the previous question get selected" is incorrect.
The simplest way to do what you want is to use a 1-column ListBox instead of an Option Group. You can size each ListBox so that it shows all the options as text strings.
When the user clicks an "option" to select it, the corresponding text string will be written to the database, with no VBA code involved.
When the user goes back to a previous record, the ListBoxes will all show the proper selections.
If you don't want to change how your form looks, then you must do as others suggested, and make Frame49 unbound, i.e. set its Control Source to blank.
Then when you set the database field's value to a text string, Frame49's value will remain as an integer.
If you want the ability to go back and edit earlier records, you can do it but it is beyond what I can answer here.

Populating a textbox, if another textbox is populated

i've got two textboxes in my form.
According to title, when I write something in one textbox (a random one), i need that at the same time, in the other textbox a text (given in the code) appears.
1 letter for 1 letter
1)Example with random text:
1 textbox ) How are you?
2 textbox) Let's just c
2)Example with random text:
1 textbox ) What is the aim of the project?
2 textbox) Let's just chill out for a mome
thanks so much
You will need to determine which responses you would like to which questions. However, the tackle to letter for letter problem. I would use the substring method and get the length of the current text.
Dim Response as String = "Hello World!"
Private Sub Text(ByVal..) Handles Textbox1.changed
Textbox2.text = Response.Substring(0, Textbox1.text.length)
End Sub
As the text changes in the textbox you are typing in, this will output the response corresponding to the length of the text input. Since the event is textbox.changed, each character entered will update the second textbox
You need to use an event based code here. I would use a loop per stroke of the key and make the event "Key Press." Write the code onto the textbox you are inserting data into.

NumericUpDown to set decimal points?

I'm trying to make a NumericUpDown box set the decimal points of a textbox and update everytime it is clicked up or down. I have tried it 2 different ways:
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
This way works but it isn't keeping the decimal values when i increase the value. So if the textbox said 2 and is really supposed to be 2.987899. I change the NumericUpDown box from 0 to 6 and all it does is display: 2.000000.
I've also tried it this way (Which i think I'm doing wrong..)
Me.txtCalc.Text = Math.Round(CDec(Me.txtCalc.Text), CInt(Me.NumericUpDown1.Value))
This was the only way I could get it to do anything. And when I try this, everytime I click the vlaue to go up, it does absolutely nothing. But if I set it to 7 first and then add some values like: 2.987899; when i click from 7 down to 1, it removes one point at a time like it should... it just doesnt go back up. at all.
I assume I have to use Math.Round because that's the only way it will keep the decimal values...
Hopefully I explained this so you guys understand.
Any help would be appreciated.
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
this not only formats the value, it resets the starting value for any next iteration. Assuming a start of 2.987899 going down in decimal places changes the VALUE:
2.98789
2.9878
2.987
..
2.
When "2" gets assigned to the TB, the starting value for going up is 2. you lost the rest of the decimals. You need to store in a decimal variable and format THAT to go up and down as you want:
Dim decVal As Decimal = 2.987899
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
' now, I am not changing the underlying VALUE, just the format
TB1.Text = FormatNumber(decVal, NumericUpDown1.Value)
End Sub
I'm not sure how you get different results with Math.Round because the code shown will still assign a new value to the TB with fewer decimals and therefore lost.

Changing TextBox fields on Lost Focus

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.

visual basic do while input

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.