Update textbox during typing - ms-access

In Access, I have a form in which there are three textboxes.
I am trying to update a textbox called tbxCombinedName with a combination of both:
textbox tbxLastName (person's Last Name)
textbox tbxFirstName (person's First Name)
My question is: what textbox property do I use, so that as I am typing text in tbxLastName, the CombinedName textbox is updated immediately and thereafter saved in the table Contacts.
On Microsoft's website, I have found that the step processes when typing in a textbox are as follows:
KeyDown → KeyPress → BeforeInsert → Change → KeyUp
I've tried using the OnChange and OnKeyDown properties, but to no avail. Which property, combined with what code, will allow the update-as-you-type action to work?
This is what I wrote earlier, which didn't work:
Private Sub tbxLName_change()
Dim lastName As String
Dim nameCode As String
lastName = tbxLName.Value
Debug.Print lastName
nameCode = tbxNameCode.Value
nameCode = lastName
Debug.Print nameCode
End Sub
Thanks for all your help in advance.

This is one of the few cases where you should refer to the .text property.
In the Change event:
lastName = tbxLName.Text
The .text property is only available when a control has focus and it refers to the visible contents of the control.
However, this is a database and the general rule is that you no not store calculated fields. The full name can easily be obtained from a query.

Just a couple of notes:
You may want to go with KeyPress because it provides the ability to change or negate the key the user pushed.
In the example below, only letters are allowed and lower case letters are upper cased:
Private Sub tbxLName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Const ASCII_LOWER_RANGE = 65
Const ASCII_UPPER_RANGE = 122
Const ASCII_LOWER_A = 97
Const ASCII_LOWER_Z = 122
Const UPPER_MODIFIER = -32
Const ASCII_CANCEL_CODE = 0
Select Case KeyAscii
Case ASCII_LOWER_RANGE To ASCII_UPPER_RANGE
If KeyAscii >= ASCII_LOWER_A And KeyAscii <= ASCII_LOWER_Z Then
KeyAscii = KeyAscii + UPPER_MODIFIER
End If
Case Else
KeyAscii = ASCII_CANCEL_CODE 'Cancel Key Press
End Select
End Sub

Related

MS-Access Check box IF statements

How would i add a check("chk3") that will be ticked when ("Customer order Number") field has been inputted. when an order number is entered, then the check box will tick..
i also would like to know if it is possible that When the value field ("Value") is under £10,000 then the check box ("chk2") will tick. if it is over £10,000 then it will not tick
You may use AfterUpdate events for such validations.
Nz function checks for null value and converts them to empty string & 0 respectively
For Customer Order Number:
Private Sub txtCustomerOrderNo_AfterUpdate()
If Nz(Me.txtCustomerOrderNo, "") <> "" Then
Me.chk3 = True
Else
Me.chk3 = False
End If
End Sub
For Project value is less than 10K:
Private Sub txtProjEstimate_AfterUpdate(Cancel As Integer)
If Nz(Me.txtProjEstimate, 0) < 10000 Then
Me.chk2 = True
Else
Me.chk2 = False
End If
End Sub
Depends on your version of Access. In older versions, it was the AfterUpdate event. In newer ones, I think it's OnExit.
In whichever event of Customer Order Number, just set its value to True.
Private Sub CustomerOrderNumber_Exit(Cancel As Integer)
Me.chk3 = true
End Sub
It sort of depends what you're trying to do with the chk3 validation. If you're looking for a specific structure. For instance, a 10 digit number, then you would want a validation along those lines within the IF Statement. Something like:
(Using AfterUpdate)
If Me.[Customer order Number] LIKE "##########" Then
Me.chk3 = True
End If
Similiarly with the validation on the ck2 > £10,000 (assuming this is an appropriately designated currency field):
If Me.[Value] <= 10000 Then
Me.chk2 = True
End If

Don't allow specific letters in ALL textbox for MS Access

On a form, i have 13 textbox. When user click on save button, Access calls an API and passes the values in these text boxes to the API (via HTTP REST)
There are some characters which are not allowed like " not allowed to be saved through API. Hence I want to restrict users from Entering these Characters.
Currently I have created a Public Function which is called from KeyPress Event to check if the Characters are allowed or Not. But I have to call this function from every textbox KeyPress event. Is there a way to not allow " across all the text boxes??
Function
Public Function CharsNotAllowed(myChar As Integer) As Integer
If myChar = 34 Then
Beep 'Let the user know they hit an illegal key
CharsNotAllowed = 0 'Don't let the keystroke through
Else
CharsNotAllowed = myChar
End If
End Function
Function Calling
Private Sub StudentName_KeyPress(KeyAscii As Integer)
KeyAscii = CharsNotAllowed(KeyAscii)
End Sub
Private Sub StudentClass_KeyPress(KeyAscii As Integer)
KeyAscii = CharsNotAllowed(KeyAscii)
End Sub
'and so on for all the 13 text boxes
Note : 34 represents "
Have you tried on Form level? BTW, most of the time users copy and paste, this will allow users to enter those unwanted characters (just a reminder :))
Private Sub Form_KeyPress(KeyAscii As Integer)
If ActiveControl.ControlType = 109 Then 'only for textboxes
KeyAscii = CharsNotAllowed(KeyAscii)
End If
End Sub

How do i protect string in text box?

I have problem with text box named "txtPrice". I want to protect this text box from a string. If we input one string it will show message that I wrote. But if I input, for example "12r", it doesn't do anything.
Dim i As Integer
Dim s As Long
i = Asc(txtPrice)
If i < 48 or i> 57 then 'ascii number
MsgBox "Error"
End If
What exactly are you looking for? Under properties you can specify an input mask.
You can try If Not IsNumeric(txtPrice) if you're looking for a number.
If you don't want any letters, try this:
Dim intPos As Integer
For intPos = 1 To Len(txtPrice)
Select Case Asc(Mid(txtPrice, intPos, 1))
Case 65 To 90, 97 To 122
IsLetter = True
Case Else
IsLetter = False
End Select
If IsLetter Then Exit For
Next intPos
If IsLetter Then
MsgBox "Error"
End If
You can also do this within the properties of the text box field using validation rules:
https://support.office.com/en-gb/article/Restrict-data-input-by-using-validation-rules-b91c6b15-bcd3-42c1-90bf-e3a0272e988d
You can restrict data entry in this way. For instance you could enter >=0 in the validation rules property and then "Please enter numbers only" in the validation text property. I have used both methods (vba as previous answer) and access validation rules and both work well.

OldValue and Value same on radio button change Access 2007 VB

I have an access 2007 front-end app. On a particular form, there are 2 radio buttons in a radio button group. I am trying to detect when the radio button group is changed and capture the old and new values, but my OldValue and Value properties are = in the save event, even if I have changed it. The OldValue is equal to the New radio button value, not what it was originally.
I tried coding this in the form's Save subroutine. The intent was to compares the RB value with the original dataset value to force setting the old value, but it doesn't like the 'SET' statements
If fraResistOption.Value = 1 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 2 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "Y"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "Y"
End If
End If
End If
End If
Could someone suggest a way to do this? Please and thank you.
The .OldValue property of the Option Group (sometimes referred to as "Frame") does work. I have a table named [optValues]:
[ID] - AutoNumber
[optValue] - Numeric (Long Integer)
It contains one record:
ID optValue
1 3
My form's Record Source is the [optValues] table. The form has an Option Group named "Frame0" whose Control Source is the [optValue] field. It contains three Option buttons
label: "foo", value: 1
label: "bar", value: 2
label: "baz", value: 3
The After Update event handler for Frame0 is:
Private Sub Frame0_AfterUpdate()
MsgBox "Old value: " & Me.Frame0.OldValue & ", New value: " & Me.Frame0.Value
End Sub
When I open the form, "baz" is selected (because [optValue] is 3 in the table):
When I click on "foo" I immediately see the (correct) old and new values:
The only way I can think of to detect and capture changes to Option-Group values is to handle the Form_Current event (save the Option Group value), then also handle the Option-Group After event. Although you can change the Option-Group.Value, OldValue is likely a protected (read-only) property. Hope something like the following helps:
Dim OldValue As Byte
Dim CurrentValue As Byte
Private Sub Form_Current()
OldValue = Frame0.Value
End Sub
Private Sub Frame0_AfterUpdate()
CurrentValue = Frame0.Value
Debug.Print "AFTER: OldValue=" & OldValue & "' CurrentValue=" & CurrentValue
End Sub
I don't know why but .oldvalue doesn't work for me.
If you are in the same boat as me, you can use the BeforeUpdate event of the optionGroup and set a static variable.
Then read the static variable in afterUpdate event and reset it for the next change.

Clear Textbox on key press

Is there any way to clear the textbox on keypress like in excel.
I tried the following code but it clears the text when clicking on the textbox. I want it to clear it when a certain key is pressed.
Private Sub Text10_GotFocus()
Text10.Value = ""
End Sub
You could select the control's entire text content whenever that control gets focus. Then your keypress would replace the selected text.
If you want that to happen for every text box on every form, you can set "Behavior entering field" setting to "Select entire field". (In Access 2007, find that setting from Office Button -> Access Options -> Advanced, then look under the Editing heading of that dialog. For Access 2003, see this page.)
Not only will that setting be applied to form controls, but also to tables and queries in datasheet view. If that's not what you want, you can use VBA in your form's module to select the text for only specific controls:
Private Sub MyTextBox_GotFocus()
Me.MyTextBox.SelStart = 0
Me.MyTextBox.SelLength = Len(Me.MyTextBox)
End Sub
If you want to do that for multiple controls, you could create a general procedure:
Private Sub SelectWholeField()
Me.ActiveControl.SelStart = 0
Me.ActiveControl.SelLength = Len(Me.ActiveControl)
End Sub
Then call that procedure from the got focus event of an individual control like this:
Private Sub MyTextBox_GotFocus()
SelectWholeField
End Sub
Private Sub Field1_KeyPress(KeyAscii As Integer)
If KeyAscii = 65 Then Me.Field1 = ""
'Or: If Chr(KeyAscii) = "A" Then Me.Field1 = ""
End Sub
change the number to the ascii value of whatever key you are wanting to use to clear the field
Declare a Form Level variable
Private CanDelete as Boolean
When the TextBox receives focus, set CanDelete to True
Private Sub txtTest_GotFocus()
CanDelete = True
End Sub
On the KeyPress event, clear the text if CanDelete is True
Private Sub txtTest_KeyPress(KeyAscii As Integer)
If CanDelete Then
Me.txtTest = ""
CanDelete = False
End If
End Sub