Changing TextBox fields on Lost Focus - sql

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.

Related

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

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

Is it possible to reference a listbox in a textbox on the same form?

I have an ID field which can not be autonumbered, and serves as the primary key for a table. When we put a new record in we need the next record to be numbered in one of two sequences that are pre-existing and can not be changed.
I was trying to do this:
=IIf([lst_DeviceType]="Cell Phone",DMax("DeviceNum","tbl_Cell_Tab","DeviceNum < 70000")+1,DMax("DeviceNum","tbl_Cell_Tab")+1)
Inside the field that will be used to create a device number.
What we have are cell phones and tablets, for cellphones the devices start at 10000, and for tablets they start at 70000. What I need to have happen is for a selection from a listbox, "Cell Phone" or "Tablet" to cause the next proper sequence number to be generated. ie. 10125 for "Cell Phone" but 70725 for "Tablet"
The above code returned a #Error
My question is thus two fold : One, is something wrong with my code? I am thinking that I am not properly referencing the listbox, but maybe that isn't it. Or two, can I not do this in the text box itself?
This is the code being used in the text box, I could try an OnUpdate on the form,
Which broke when I tried that using this code it was placed in the OnUpdate for the listbox, because that's the main selector:
If Me.lst_DeviceType = "Cell Phone" Then
Me.txt_DN.Value = DoCmd.RunSQL("SELECT MAX(DeviceNum)+1 FROM tbl_Cell_Tab
WHERE DeviceNum < 70000")
Else
Me.txt_DN.Value = DoCmd.RunSQL("SELECT MAX(DeviceNum)+1 FROM
tbl_Cell_Tab")
End If
Me.txt_DN.Locked = True
but I wonder if that is both the best and easiest way to do it?
Listboxes and comboboxes behave differently.
For a listbox, must include Column index when pulling value from selection: [lst_DeviceType].Column(0)
Then you will need code to save the calculated value. Instead of expression in textbox, bind textbox to ID field and use listbox AfterUpdate event:
If IsNull(Me!ID) Then
Me!ID = IIf(...)
End If
AFAIK, cannot use DoCmd.RunSQL to return a value to a variable. That's what domain aggregate functions are for.

Creating a Userform to compare 4 scanned items

I apologize if this seems very simple. I am new to VBA and am struggling to determine where to begin.
I am trying to create a Userform where you scan a sample, and then 3 tubes that should match the first sample, and if they do, it will send that data to a spreadsheet, and if they do not, then it will highlight Red and make you restart.
I am running into a few problems. I am not sure how to auto enter to the next text box after the first sample is scanned. For ease of use, I would like the user to just be able to scan, scan, scan, scan without interaction with the spreadsheet.
I also have tried to make use of the AutoTab function by putting in an If/Else statement, saying if value of the textbox is NOT null, it will AutoTab to the next box in the tab index, but that does not seem to be working
Please let me know of any advice you may have.
Basically, the input of the barcode scanner is the same as if someone enters the data manually - just all characters come at once (as if it is pasted). Usually, the scanner should be able to send some End-sign, in that case you should be fine as TAB or ENTER will jump to the next field automatically.
If the scanner really send the data, you have to deal with the Change-Event of the text boxes and check if the data is valid. The following example checks for a length of 8 characters. Adapt the CheckBarcodeInput to your needs. If a valid barcode was entered, the focus is set to the next textbox.
Private Sub TextBox1_Change()
If CheckBarcodeInput(Me.TextBox1.Text) Then Me.TextBox2.SetFocus
End Sub
Private Sub TextBox2_Change()
If CheckBarcodeInput(Me.TextBox2.Text) Then Me.TextBox3.SetFocus
End Sub
(...)
Private Function CheckBarcodeInput(s As String) As Boolean
' Check if barcode is valid. In this example, it simply checks the length.
If Len(s) >= 8 Then CheckBarcodeInput = True
End Function

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.