MS Access - Validation rule for unbound text box - ms-access

I have a Textbox2 in FormX which updates after I enter the value in Textbox1 using Afterupdate for Textbox1.
Textbox2 is unbounded text box.
Now I am trying to set up validation rule for the text in Textbox2. If Textbox2 contains word "Hold", it should pop up a message box saying "Invalid Entry".
Is it something possible? I could not find any reference online.
Can anyone please help?
Thank you,

Including code would be nice, but based on what you wrote, I would think you could add a check to your after update event in textbox 1 that occurs after the text is written to textbox 2 that will fire off the msgbox if it is invalid and then clear the entry in textbox 1.
Private Sub Textbox1_AfterUpdate()
If Textbox1.Value = "abc" Then
Textbox2.Value = "hold"
End If
If Textbox2.Value = "hold" Then
MsgBox "Invalid Entry", vbOKOnly, "Wrong!"
Textbox1.Value = ""
End If
End Sub

Related

Sorting a text box in continuous form using a command button

I'm building a form in continuous form format, and I want to add a button that sorts a specific text box in ascending, and when I click again in descending.
I've tried to do the following code but it doesn't seem to focus on a specific text box.
Private Sub cmdSort_Click()
Me.OrderBy = Me!lblTaTtxt.tag
DoCmd.RunCommand acCmdSortDescending
Me.OrderByOn = True
End Sub
I expected it to focus on the text box connected to "lblTaTtxt" and to sort the text box, but it sorts a different box in the form. Would appreciate some clarification, as I am very new to Access
Once you click on the label lblTaTtxt it will sort the connected textbox
Private Sub lblTaTtxt_Click()
Dim strDirection As String
strDirection = Me.OrderBy
If strDirection Like "* DESC" Then
DoCmd.RunCommand acCmdSortAscending
Else
DoCmd.RunCommand acCmdSortDescending
End If
Me.OrderByOn = True
End Sub

Prevent Form Close

I am using Access 2007 and have the following code in the close event:
If [field1] = "" and [field2] = "" then
Msgbox " You must enter a value in either field 1 or field 2 "
Cancel = false
Else
Cancel = true
End If
I am trying to prevent the user from disregarding the message and closing the form via the close X button and saving the record. There have been some previous threads about this but I am unable to figure out the exact syntax.
Any assistance is greatly appreciated!
You probably must check for Null:
Cancel = IsNull([field1] & [field2])
If Cancel = True Then
Msgbox "You must enter a value in either field 1 or field 2."
End If

MS Access Msg box VBOKonly if number field data is greater than 1

I want Msgbox for user to remind something if user enters a number greater than 1 in a form Number field.Any help would be appreciated
If you want to prevent the user from entering a number > 1, set "<=1" as validation rule for the field, and your Msgbox text as validation text.
Otherwise use the AfterUpdate event of the form field:
Private Sub myNumberField_AfterUpdate()
If Me.myNumberField > 1 Then
MsgBox "Hello user, I have a message for you!", vbInformation
End If
End Sub

Add a record to Access with text from a text box

In MS-Access, I have a form with textboxs and a button, and I want it so that when I click on the button, it will add the data in the textboxs to a new record.
How do I do this?
Note: I do not have much experience in Access
To accomplish this using an unbound form.
Assume your destination table is named Table1 and has the following format:
Field Name Data Type
------------- ---------------
ID AutoNumber
Field1 Text
Field2 Text
Assume your form is named Form1 and has the following controls:
Control Name Control Type
------------- ---------------
Text0 Text Box
Text2 Text Box
Command4 Command Button
Finally the On Click event of the Command4 control is assigned the following Event Procedure:
Private Sub Command4_Click()
Dim rs As Recordset
On Error GoTo ErrHandler
If MsgBox("Do you want to add the record.", vbQuestion + vbYesNo, "Add New Record?") = vbYes Then
Set rs = CurrentDb.OpenRecordset("Table1", dbOpenDynaset)
rs.AddNew
rs.Fields("Field1").Value = Me.Text0.Value
rs.Fields("Field2").Value = Me.Text2.Value
rs.Update
MsgBox "Record saved.", vbInformation, "Record Saved"
End If
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "Error Occurred"
End Sub
This code will first prompt the user to confirm that the record should be saved. If the answer is yes the record will be inserted into the table and a confirmation message will be displayed. If an error occurs, the code will jump to the ErrHandler section and display a message box with the problem.
There are two ways you can do it.
Firstly you can make the form a bound form, so that the form is bound to a table (form RecordSource), and you bound the texboxt to a field in table.
Secondly you can write some VBA code that will insert the record into the table, using an INSERT INTO sql statement

VBA isnumber istext function problem

im trying to check if a number is a text or number in a banking system. If the value is incorrect it should erase and send an error message, but my code isnt working at all... can someone help me? here is the code:
Private Sub TextBox1_Change()
name = TextBox1
If Application.IsText(name) = True Then
IsText = True
Else
MsgBox "Insert only words"
Selection.name.Delete
End If
End Sub
I think you should be using name = TextBox1.Text instead. It's probably giving you an error on that line.
Also, you may want to wait until the user is finished typing the response, I believe the changed event will run each time a character is pressed. The AfterUpdate will run after you tab or click away from the textbox.
And really, instead of deleting the response (which I don't think will work), you should simply set the textbox blank back to an empty string.
Private Sub TextBox1_AfterUpdate()
If Not MyIsANumber(TextBox1.Text) Then
MsgBox ("It's Text")
Else
TextBox1.Text = ""
End If
End Sub
Function MyIsANumber(str As String) As Boolean
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
MyIsANumber = True
Exit Function
End If
Next
MyIsANumber = False
End Function
I'm sure the code could be structured better, but there's something along the lines of what you need. In this case, it just wipes the textbox if there is a number entered. Obviously, things will need to be the opposite for your textbox that you want to contain a number.
Have you considered the vartype() function? https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function