我想顯示窗體上兩個表中的字段。這兩個表格是「客戶」和「車輛」。 (1:M)。客戶字段將顯示在主窗體中,並顯示我已經在數據表視圖中放置子窗體的車輛字段,並且我爲車輛ID添加了一個文本框。當我輸入CustomerID並在另一個窗體上單擊一個命令按鈕時,將使用子窗體(sfrmVehicle)打開此主窗體(frmCustomerDetails)。 客戶字段正確顯示在主窗體中。沒有錯誤消息,但我不能在子窗體中看到車輛字段。它只顯示#Name?六次。車輛表中有六條記錄,所以我認爲它甚至不會過濾該客戶的車輛。 (車輛表中只有兩條記錄屬於此客戶。) 我的Ms Access 2013 VBA代碼是;Access 2013中1:m表的主窗體和子窗體
Dim strSQL1 As String
Dim rs1 As DAO.Recordset
strSQL1 = "SELECT Customer.CustomerID, " & _
" Customer.fName, " & _
" Customer.lName, " & _
" Customer.Telephone, " & _
" Customer.MobilePhone, " & _
" Customer.EMail, " & _
" Customer.Address1, " & _
" Customer.Address2, " & _
" Customer.City, " & _
" Customer.State, " & _
" Customer.PostalCode, " & _
" Vehicle.VehicleID " & _
"FROM Customer INNER JOIN Vehicle ON Customer.CustomerID = Vehicle.CustomerID;"
Set rs1 = CurrentDb.OpenRecordset(Name:=strSQL1, Type:=dbOpenDynaset)
rs1.FindFirst "[CustomerID] =""" & txtIDs & """"
DoCmd.OpenForm "frmCustomerDetails"
[Forms]![frmCustomerDetails]![txtCustomerName] = rs1.Fields!fName
..
..
[Forms]![frmCustomerDetails]![sfrmVehicle]![txtVehicleId].ControlSource = rs1.Fields!VehicleID
rs1.Close
Set rs1 = Nothing
我是否必須爲主窗體和子窗體使用兩個不同的SQL語句?那麼如何做過濾。需要幫忙。請。
對於主窗體+子窗體,您根本不需要任何代碼。請閱讀:https://support.office.com/zh-CN/article/Create-a-form-that-contains-a-subform-a-one-to-many-form-ddf3822f-8aba-49cb-831a -1e74d6f5f06b – Andre