I was wondering if anybody would be able to tell me how to check if multiple fields in a form are valid or not. Basically I want to set up a validation rule for my last field to check if all of the previous fields are blank or not.
I have already tried this:
IsNull([FieldName])
I know that this is only for one field, but I can't even get that to work. I am pretty new with access so any help would be much appreciated. Thank you!
What i would do is in the tag property for each control you want to validate, put a word in there that you can check for (example - enter "VALIDATE" for each control you want checked). To find the tag property: look at the property sheet for each control, in the OTHER tab, the last field listed = tag).
Then as code in the afterupdate event of the last control, select EVENT PROCEDURE and enter this code:
Dim ctl As Control
For Each ctl In Forms!YourFormName
If ctl.Tag = "validate" Then
If IsNull(ctl.Value) Then
ctl.SetFocus
MsgBox "You must complete the " & ctl.Name & " field.", vbOKOnly, "Required Field Missing"
GoTo exitsub
End If
End If
Next
exitsub:
Exit Sub
If this finds and empty field, it will move back to that field, (set focus) and display a message box that states they must enter a value.
Related
This is my first question, so I hope I get it right.
I am working on a project to review Employee information. I am using two forms, the first one is "ReadOnly," the second is "EditOnly."
The "ReadOnly" form is bound to a query, which defines criteria for specific records to be displayed.
The "EditOnly" form has a subform, bound to 2 tables (which are also part of the query that is the source for the "ReadOnly" form), that have a 1-to-many relation (using info from this answer:
Error 3251 on .oldValue control property).
The "EditOnly" form is opened via a button and is used to edit the data of a specific Employee. In this form I have an Undo button, using this code:
Sub UndoEdits()
If Me.Dirty Then
Me!btnUndo.Enabled = True
Else
Me!btnUndo.Enabled = False
End If
End Sub
Sub btnUndo Click()
Dim ctlC As Control
For Each ctlC in Me.Controls
If ctlC.ControlType = acTextBox Then
ctlC.Value = ctlC.OldValue
End If
Next ctlC
End Sub
which reverts unwanted changes. But when testing the function, after changing some data I get run-time error 2448. Using Debug highlights this row:
ctlC.Value = ctlC.OldValue
Is there a way to make the Undo button work?
Error 2448 is "Can't assign a value to this object".
You will get this error if you try to assign a value to any text boxes on the form that are read only/locked or disabled.
Modify your code to check ctlc for Enabled and Locked and skip any controls that aren't assignable, like this:
Dim ctlC As Control
For Each ctlC in Me.Controls
If ctlC.ControlType = acTextBox Then
If ctlC.Enabled And Not ctlC.Locked Then
ctlC.Value = ctlC.OldValue
End If
End If
Next ctlC
Debugging Tip:
You can check the value of ctlc.Name either by stepping or by writing to the immediate window (Debug.Print ctlc.Name) to see which text box is the source of the problem.
Good afternoon,
I have to maintain an Access form which contain a linked subform (using Master and Child fields).
Basically, in the main form, the user choose a value in a combobox and the subform is automatically updated.
My issue is that I have a BeforeUpdate event on one field of my subform which is preventing to update the field (Cancel=true) when it does not meet the criteria. The alert msgbox should appear once if there is any error in the field but the BeforeUpdate event is always fired 3 times for unknown reason.
I have created a simple accdb file which reproduce my issue. It is located here: https://www.hightail.com/download/bXBhU2V0Q1JxRTFsQXNUQw.
Open the Form1, choose a value in the combobox and then try to update one of the letters in the subform by X and you will get the msgbox appearing multiple times.
Thanks in advance for your help on this issue as it's driving me crazy.
Sylvain
This happens because you do not change the value of Text0 back to what it was.
Try this
Private Sub Text0_BeforeUpdate(Cancel As Integer)
If Me.Text0 = "X" Then
MsgBox "Wrong value"
Cancel = True
Me.Text0.Undo
End If
End Sub
OK finally found the solution.
It was related to the text box used for storing the Link master fields used by my subform. This control contains the value of my combobox in order to filter out my subform.
The control source definition of this link master field used to be:
=myCombobox.column(0)
By replacing the definition by just:
=myCombobox
The beforeUpdate event in my linked subform is only triggered once and behaves as expected;
Private Sub Text0_BeforeUpdate(Cancel As Integer)
If Me.Text0 = "X" Then
MsgBox "Wrong Value"
Cancel = True
End If
End Sub
I can not explained the magic of why it is behaving that way but this is how I made it worked.
Sylvain
I have a database form that is linked to a table.
Users create records where data is inputted into a field called "seq.".
The users will need to change selected fields within the record, but the "seq." needs to remain unchanged, under most circumstances.
I want to have an on.change event where a message box comes up stating "you are about to change the seq. field, please confirm change" that makes the user aware that they may be changing this information.I need help creating this.
You need to make use the Control's Before Update event. It should disallow any changes, if it is meant to be.
Private Sub controlName_BeforeUpdate(Cancel As Integer)
If MsgBox("You are about to change the seq. field, please confirm change" _
vbYesNo) = vbNo Then
Cancel = True
Else
MsgBox "Changes have been confirmed."
End If
End Sub
I have multiple fields on a form that have no values associated with them until data is entered into that field. At which point those controls have the #Name? value. All I'm trying to do is hide the fields using a loop as opposed to writing If statements. Here is the code working with a single If statement to hide said field/fields.
If Not Me.txtField.ControlSource = "#Name?" Then
Me.txtField.ColumnHidden = True
End If
I can't seem to figure out how to hide multiple controls that have the #Name? flag. Any help would be appreciated.
Dim ctl As Control
Dim ctlError As String
ctlError = "#Name?"
For Each ctl In Me.Controls
If Not ctl.ItemsSelected(txtField) = ctlError Then
ctl.ColumnHidden = True
End If
Next ctl
EDIT:
On the main form there is a subform with a cross tab query. But the problem is results on the query are populated from a combobox. So I've added fields that are not yet available in the query to the subform and end up with #Name? Attached is a screenshot to better illustrate the issue.
There is a 90% chance I'm going about this process wrong, so it's a learning process right now.
It would be easier to address this question if you described the specific errors that are raised by your code. This answer assumes that the control in question is a TextBox.
"#Name?" means that a control's ControlSource refers to a field that is not in the form's RecordSource. For example, if your query has two fields (ID and BirthDate, for example) and your form has three controls, with ControlSource of "ID", "BirthDate", and "BirthCountry", then the control whose RecordSource is "BirthCountry" will show "#Name?". However, its Value is not "#Name?". Rather, calling the Value property raises error 2424 (with the rather unhelpful message "The expression you entered has a field, control, or property name that Microsoft Office Access can't find").
The only way I know to check for the string "#Name?" is through the control's Text property, which is only available when the control has the focus, so that's not much help. If you retrieve the value of the ControlSource property, you'll get "BirthCountry" (of course). So you could check all ControlSources on your form against the fields of the form's RecordsetClone (add error handling, please):
For Each ctl In Controls
If Not RecordSourceContains(ctl.ControlSource) Then
ctl.Visible = False
End If
Next
Function RecordSourceContains(strFieldName as String) As Boolean
Dim fld As Field
For Each fld In RecordsetClone.Fields
If fld.Name = strFieldName Then
RecordSourceContains = True
Exit For
End If
Next
End Function
You'll probably want to do some other bookkeeping. For example, you'd want to check for expression control sources (which begin with "=") because they won't be in the RecordSource. You'll also either need to handle error 438 (Object doesn't support this property or method) or check each control's type to make sure it has a ControlSource property.
In design view for the form, what does the field say for Control Source? It should be blank, meaning the form is unbound. If it's not, then that explains #Name.
More information: http://www.databasedev.co.uk/unbound-forms-add-data.html
Here is code I use to take care of that:
Sub setControl
'' You can use this in your loop
Call FieldEnable(subFrm, "boxQty", booSellSet)
End Sub
''----------------
Private Sub FieldEnable(subFrm As Access.Form, strCtlName As String, _
booEnableMe As Boolean)
On Error GoTo errHandler
subFrm(strCtlName).Enabled = booEnableMe
subFrm(strCtlName).Visible = booEnableMe
errExit:
Exit Sub
errHandler:
If Err.Number = 2164 Or Err.Number = 2165 Then ''control has the focus
Resume Next
Else
DoCmd.Hourglass False
Screen.Application.Echo True
MsgBox "Problem with control named " & strCtlName
Resume errExit
End If
End Sub
I have to adjust many fields on the form, so this is very reusable. I might be turning them "on" or "off"; this takes care of either. Let me know if you have questions.
It all boils down to this method for hiding the field:
Control.Enabled = False
Control.Visible = False
Delete the field Black from table.
And then make make again the Black field in your table.
I'm trying to password protect a submit button on a form I'm building in Microsoft Access 2003. The button, when clicked, will add a new record to my database. The idea of the password protection is that when the user clicks the button, a prompt will appear asking for a password. They can either enter the password and click OK to proceed with verifying it has been entered properly, or they can click Cancel and close the prompt window (after which they will receive an confirmation alert). If the password matches what is hardcoded, the record will be added. If the password is not a match, an error message will display.
This should be easy enough. However, the record will ALWAYS be added to the database, no matter if the password is entered incorrectly, no password is entered, or the user cancels out of the password window. What am I doing wrong with the below code?
Private Sub AddLeadServerButton_Click()
Dim strPasswd
strPasswd = InputBox("Enter Password", "Restricted Form")
'Check to see if there is any entry made to input box, or if
'cancel button is pressed. If no entry made then exit sub.
If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInformation, "Required Data"
Exit Sub
End If
'If correct password is entered open Employees form
'If incorrect password entered give message and exit sub
If strPasswd = "thepassword" Then
DoCmd.GoToRecord , , acNewRec
Me.Parent!NewInstallation.Form!InstallationLeadServerComboBox.Requery
Me.Parent!NewReport.Form!LeadServerFilterComboBox.Requery
Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If
End Sub
To accomplish the behavior you want, you will have to set the form to prevent new records from being added. Then, ask the user for the password, set the form back to enable adding new records, and move to the new record.
If the property sheet is not
displayed, on the View menu, click
Properties to display the form's
property sheet.
In the Form property sheet, click
the Data tab, and then set the
AllowAdditions property to No.
Add a command button to the
form. Set the command
button's OnClick property to [Event
Procedure], and then click the Build
button to the right of the OnClick
property box. Type the following
statement in the Form_Customers
module:
Forms!Customers.AllowAdditions = True
http://support.microsoft.com/kb/208586
I don't recommend checking for the password after the user has already entered data. It gets frustrating when you fill out a form, only to find out at the end that you don't have rights to save your work.
Hook the BeforeInsert event on your form, and add the password check there. You can set Cancel=True if they don't provide the correct password, and that will cause the addition of the record to be abandoned.
Example:
Private Sub Form_BeforeInsert(Cancel As Integer)
If MsgBox("Insert new record here?", _
vbOKCancel) = vbCancel Then
Cancel = True
End If
End Sub
Try to avoid using bound forms where you can, this will give you greater control over your data with a little extra work, the best way to think of it is addressesing your data from opposite ends.
Bound forms are very much about telling the database to prevent updates that you don't wish to make whereas unbound forms are more about not making any changes until you are absolutely happy.
Just a personal preference but I think unbound are worth the time and effort.