Preventing a user from entering anything but integers into a textbox - ms-access

I am constructing a form in Access and I have several textboxes that require an integer value. Right now you can enter characters instead of number and a debugging window pops up when the submit button is pressed. How can I prevent users from being able to press the submit button when an invalid entry is made in the interger textbox.
Thank you!

First, make the field a Number type in the table design. Then go down to the "Field properties" and make sure "Field size" is set to Integer. This takes care of decimal numbers because it will round them to the nearest integer when the textbox loses focus.
Next, go into the submit button properties and set Enabled = False. This will gray it out and won't allow the user to click it. You may want to add this code to the form's On Current property so that whenever a record is loaded, it will enable or disable the submit button based on the value that is already there. You will need to "AND" in more integer textbox objects to the same if statement if you have more than one integer textbox on your form:
Private Sub Form_Current()
If Not IsNumeric(Me.IntegerTextBox.Value) Then
Me.SubmitButton.Enabled = False
Else
Me.SubmitButton.Enabled = True
End If
End Sub
Finally, add this code to the On Change event for the textbox. This will enable the button whenever an appropriate a numeric value has been entered and will update the button every time a key is pressed.
Private Sub IntegerTextBox_Change()
If Not IsNumeric(Me.IntegerTextBox.Text) Then
Me.SubmitButton.Enabled = False
Else
Me.SubmitButton.Enabled = True
End If
End Sub
Do this for each integer field on your form.

In table Design make the data Type Number (Integer).

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub

Textbox Form Field Required

I inserted the following VBA code to my textbox in a form. It only works if i type something then delete what i typed. If i go to the next box, then the error message comes out. Is there any way to require the textbox is filled out?
Private Sub master_bill_a_BeforeUpdate(Cancel As Integer)
If IsNull(Me.master_bill_a) Then
Cancel = True
MsgBox "You must enter a value for 'YourControlNameOrYourDescription'.
Please make a valid entry or press ESC to Cancel"
'You could set the focus on the specific control if your wish, change
the background color, ...
End If
End Sub

Change caption of button to field in a table using Access

I am trying to make the caption of a button the content of a field from a table.
Everything I am finding online in forums and MS websites only shows using a macro with the OnClick. I need it to display without every clicking. It also needs to dynamically change every time the field from the table changes.
How can I achieve this? Again, I don't want an event such as OnClick, I want it to simply read as the field from the table reads.
You could simply use the Form's OnCurrent event to set the buttons caption.
On Current:
Private Sub Form_Current()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub
This will make it so ever time the selected record changes the button will update. Also just add the code into the specific field's AfterUpdate event so it changes then as well.
After Update:
Private Sub Textbox1_AfterUpdate()
If Nz(Me.FieldName, "") <> "" Then
Me.btnTest.Caption = Me.FieldName
Else
'Handle blank record here
End If
End Sub

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

Update on Change

I have a textbox(1) wich automaticly calculates and displays the input after a textbox(2) is updated.
Textbox(1) is set to enabled = false so the user can not input any values as it updates automatically.
I want to display the number in Textbox(1) after it has changed/updated.
I have tried using the AfterUpdate property for Textbox(1) but does not work because the user is not physically updating the value.
Is there any way to detect a change when the number changes in the textbox and store the number in another textbox?
As already said by others, if the user enters stuff into Textbox2 and you want to display that in Textbox1, then you have to use the events of Textbox2, not Textbox1!
The easiest way would be to use the AfterUpdate event of Textbox2.
Example:
Private Sub Textbox2_AfterUpdate()
Me.Textbox1 = "Text from second textbox: " & Me.Textbox2
End Sub
You can either use the OnLostFocus (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onlostfocus.aspx) to tell when the user's focus leaves the textbox.
Otherwise you can use OnKeyPress (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx) and a timer to tell how long since the last keystroke.