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
Related
I would like to find the best way to have a text box displays a message everytime another control (a button) is pushed. Each time the button is pushed, the message will change and that message should show in my text box. The trick I would like to do is after the user stops pressing the button, that after a certain period (3 seconds) the text box will disappear.. (perhaps the message can be deleted). What is the correct event to use ?
Basically, for each control named 'msgPrincipio' in the code below, i would like that message to appear within the text box for 3 seconds and then disappear:
Private Sub Form_Timer()
Dim intTimerStart As Integer, intTimerUsed As Integer
Dim intCountdown As Integer
On Error GoTo Err_Handle
If Me!msgPrincipio <> "" Then
If intTimerStart > 0 Then
intTimerUsed = CLng((Timer / 60) - intTimerStart)
Else
intTimerStart = CLng(Timer / 60)
End If
If intCountdown > 3 Then
Me!msgPrincipio = ""
End If
intCountdown = intCountdown + 1
End If
Err_Exit: Exit Sub
Err_Handle: Resume Next
End Sub
Dim intTimerStart as Integer, intTimerUsed as Integer
Dim intCountdown as Integer
Sub Form_Timer()
On Error GoTo Err_Handle
If Me!MyBox <> "" Then
If intTimerStart > 0 Then
intTimerUsed = CLng((Timer / 60) - intTimerStart)
Else
intTimerStart = CLng(Timer / 60)
End If
If intCountdown > 3 Then
Me!MyBox = ""
End If
intCountdown = intCountdown + 1
End If
Err_Exit: Exit Sub
Err_Handle: Resume Next
End Sub
You also need to go to the form's design view and set the "Timer Interval" property on the form to an appropriate value. This code assumes 1,000 (1 second).
You almost never want to use Resume Next, but it's good here -- the goal is to pass through this block of code as seamlessly as possible. (Which you can accomplish with simple On Error Resume Next at the start -- but I don't like seeing it in my code that way, not one bit. I do this so I'll easily recognize it's by design, not carelessness.)
New to Access' Form Timer?
Private Sub Form_Timer()
Debug.Print Time ' Update time display.
End Sub
Put this code in the form's VBA module. Return to the form design view and switch to form view. Now go back to VBA and check your Immediate window. You should see evidence the form timer event is kicking. Note the Timer property of the form (found under form properties, design view) must not be blank or zero. It needs an entry to kick.
Using C#
using System.Windows.Forms;
public partial class Form1 : Form
{ private Timer x = new Timer();
public Form1()
{
x.Interval = (6000); //1 second = 1000
x.Tick += new EventHandler(TimerTask);
x.Start();
}
private void TimerTask(object sender, EventArgs e)
{
TextboxName.Text = String.Empty;
}
To set a label content dispear automatically: https://gamespec.tech/how-to-clear-textbox-after-few-seconds-in-c-sharp/#3-set-label-content-and-make-it-disappear-automatically
I have a Main Form with 3 fields, where the rep would enter the Property, Room Type, and the requested Check-in Date - however on the same form i have a subform displaying Property, Room Type and Check-in Dates NOT AVAILABLE. If a rep enters data in those 3 fields and ALL 3 match what is in the NOT AVAILABLE subform - what code (either VBA or On Lost Focus etc) can i use to look up those values in the subform (so that if it matches what's in the subform it will not allow for submission) and also popup an ERROR message that the "Property, Room Type and Check-In Date you selected are not available" ?
(Main Form data entered will go into a table let's say RoomRequestTable and Not Available subform displays data from another table RoomNotAvailableTable)
Below is some sample code utilizing the BeforeUpdate event. I'm assuming you want to trigger this from any of the fields, so I wrote a sample function that returns false if all of the fields match. We access the subform by calling Me.[SubFromName].Form.
Private function validate() As Boolean
Dim sbfrm As Form
Set sbfrm = Me.SubFormName.Form
If Me.FieldName = sbfrm.FieldName And Me.FieldName2 = sbfrm.FieldName2 And Me.FieldName3 = sbfrm.FieldName3 Then
MsgBox "you can't do that", vbExclamation, "Sorry kid"
'return false
validate = false
Else
'return true
validate = true
End If
End Function
Private Sub ControlName1_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName1.Undo
End If
End Sub
Private Sub ControlName2_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName2.Undo
End If
End Sub
Private Sub ControlName3_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName3.Undo
End If
End Sub
I have an access form with a text-box that user can update using a list-box or by editing it directly, I need to make an action if the user starts to delete from this text-box, if he starts to add data no problem only if he starts to delete from it, something like :
Private Sub textbox_AfterUpdate()
If Me.[textbox].SelStart = Len(Me.[textbox].Text) - 1 Then
' do something
else
' do something else
End If
End Sub
You might want to use the KeyDown event instead of AfterUpdate
This captures the Backspace and the Del button.
Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 8 Or KeyCode = 46 Then
MsgBox "Text was deleted"
End If
End Sub
There can be other scenarios where the text is deleted for example Ctrl + X So you can trap all that.
You might like to consider the change event, the text property is the current unsaved content of the control that has focus and the value property is the saved content of the control with focus.
The advantage of Change is that the user cannot simply insert letters into the existing string, for example, the user cannot change 20 meters to 200 meters.
Private Sub AText_Change()
''If the current content is the same as the
''previous content with something added
If Me.AText.Text Like Me.AText.Value & "*" Then
''Ok
Else
''Problem
MsgBox "Not allowed"
Me.AText = Me.AText.Value
End If
End Sub
You could also allow the saved content to be a substring of the current content.
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
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