How to add new record to subform - ms-access

I recently got help on When select value from combo, highlight that record in Access subform datasheet and now I'm trying to do a related task.
How can I make the subform give me a new row for data entry from a button on the main form?
Tried the method found here with no luck: http://www.access-programmers.co.uk/forums/showthread.php?t=26537
EDIT: Not sure if it matters, but the table my subform is supposed to update is a linked table to SQL Server.

Please first make sure your recordsource is updateable by opening the table or query in which your subform recordsource is based, and ensure that you can edit and add new records. If not, then it's possible that your SQL Server linked table is missing a unique indentifier. To create one, simply delete the linked table, and relink it. Upon relinking, MS Access should prompt you to specify a unique index (may take more than one field to make it truly unique). Then try again.

If the subform datasheet is editable and with AllowAdditions turned on, it should already give you the * button to add a new row.
If it doesn't, then you need to check the datasheet's properties and change them to match what you're actually seeking.

it has been a while since I used Access. Doesn't the RecordSource property of the subform give you the underlying table or query. If the source is editable, you should be able to add a record then Requery or Refresh the subform.

For those who need just to add a new record to sub form: just set focus from main form to subform. Then use DoCmd to add new record:
Forms![MainFormName]![SubFormName].SetFocus
DoCmd.GoToRecord , , acNewRec

Related

Cannot refresh subform from other subform

I have a form that has multiple subforms on it. When someone changes a value in a combobox of one of the subforms it needs to refresh another subform. I am using this code but it doesn't work:
Forms!frmDispatchPlanningSheet!subfrmExtraDriversForPlanning.Form.Requery
I have even tried to remove the Record Source and re-add it. However, that also did not work.
When I am saying it doesn't work for the direct requery, I don't get an error; it just doesn't requery.
For the remove and re-add the Record Source, it does remove and re-add the source; however, it still doesn't refresh the query.
however, when I click in the other form and then hit refresh all in the Ribbon, it will refresh (so I know the query works).
What am I doing wrong? how do I fix this?
If subfrmExtraDriversForPlanning refers to Driver via the table, you need to save the record first.
Add
Me.Dirty = False
before doing the .Requery.

MS Access form - lock columns from editing

Is it possible to lock specific columns from editing in an Access form ?
The problem is that I have a linked table in a form that shows information and users can update it. Now it turned out that there are some columns that need to be displayed to make the right decision, but I don't want them to edit these columns.
Probably the easiest way to do this is to create a subform for your linked table, and embed that subform in datasheet view in your main form. Taking this approach, you can then lock the desired columns in design view on the subform, but it will still look and feel like a linked table in your main form.
Use Form_BeforeUpdate event on your form.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Field <> Me.Field.OldValue Then
Cancel = True
End If
End Sub
Me.Field is the column which user cannot change here. You can apply any logic/validation in this block.
Download Sample File - It has form frmEmployee which doesn't allow to change Employee name
What you can do is go to the the properties pane of the object. Then go to the data tab. You will find a value that says enabled and locked.
Set enabled to NO
and locked to YES.
Not sure if this is what your after or if you need something a little more secure, but this is where I would start

Access VBA: Changing record source of subform

I have a form with a subform, and I want the subform record source that populates the subform to change depending on the inputs on some combo boxes in the main form.
Using VBA, I have built a function that generates the SQL statement I want to populate the subform with. I know this works because I have tested it with the msgbox and it gives me the SQL statement I want. The sql statement uses an aggregate function so the resulting table has a different structure than the table it is querying from.
The code I use to change the subform record source is:
me![subformname subform].form.recordsource=myfunction()
This has worked for me in the past, but does not work here, I simply get "#Name?" in the subform on my form.
When I open the subform separately I get "#Name?" but when I open the Recourd Source and run the query from the record source I get a value so I am confused.
Any ideas?
Try setting up two subforms - one for each format that you need - and when you swap the underlying recordsets also swap or hide/unhide the relevant subform. Then you can set the controlsource for the relevant controls to the fields that are actually available in the current recordset.
I got it to work by changing the structure of the subform I was changing so that the structure stayed the same when I altered the recordsource. The lesson is, you can change the recordsource in VBA, but you can't change the structure of the subform.
My problem occurred because I used an alias in my subform record source SELECT query. When I replaced the alias table names with the actual database table names in my VBA code, the .recordsource = strSQL01 statement worked and the data appeared in my subform.

Trigger database update after control update on subform

I have an Access form with a subform that allows updating data in table X.
The subform contains a text box control named Y, linked to column Y in table X.
Now I thought that the table would be updated immediately after that text box control loses focus, but I noticed that this only happens later, most probably after the subform loses focus.
The point is that I need the table to be updated before (or during) calling the sub Y_AfterUpdate, because I wish to perform some standard subs/functions that rely on the table to contain the new data.
What is the best way of achieving this table update? I applied subForm.Requery and that worked, but there may be another, more intuitive method available. Note that Requery to me sounds more like "populate controls of the subForm, using the current table contents", and not like "update table and then populate the subForm again". Hence, intuitively, I would have expected that I should have used another method.
You want:
DoCmd.RunCommand acCmdSaveRecord

MS-Access unbound ComboBox in DataSheet?

In ms-access 2007, i'm trying to make a form for a table. this table has foreign keys from 2 parent tables. so i thought i would make these fields a lookup. but i couldn't create a single lookup for each parent table because they are composite keys.
I decided to create a query in which for each of these parent tables and the child table with an extra field for each composite key. this works fine with a normal form using an unbound ComboBox... but the unbound ComboBox does not work in a DataSheet Subform. when i make changes to a ComboBox in the Subform code, they are applied to all the other ComboBoxes in the same column as well.
My questions:
is there a way to change the values of the individual unbound ComboBox?
is there a different control i should be using other than the ComboBox or the DataSheet Subform?
what is the normal work around for this situation?
I cannot bind the ComboBox's because the field from the query is calculated/an-expression as I said.
I ran across a form of this problem myself, so for the sake of posterity:
While generally, the advice "Don't use continuous forms/datasheets in this situtation" is the best advice...It is possible to work around this.
However, access will not let you update the value of a single control on a datasheet. What can be used instead in this case is a temporary table, which can, when used as a recordsource, become the values of those controls. You will need to repopulate the table, and requery the controls (requerying the entire form should work as well) every time the calculation needs to change, however. Furthermore, should you enable editing on the controls, you will have to write some VBA on each control to handle the update event (Before Update), and run your own query to update the source tables, not merely the temporary table. Annoying to do perhaps, but effective.
There is another possibillity, which may also work, but I haven't tried to do something quite like this myself. The rowsource of a combobox can be very complex, so it is probable that you do not need to update the comboboxes with VBA at all. The rowsource can depend on other controls (such as another combobox) using the syntax Me.Form!controlName or Forms!FormName!ControlName, which will allow you to form the composite key. Of course, you can also select from queries with a rowsource. What is more interesting is that queries can reference controls on your form, provided the form is open, and you should safely be able to modify that with VBA should you have to.
Between the two of these, you should be able to force access, kicking and screaming, to display any data you wish, even on a datasheet, and to allow the user to change that data (but only if you want it to), and using the BeforeUpdate event, drag modified data back to whatever table it came from.
Continuous forms and datasheets do not work well for editing in situations where combo boxes need to be changed conditionally. The problem is that if you use the OnCurrent event to set the Rowsource of the combo box, it will be OK for that row, but will then hide the stored values for other rows.
The solution is to never use continuous forms/datasheets for editing data when this is the case (I hardly ever use them for editing, in fact). You can create two subforms, a continuous/datasheet subform that functions as a list, and a detail subform, that displays one record. Make the list subform uneditable, and the detail subform editable. You can link the two of them by using the Link Child/Link Master property of the detail subform control, and set it to the PK of the list subform.
If your list subform is Me!List and your detail is Me!Form, and the PK field is MyID, your link properties for the detail subform would be:
Master: Me!List.Form!MyID
Child: MyID
When you move to a different record in the list form, it will be automatically loaded in the child form. Any edits to the previously displayed detail will be saved before record departure.