我正在創建一組 Access 資料庫表單,用于將植被資料輸入到鏈接的 SQL Server 資料庫中。對于一個協議,我創建了一個表單“frmLPI”,用于輸入來自名為 Line-Point-Intercept 的植被監測方法的資料。這是一個表格,其中包含一個名為“frmLPIDetail”的子表格,其中記錄了植物物種的個體計數。主表單具有三個未系結的控制元件:[TransectOID]、[DataRec] 和 [DataObs]。TransectOID 是我們每次運行協議時的唯一 ID。對于每個 TransectOID,有 30 個我們對植被進行采樣的位置,這些位置在名為 LPI_OID 的子表單中具有隱藏的唯一 ID。子表單通過 TransectOID 鏈接到主表單。我希望我的用戶能夠單擊主表單中未系結的 [DataRec] 和 [DataObs] 組合框,并在子表單中為所有 30 條記錄自動填充相應的欄位。我已經弄清楚如何為子表單中的記錄完成此操作,但無法弄清楚如何為主表單中 TransectOID 的每個值的 30 條記錄執行此操作。下面是我的表單的螢屏截圖,以幫助您直觀地了解我所追求的內容:

這是我想出的代碼來自動填充一條記錄
Private Sub Form_Load()
Me.TransectOID = Me.OpenArgs
End Sub
Private Sub Form_Deactivate()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Private Sub DataObs_AfterUpdate()
Me!frmLPIDetail.Form!Data_observer = Me!DataObs
Me.Dirty = False
End Sub
Private Sub DataRec_AfterUpdate()
Me!frmLPIDetail.Form!Data_recorder = Me!DataRec
Me.Dirty = False
End Sub
我們歡迎所有的建議
uj5u.com熱心網友回復:
由于一次插入多條記錄是可取的,您之前曾問過您的問題,但我找不到特別有用的答案,因此我將提供比您要求的更通用的答案。Access 不提供用于插入多條記錄的默認表單。您必須自己撰寫代碼,但程序始終幾乎相同。
- 找出資料的規范化表結構
- 確定您需要從用戶那里收集哪些資料以進行多次插入
- 在表單中添加一個按鈕,并在點擊事件中放置多個插入的 vba
所以這里有 1 個可能適合您的資料的規范化表結構:

因為我不知道 TransectionOID 來自哪里,所以我們讓 Access 提供 TransectionID 作為主鍵,并假設 TransectionOID 是在另一個表單上輸入的。所有其他感興趣的資訊都在 TranssectionDetails 表中,無需撰寫查詢來收集我們需要的所有變數到我們的表單記錄源中以完成第 2 步。為了快速啟動,我選擇了 TransactionDetails 表并使用了創建表單向導以制作表格樣式的表單。

為了完成第 2 步,我們將控制元件放在標題中以從我們需要的用戶那里收集資訊,并開始編輯表單以提高用戶友好度。例如,我洗掉了詳細資訊部分中 TranssectionDetailID 的復選框,并用組合框替換了所有其他控制元件。我通常也用組合框替換帶圓圈的記錄選擇器,但在這里可能會令人困惑,所以我讓記錄選擇器提供一些搜索功能。最終形式如下:

最后,對于第 3 步,我們為單擊事件添加 vba
Private Sub cmdInsert_Click()
Dim db As Database
Dim rs As Recordset 'using recordset because lower error rate than using sql strings
Set db = CurrentDb
Set rs = db.OpenRecordset("TransectionDetails")
Dim L As Integer
Dim S As Integer
If Not Me.lstLocations.ListCount = 0 Then 'if no locations are selected no records can be inserted
For L = 0 To Me.lstLocations.ListCount 'simple multiselect listbox version matters for the vba code
If Me.lstLocations.Selected(L) = True Then
For S = 0 To Me.lstSpecies.ListCount
If Me.lstSpecies.Selected(S) = True Then
rs.AddNew
rs!TransectionID = Me.cmbTransectionID
rs!Data_observer = Me.cmbData_observer
rs!Data_recorder = Me.cmbData_recorder
rs!TransectLocation = Me.lstLocations.Column(0, L) 'column gives you access to values in the listbox
rs!SpeciesID = Me.lstSpecies.Column(0, S)
If Not IsNull(Me.chkDead) Then 'chkDead is a triple value checkbox, this both avoids setting Dead to null and shows how to handle when the user doesn't set all controls
rs!Dead = Me.chkDead
End If 'chkdead
rs.Update
End If 'lstspecies selected
Next S
End If
Next L
End If
Me.Detail.Visible = True 'quick and dirty bit of style (detail starts invisible)
Me.Filter = "TransectionID = " & Me.cmbTransectionID 'more quick and dirty style filter to focus on inserted records
Me.FilterOn = True
'clean up
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Private Sub cmdSelectAllLocations_Click()
Dim i As Integer
For i = 0 To Me.lstLocations.ListCount
Me.lstLocations.Selected(i) = True
Next
End Sub
Private Sub cmdSelectAllSpecies_Click()
Dim i As Integer
For i = 0 To Me.lstSpecies.ListCount
Me.lstSpecies.Selected(i) = True
Next
End Sub
Private Sub cmdSelectNoLocations_Click()
Dim i As Integer
For i = 0 To Me.lstLocations.ListCount
Me.lstLocations.Selected(i) = False
Next
End Sub
Private Sub cmdSelectNoSpecies_Click()
Dim i As Integer
For i = 0 To Me.lstSpecies.ListCount
Me.lstSpecies.Selected(i) = False
Next
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371212.html
