我有一個科學資料庫,它從所有鏈接到同一個 entityID 的不同形式中進行測量。我的問題是,雖然我有一個主表的自動編號 ID,就像發票結構一樣,但子樣本表格可以而且應該弄臟主 ID。當我在任何子表單上輸入第一個測量值時,我想從任何子表單中增加主 ID,但是當我訪問其他子子表單并輸入當前的附加測量值時,我不想增加主 ID,如果主 id 是臟的,我不希望 3 個子表單中的任何一個推進自動編號 EntityID,這意味著我似乎無法在可以創建新條目的任何子表單上使用 DMax。但是一旦其中一個子子表單臟了,我就需要參考從其他子表單臟后生成的主物體ID。任何人?
|obsid auto pk|
|obsnum long pk| |number| main experiment number|***herein lies the DMax problem***
|meas1id| |number| -required
|obsnum| |number| fk -required
|meas1num| |number|
meas2id number not required
meas2num number
meas3id number not required
meas3num number
uj5u.com熱心網友回復:
您不清楚主表單(和記錄)是否會進行“某種”編輯。
如果您在現有記錄上,那么主表單(和記錄)當然已經有記錄。
如果你去一個新的主表單記錄,那么是的,在你跳轉或切換焦點到任何子表單之前,主記錄必須變得“臟”。在 99% 的情況下,首先將沒有資料或根本沒有任何資料輸入到主表單記錄中是非常非常罕見的,對吧?
因此,Access 將自動始終為您保存主記錄(如果它是臟的),因此也會為您生成自動編號 PK。
我想您可能正在跳轉到一個新的主記錄,并且在主表單上不會或不會發生任何編輯,當然,如果用戶將焦點移動到子表單,那么是的,您有麻煩了。
這是一個非常罕見的設定。
然后我會對主表做的是添加一個新列,稱之為 CreateDate。
然后您可以將此事件添加到主表單:
如果用戶要鍵入或執行任何操作,那么我們可以使用 OnInsert 事件,如下所示:
Private Sub Form_BeforeInsert(Cancel As Integer)
If IsNull(Me!CreateDate) Then
Me!CreateDate = Date
End If
End Sub
因此,這將“總是”在主表單中設定一個變臟的列。
但是,如果我們導航到一個新的空白主記錄并鍵入 NOTHING,則無法自動保存主記錄。因此,對于每個子表單,您甚至可以將其添加到子表單控制元件的 on-enter 中。
Private Sub SbubformColors_Enter()
If IsNull(Me!CreateDate) Then
Me!CreateDate = Date
End If
End Sub
另一種方式?洗掉導航按鈕,并讓您的 OWN 按鈕將表單跳轉到新記錄,然后在代碼中設定(臟)該 CreateDate 列。再一次,對子表單的任何焦點更改都會首先觸發主表單記錄的自動保存。
So, your issue can ONLY occur if navigation to a blank main new record occurs, and ZERO editing occurs. In ALL other cases, the create automatic save of the main record will occur for you without code.
If for some strange reason you do allow navigation to a new main record AND also no editing will occur, then you need to setup that "on enter" code for EACH of the sub form controls, and dirty the record with the above code (e.g. this).
Private Sub SbubformColors_Enter()
If IsNull(Me!CreateDate) Then
Me!CreateDate = Date
End If
End Sub
So, if you have 4 subforms, then you can cut paste the above code inside the above. The above will ensure that the main record exists or becomes dirty, and after the above event, the main form will be saved automatic (and thus create the autonumber PK), and then you are free to edit use the subform.
I suppose there are a number of ways to approach the above. So, maybe in place of adding a new column CreateDate, you might have some existing column that could be set a value and thus "force" the dirty of the record.
Edit:
Keep in mind that Access can and will setup maintain the auto number, and for all the tables in the sub form(s), we assume that those tables have a column that points back to the main record. In other words, each of the child forms tables is assumed to have a column in their tables. Perhaps called main_ID or some such.
To have access automatic "maintain" and insert/setup/maintain the main form/record autonumber id?
In each sub form control, you set the link master, and link child field setting. Once you do this, then no code at all is required.
In fact, as noted, if the main record exists, or in all cases the main record will be create (say by going to new record), then as long as this record becomes dirty (say by user editing, or our above code), then all sub forms (and sub tables) will automatic be maintain.
You DO NOT need to use dmax, and in fact you need ZERO code, UNLESS the issue is that the main form will never see ANY editing when you just moved to a new record.
In design mode of the main form, view the property sheet for each of teh sub forms. You see (and want to) ensure these settings are correct:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443338.html
標籤:毫秒访问
上一篇:訪問運算式生成器中的字串給出錯誤
下一篇:構建包含詳細資訊的交叉表查詢
