I'm trying to get the following done.
I've a form with 7 fields which I want to be required before adding those to the record. The problem is that I use VBA to make every field empty if you change something in a field above. If I set the field as required in the table I always get an error when I'm running the VBA saying that one off the fields is required. Is there a way around this?
Gr.
Ralf
Use the BeforeInsert event:
Cancel = IsNull(Me!Field1.Value + Me!Field2.Value + … + Me!Field7.Value)
If Cancel = True Then
MsgBox "Missing field"
End If
Related
I have an Access database in which I store some data, and that database has 13 tables plus a reference one.
I want to make a form where there are several checkboxes and a button. Each chekbox represents a table, and every table selected will be joined inside a query writen in VBA, associated with the button click.
I've already made the same thing in Excel, and it works perfectly, so the only problem here is that I don't know how to access the checkbox value and use an IF condition to get the correct SQL string.
To make it clear, here I have a IF statement for one of the checkboxes in Excel:
If Range("B8").Value = True Then
CTODStrc = ", CTODTYPE, CTOD.TEMPERATURE, VALIDITY, DELTAR, DELTAL"
CTODStr = " JOIN CTOD ON REF.ID = CTOD.REF_ID"
JoinStr = JoinStr & CTODStr
Columns = Columns & CTODStrc
End If
SQLStr = RefStr & JoinStr 'Query sentence
The SQLStr is the query text, and it has a prior "select" string which is added.
In Excel, the cell B8 was associated with the checkbox, but in Access I have to make this condition using a checkbox thats in the form - how can I do it?
I've tried Me.CbName.Value, but it says the command is not supported.
Thank you.
The checked state of a checkbox is given by the Value property of the checkbox control. This property may be 0 (unchecked), -1 (checked), or Null for a block-filled triple state checkbox.
Since the Value property is the default property for a checkbox, and assuming you are not using a triple state checkbox, you should be able to use simply:
If CBName Then
' Do stuff
End If
I have a frmA, based on qryA.
Button btnOpenfrmB on frmA opens another frmB, based on qryB.
But this frmB includes some unbound textboxes, with data from qryC, i.e.
=DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]). If qryB has no data (records), Dlookup fields return errors and opening frmB, which includes these textboxes, ends with an error. I understand this behaviour of frmB (there is no value B, that's why an error), but HOW CAN I AVOID IT and correctly open frmB for entering the first record ? When qryB has at least one record, everything works OK.
Thanx in advance
Duski.
Just use Nz function if Field don't have, for instance 0 values:
=DLookUp("Field";"[qryC]";"[ValueC] = " & Nz([ValueB],0))
or iif function if Field can contain any value:
=iif(IsNull([ValueB]),"",DLookUp("Field";"[qryC]";"[ValueC] = " & [ValueB]))
I have a combobox on a form and I'm trying to programmatically select one of the items in the combobox after I've run my SQL query on the access database.
I use the following code to iterate over the items and set the selected item:
'Make the appropriate location appear in the combobox
For i = 0 To cboLocations.ListCount - 1
If Me.cboLocations.Column(0, i) = locindex Then
Debug.Print "locindex: " & locindex & vbCrLf & " Me.cboLocations.Column(0, i):" & Me.cboLocations.Column(0, i)
Me.cboLocations.SetFocus
Me.cboLocations.ListIndex = i '<<< error 2115
Exit For
End If
Next i
As indicated, I keep getting error 2115: The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Access from saving the data in the field.
Neither of the properties for this combobox indicated in the error message is set to anything. So I'm stuck. Please advise.
programmatically select one of the items in the combobox
The way I've always done that is to assign something to the combo's Value like this ...
Me.MyCombo.Value = "target text"
Value comes from the Bound Column of the combo's selected row. (You can find Bound Column on the Data tab of the combo's property sheet.) Conversely, assigning "target text" to Value selects the matching row.
In your situation, I think you're trying to select the combo row which contains the same text as your locindex variable. And if that is true, then I think all you need is this ...
Me.cboLocations.Value = locindex
As far as when you do that, neither Before Update nor Validation Rule seems like the right choice to me. I suggest you do it from whatever code you're using to run your "SQL query on the database", immediately after the query.
You are probably colliding with the BeforeUpdate event.
Try using AfterUpdate.
I am having some trouble with MS Access 2010. I am trying to modify a database I downloaded, a template from Microsoft‘s web site, I am doing a little modifying to keep track of the food nutrition. I have some experience with basic from the old days of the 16 bit Atari days. I’m not totally green to VBA, just know enough to get in trouble. I have a Form that uses a select query to populate lower half of the form. I added an attachment field to the foods table which has over 8500 record that is the table for the query. My problem is to add VBA code in a module to see if there is a photo present or not. I want to be able to show an icon grayed out for no photo and a regular icon if there is a photo file. But that's for when I get it work to begin with. I call the function in the field properties:
Photo1: chkAttachment([Photo])
This one to start with gives me and error saying: "The multi-valued field '[Photo]' is not a valid in the expression 'chkAttachment([Photo])'. When I change it to:
Photo1: ChkAttachment([Photo].[FileName])
I get and #Error for the empty fields and a "Has Photo" for the one with a file in it.
The Following code is the function I am referencing To Show what I am trying to do
Public Function chkAttachment(fldPhoto As String) As String
On Error GoTo chkAttachment_Err
chkAttachment = ""
'Debug.Print fldPhoto
If fldPhoto = Null Then
chkAttachment = "No Photo"
Else
chkAttachment = "Has Photo"
End If
chkAttachment_Exit:
Exit Function
chkAttachment_Err:
MsgBox " It don't like Error number: " & Err.Number & " " & Error$
Resume chkAttachment_Exit
End Function
The query won’t even call the code if the attachment is empty. I Googled the following question, and searched on this web site “Microsoft Access 2010 VBA how to query attachment field” without any luck. I have tried to use some code to count the number of files in the attachments, I found in the thread “How to query number of attachments from Attachment field in Microsoft Access?” from Aug 2011, but couldn’t figure out how to get it to work. When it comes to SQL, I’m in the dark with that part.
Thanks for any help that comes my way.
Steven
If you have an Attachment control named [attachPhoto] on your form and that control is bound to the [Photo] field (i.e., the Control Source of the Attachment control is Photo) then you can just check the value of
Me.attachPhoto.AttachmentCount
to see if the record has any attachments.
(Note that if you don't want the users to actually see the [attachPhoto] control you can just set its Visible property to No.)
You can check it in SQL statement " Not (tblxx.Pic.FileData) Is Null " probably in the where clause. I did it this way using a recordset.
I believe this is causing the #ERROR in your field
If fldPhoto = Null Then
Correct checking for Null would be the following:
If IsNull(fldPhoto) Then
But then you might still get a Null exception while calling the function. So you might want to edit your overall code to the following:
If (fldPhoto = "") Then
and when calling the function :
Photo1: ChkAttachment(Nz([Photo].[FileName]))
Hope I helped
I wanted to check if an attachment was made or not before running a query on a form. I made a text box in the form and under control source of the text box, went to expression builder and entered " = [name of column which has the attachment].[AttachmentCount]".
The text box gave me the count as 0(zero) if no attachment is made and 1 for 1 attachment. I could use the text box to condition run my query. All the above in Access 07.
I have a table in MS-Access - let's call it tComputers. Two of the fields in that table are titled Status - with the options of Active, Storage, Deactivated - and DeactivationDate.
If I wanted to make DeactivationDate mandatory if and only if the value of Status is Deactivated, how can I do that?
If I wanted to make DeactivationDate mandatory if and only if the value of status is Deactivated how can I do that?
Conversely, if a Deactivated record later changes Status ... say becomes Active ... should the DeactivationDate be discarded?
I think you can accomplish this with a table level Validation Rule. With tComputers in Design View, open the property sheet and use this as the Validation Rule property:
IsNull([DeactivationDate])=IIf([Status]="Deactivated",False,True)
Figure out what message you want the users to see when that validation rule is violated and put it in as the Validation Text property. It's unlikely the default message would mean anything to them.
Although it's possible, I doubt know how useful this is. The users should be editing data via a form, and that gives you the opportunity to enforce your validation requirements before the database
engine even attempts to save the data ... use the Form_BeforeUpdate event as #mwolfe02 described.
Edit: Here is an outline for Form_BeforeUpdate. It assumes you have a combo named cboStatus bound to the Status field, and a text box named txtRetireDate bound to the RetireDate field.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Select Case Me.cboStatus
Case "Deactivated"
If IsNull(Me.txtRetireDate) Then
strMsg = "RetireDate is required for Status = Deactivated"
MsgBox strMsg
Me.txtRetireDate.SetFocus
Cancel = True
End If
Case "Active", "Storage"
'* what should happen here? *'
Case Else
'* what should happen here? *'
End Select
End Sub
If you want to enforce this at the table level I believe you are out of luck. You might be able to do something with Data Macros if you are using Access 2010. EDIT: I stand corrected. Though I personally don't ever use this functionality (preferring to handle the validation at the form level where more complex validation is practical) it is most definitely possible: Validation Rules
If your users will only be updating the data through a bound form, you can perform the validation in the Form_BeforeUpdate event. Then if Status = 'Deactivated' and DeactivationDate is Null, you would set Cancel = True which will prevent the changes from being saved. Obviously you'd want to show a message box or indicate in some other way why the form could not be saved.
If you are entering the data with a form, you could use VBA to validate the form entries. On the button press to save the data you can check to see if the value of status is Deactivated, and if it is, you can require DeactivationDate to have a value. Otherwise, the data won't be saved.
Doing what you want in the table design window, I'm not sure how that could be done.