Deleting/Adding records on a subform that has a similar record source of the main form - forms

I am trying to make a subform that works similarly to a splitform.
I would use splitform but, as far as I could tell, I can't filter only the results of the datasheet portion of the split form, and not the results of the rest.
See here...
The subformPartDetail only returns records where the OrderID is equal to the mainform's OrderID. There is no filter in the mainform so it returns all partDetail Records regardless of the OrderID. In this particular instance, the main form has 21 records to cycle where as the subform only has four.
An issue occured when I use the subform for adding or deleting records. When I try to use the main form to cycle through the records, the added ones were skipped and the deleted ones threw an error telling me that the record I was attempting to go to has been deleted.
I corrected it like this by putting these on the subform events...
Private Sub Form_AfterInsert()
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset
rst.Requery
End Sub
Private Sub Form_Delete(Cancel As Integer)
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset
rst.Requery
End Sub
But say I try to delete the record the mainform is displaying from the subform. That code does not work and will not requery. It throws an error when I cycle away from and then back to that record.
How can I fix this?
I know I can move the record from the deleted one before it deletes by using
rst.move
I don't know where to began for testing whether the record that is being deleted is the same one that the main form is displaying.

Maybe I'm not understanding the situation correctly, but there shouldn't be any need for VBA to achieve this. If the subform is using the Link Master Fields and the Link Child Fields with your subform, those IDs would work automatically. And if you have a relationship set up between the two tables, if you delete the Order record (and have Enforce Referential Integrity: Cascade Delete) selected, it should clear out the line items automatically to avoid orphan records.

Related

Is there a way to delete main form data if data in subform is empty?

I have a form in MS Access that has a subform.
My problem is that if the user starts filling the main form with date, customer, IDOrderNumber etc. and suddenly wants to leave the form without entering data in the subform, when he tries to find that form with the same IDOrderNumber, he can't. But when he enters the same IDOrderNumber he can't because it's the primary key and can't allow duplicate values.
What should I do?
I tried to add a search field for IDOrderNumber but it doesn't work - it shows empty Master and Child form/subform. Also I have an Order list Form and I can't access a form that has no subform data entered..
I need a solution because it is a big problem for my customer/user of database.
Thanks all in advance!
The way it sounds to me, is that you want the record in the main form to be deleted if no data is entered in the subform. Ill assume the data in the subform is bound to a different table, and used for multiple records tied to IDOrderNumber
I would suggestion using the below vba code. I'm assuming some of your database's structure, so correct me as needed
This code will go in your MainForms Form_Close() Event
Private Sub Form_Close()
Dim recordexists as Integer
Dim db as Database
Dim rs as Recordset
Dim strSQL as String
strSQL = "SELECT * FROM YourSecondaryTable T2 WHERE T2.IDOrderNumber =" & Me.IDOrderNumberField.Value
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
'This recordset is checking to see if a value in the secondary table exists with the OrderNumber that exists in the main table.
With rs
If Not .BOF And Not .EOF Then
.MoveFirst
While (Not .EOF)
'If the code gets to this point, then a record exists in your subform.
'So we set the value to 1
recordexists = 1
Wend
End If
.Close
End With
'If the above recordset didnt find anything, the the recordexists value would never be set.
'Therefore you want to delete the parent record. So we delete it
If recordexists = 0 Then
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE TOP 1 FROM YourMainTable T1 WHERE T1.IDOrderNumber = " & Me.IDOrderNumberField.Value
DoCmd.SetWarnings True
End IF
Set rs = Nothing
Set db = Nothing
End Sub

Take value from field in Form to a new record in other Table

Would really like some guidance on this one. Not as easy as the title might say at first and I'm stuck beyond all and frustrated.
So:
I have a MainForm (With a current ID I would like to stay on!)
I have a Subform (search engine that searches in various linked excel files)
I have 2 tables; one for the Mainform, and one for Items
So on the Mainform, I display the Subform (search).
I do my search and find a specific item in the excel files
I have a command button, that I want to click save the found item in the search field, into my ItemsTable as a new record.
Currently I'm doing this:
Private Sub Command13_Click()
Forms!MainForm.Form.Item1.Value = Me.Searchresult.Value
End Sub
This does actually post my search result into a new field and saves it into the itemsTable.
My problem: I don't need to actually have a field to post to. I just want it to write directly to my table and store it there as a NEW record. (Right now it just overwrites the previous data. Logically because its the same field every time with the same properties).
Can someone PLEASE help me with this one?
Options:
set focus to the subform container then move to New Record row followed by your code, this is not simple, review https://access-programmers.co.uk/forums/showthread.php?t=127337
use SQL INSERT action then requery the subform
CurrentDb.Execute("INSERT INTO Items(Item1) VALUES('" & Me.Searchresult & "'")

Open form to record seleced from double click on same record in datasheet form

I've got a pair of forms that are both based on the same data table. One is a single form with a better layout of all the onfo, the other is a datasheet. I want to be able to double click on the project number in the datasheet form to open the details single form to that same record.
I've done it on a simplified practice db, but when I try it in my current db it opens the second form, but to a new record entry. This form is one that was a template that someone else downloaded and modified for our purpose, so maybe there is something stored somewhere else that is interrupting the filtering.
I deleted a couple of macros that I thought might be interfering, made sure that the form properties are cycling all records and that on form load it isn't directed to a new record. I can't figure out where else there is something preventing the form from being filtered for the selected record. Here's the code I've used in the datasheet form:
Private Sub combined_DblClick(Cancel As Integer)
DoCmd.OpenForm "frmTaskDetailsExisting", acNormal, , "SLRtaskID = " & Me.SLRtaskID
End Sub
Anyone have an idea what I'm missing here.
Have you considered doing this with a split Form? You will have the Detailed view per record and the dataseet.
If you really want two independent forms due to your needs, your code looks ok. But two things may be happening:
The value in Me.SLRtaskID is not correct. Put a breakpoint in the code and check it.
There is code in frmTaskDetailsExisting that moves it to a new record when it is opened, regardless to the filter.

Trying to design form to query and edit records Access

I'm trying to make an Access database where we can scan in a list of bar-codes, batch edit information for them, then save all the records. Later, they will need to be scanned again to batch edit more information such as shipping and order numbers.
Does anyone have suggestions on how to best design this? To start with, I've (1) made separate tables for "orders" and "parts" and (2) created a form of text boxes and a button attempting to apply this info to a part subform where records are created via the bar-code scanner. I'm a rather new Access designer. Thanks!
The questions are:
I'm not sure how to query these records via the bar code (Part_ID field) to apply order information to them later.
I can't currently update all new part records at once through my form.
Here's a little guide on how to do it. Let me know if you have any questions.
Dim db As DAO.Database
Dim Rs As DAO.Recordset
'Here is an example of a vba qarry in access. tblOrdersWhere is obviously your table.
'orderNum is a column name in your table pref a primary key
'txtOrderNum.Value is the value of a text box
'If you want just a simple select * from table you can put after db.OpenRecordset("tblOrders")
set rs = db.OpenRecordset("Select * from tblOrders Where orderNum like '*" & txtOrderNum.Value & "*';", dbOpenDynaset)
'This is how you update records in Access
'If you want to update it's Rs.Edit instead of Rs.AddNew
Rs.AddNew
Rs("OrderItem").Value = txtGroupName.Value
Rs("OrderTime").Value = txtGroupNum.Value
rs.Update

In MS Access, how to select record on form1 after adding from form2?

This is my scenario (MS Access 2007):
On Form1 I have a list of records (Orders). Form1 has a button that launches Form2.
Form2 is used for creating new Orders. The "Save" button on Form2 uses VBA to save the record, close the form and requery Form1:
DoCmd.RunCommand acCmdSaveRecord
DoCmd.RunCommand acCmdCloseWindow
Forms!Orders.Requery
The next step I'd like to be able to do is have the new record selected automatically on Form1. My guess is it's somehow done with DoCmd.FindRecord? I can't figure it out -- any help is very appreciated.
Thanks,
Stewart
The answer would depend on whether the application has a single user or multiple users. In a multiuser scenario, simply getting the most recently added Order would not necessarily return the one YOU have just entered. But if it's only you, getting the most recently added value would do that.
You can find the row with the max(orderid) if you're using an autoincrementing key; or find the row with the max(orderDateTime) if your orders are timestamped; or simply have the datasource for the Orderslist form be sorted by either of those values, descending, and reposition to the first record after a Requery.