Access 2007 continuous form - only matching row editable - ms-access

Good afternoon,
I've got a continuous form that displays records for any number of selected employees. I'd like for only that employee's row (or more specifically a particular text box in their row) be editable and no others.
I thought about doing something like this.
Private Sub Form_Load()
If Me.txtResponse <> [Forms]![Home].txtEmployeeName Then
Me.txtResponse.Locked = True
End If
End Sub
and I get an error that I entered an expression that has no value - and it highlights the me.txtResponse.
I don't know if i'm barking up the wrong tree or if this is even possible in a continuous bound form. Any ideas?

Form_Load is too early for that code. Move it to the Form_Current event and it will run when first opened and again with each record navigation. You'll want to add
Else
Me.txtResponse.Locked = False
in their to allow changes when a match is made.

Related

Access report details - change visibility of an object for each row

This is my first question on this forum, I'm a very novice databaser, don't have coding skills unfortunately, but I can macro ok and I'm pretty handy with a cut-and-paste VBA code :-)
I have a report within a form, and essentially I want icons to represent if one of any 4 data elements are present for each row of the report - as represented by 'X's in my picture.screenshot For example I allow the user to set a reminder date to check on new results - I want an icon to be visible if they've set a reminder for that row.
So far I've tried a few things without success, including having a subreport within the report detail with an 'if data present then browse to' macro (in either the report/"onLoad" or detail/"onPrint" events, or an 'if data present change visible property' macro for a picture or other report detail element. In short, I can't find a way to change anything in the detail section of the report per row other than the pure data contained within the referenced tables.
I'm sure there's a simple solution to this which I just haven't thought of yet, but any suggestions much appreciated!
You can do this with the OnFormat event of the detail section. Assuming you have a field in the recordsource called optFlag and a label in the detail section called lblHello:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If ([optFlag]) Then
lblHello.Visible = True
Else
lblHello.Visible = False
End If
End Sub
The trick is that this event only fires when you open the report in Print Preview. It will not fire when you open the report in Report View.

BeforeUpdate event of a linked subform control firing multiple times

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

Use sorted listbox to select first record in list, not in database

In an Access form, I'm using a listbox to select the record I want to view (which is working fine, manually). The listbox is populated with names in a query that sorts them alphabetically, and that works fine.
Using VBA, I have the listbox selecting the first item in the box both when the form first loads, and when the user clicks a button to requery (the user can narrow down the list of names, by age, on the form itself.
When the form first loads, it has no trouble loading the record of the first item in the listbox. When the Search button is pressed, the form loads the first record in the database that matches the criteria, even if it doesn't match the first item in the listbox.
This is my code for both events:
Private Sub btnSearch_Click()
DoCmd.RunMacro "Requery", 1
Me.listControl.SetFocus
Me.listControl.Selected(0) = True
Me.listControl = Me.listControl.ItemData(0)
End Sub
Private Sub Form_Load()
DoCmd.RunMacro "Requery", 1
Me.listControl.SetFocus
Me.listControl.Selected(0) = True
Me.listControl = Me.listControl.ItemData(0)
End Sub
What am I doing wrong?
I didn't know this feature and tried it at work today. It generates an embedded macro. It is also only a one-way toggling, i.e. the listbox controls the form but not vice versa. I guess that's where your problems started in the first place.
You will have to do the following:
Keep ListBox and Form recordsets equivalent, i.e. if you filter one, filter the other also. You will run into crazy situations, if not.
Throw away the embedded macro and use the FindFirst method in your AfterUpdate event (best in the module of the form, not in the listbox props)
Wire up the ListBox so it always shows the selected record in the form by selecting the correlated row in the form's Form_Current event
Catch an endless loop by doing 2 and 3 only if the ids of both are not the same
If you can't get it to work, post the code where you are doing the filtering (and explain how) and I'll have a look tomorrow.

Make fields visible in MS Access form as certain fields are completed

I am building a form In MS Access for users to input data but there are too many possible fields. Most of the time only about half the fields will be used.
I thus would like to have certain fields appear only depending on what the user inputted on a prior given field.
Ex: user enters project number, title, then he checks a "yes/no" engineering. since he checked it this means engineering is impacted so a dozen fields that the user will have to fill out appear.
Is this possible:
1)without VBA
2)with VBA
Probably not possible without VBA.
With VBA for example:
Ensure your form is in Design view
Right click on your Combo Box, Build Event, Code Builder
This opens the code behind your form. It drops you into the default code for the BeforeUpdate event. We want the Change event instead, so at the top right change the drop down from BeforeUpdate to Change. This will give you a bit of code like this:
Private Sub Field1_Change()
End Sub
Inside here, you want to check the value of the combo box and hide fields as required:
Assuming the name of your combo box is Field1 (yours of course will be different), you add some code so it looks like this to hide Field2:
Private Sub Field1_Change()
If Field1.Value = "Yes" Then
Me.Field2.Visible = False
End If
End Sub
Note you need to know the names of all your fields - this is in the Other tab, Name field in the properties box (F4). You should give all of your fields sensible names so you can understand what is going on in the code.
For a check box, follow exactly the same procedure, but you probably need to use the Click event. Just experiment.
Sample check box code:
Private Sub Check5_Click()
' Note: vbTrue = -1
If Me.Check5 = vbTrue Then
MsgBox ("Ticked")
Else
MsgBox ("Not Ticked")
End If
End Sub
I have a form that will show certain fields after a list box value is selected. I use the AfterUpdate function. It has worked so far. My code is below. ProjectName and ProjectNumber are fields you only want displayed if Engineering is selected. OtherName and OtherNumber are fields you only want to show if it is a "NotEngineering" project.
Insert this code by clicking on the Field that selects the project type, go to the Event tab on the property sheet, and click "After Update" and choose code builder and paste in VBA.
Private Sub ProjectType_AfterUpdate()
If ProjectType.Value = "Engineering" Then
Me.ProjectName.Visible = True
Me.ProjectNumber.Visible = True
Else
Me.ProjectName.Visible = False
Me.ProjectNumber.Visible = False
End If
If ProjectType.Value = "NotEngineering" Then
Me.OtherName.Visible = True
Me.OtherNumber.Visible = True
Else
Me.OtherName.Visible = False
Me.OtherNumber.Visible = False
End If
End Sub
There is a way to do not-quite-this without VBA. I'd definitely recommend VBA though, because you can do a lot more with it.
Rather than hiding, try disabling the unnecessary fields with conditional formatting by expression.
right-click on the control you want disabled.
go down and click on 'Conditional Formatting'
Add a new rule
Select 'Expression is'
example:
[fld1]="yes"
hit the disabled box
click ok
click ok
now the control you've selected will disable if field 1 has "yes" selected.
I have the same problem and I did the following:
Private Sub Field1_Click()
If Field1 = "Yes" Then
Me.Field2.Visible = True
Else: Me.Field2.Visible = False
End If
End Sub
but now I have other problem, when I change record, the field that I choosen to be visible in the last record is now visible on the current record, although I have not choosen any option.
Thank you,

MS Access - Write to Table Immediately After Changing Value in Form

Is there a way to make Access write values immediately to a table when changing values in a form? Right now, I have to change a value in the form, and then click off the field onto the subform before the value is written to the corresponding table.
Sounds like no big deal, but certain controls require current data to give valid options. If the user doesn't know enough to click into the subform then the data they view could be out-of-date.
The behavior in your requirement are asking for is by default how access functions. In most typical scenarios your master form is going to be the parent table, and as a result before you can add child records to such a relational setup, then the parent record would have to be written and saved to disk.
And the reverse while not a requirement is also the default operation for access.
In other words it's not clear why when the focus moves from your parent form to a sub form, that the parent form's record is not been written to the table.
So something in your setup is incorrect here, or you'll have to expand on the behavior you're witnessing.
By default changing the focus from the main form to a sub form will cause the main form record to be written to the table, and changing the focus from a sub form tool main form as a general rule should also cause a sub form record to be written to the table.
If you are matching a combo box, you can use that as your link master field, that is, the name of the control itself. Your subform should update as soon as you select a ticket with the combo.
Link Master Fields: Combo1
Link Child Fields: Field1
You can force the save of the record when a user moves off a field in the Lost_Focus event.
Private Sub MyField_LostFocus()
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End Sub
I was also facing the same problem, but in another scenario. I am not having sub-form, but a datasheet view of the same table on a split screen, which was not updating immediately after save button. On pressing DUPLICATE button, instead of getting duplicate data of latest record, I was getting pointer shifted to first record, and then the data from the first record was getting duplicated.
By inserting
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
before data duplicating code, I am now getting the data updated immediately with record pointer stick to the current record, and the same last record gets duplicated as required.
Sharing the experience.
Thanks ChrisPadgham
Just use this in your form model:
Private Sub {your textbox name her}_AfterUpdate()
Me.Refresh
End Sub
Essentially: in the AfterUpdate event of every desired control execute a acCmdSaveRecord command.
So:
In the design view of the form click on your control.
Menu > Design > Property Sheet (to bring up the property sheet for your control)
Property Sheet > Event [tab] > After Update. Choose "[Event Procedure]". Click on the elipsis "..." to take you through to the Code-Behind-Form VBA dev environment.
Supply your code as follows ...
Private Sub [Control Name]_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
E.g.
Private Sub cboFinancialYear_AfterUpdate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Repeat the above for every relevant control on the form.
The acCmdSaveRecord avoids an unecessary visual refresh of the current form, as when using Me.Refresh (which #medjahed mohamed has suggested).