@LocTrang:实际上,一位客户可能拥有多个地址(账单、送货、替代送货)。关系看起来像
- [Customer]-1:1----0:m[Address] > 如果没有相应的客户详细信息,客户地址就不能存在。
供应商也是如此。供应商可能有多个地址(帐单、仓库)
供应商和地址表之间的关系看起来就像客户与地址之间的关系。
通常客户和供应商都有自己的地址表,但如果您想将两个地址保存在一个表中,您可以像这样设计地址表
- Address_ID > Primary1
- Owner_ID > Primary2 (*)
- Address_type >(可以是address_type表的主键)
- 地址_line1
- 地址_line2
- 城市
- ...其余列
这种设计将防止数据库中的数据不一致。 (*) 但是: 要实现这一点,owner_id 在整个数据库中或至少在客户和供应商表之间必须是唯一的。由于客户和供应商表中的自动编号字段将从 1 开始,因此您不能将其用作 owner_id,因为它在这两个表之间不是唯一的。最好在表中使用“GUID/uuid”作为 unique_id或构建自定义唯一字段,如供应商 ID 以“S_”开头,客户 ID 以 C_ 开头
您可以选择创建另一个名为 Address_type 的表:
- Address_type_id > 主键,自动编号
- 地址类型
- 说明
并将其粘贴到地址表中。
现在回答您的问题:
如果您使用 Access 表单来输入您的客户详细信息,您可以为地址创建一个子表单,并通过 LinkMasterField 和 LinkChildField 链接主表单和子表单。这样,您首先创建客户记录,然后当您移动到地址子表单时,新创建的 customer_Id 会为您预先填充。
喜欢她:(C_ID 是唯一的客户/所有者 ID)
使用插入后事件自动更新您的客户唯一 ID
Private Sub Form_AfterInsert()
Me.txt_c_id.value = "C_" & Me.txt_Customer_id.value
End Sub
如果您使用 VBA 输入客户详细信息,请尝试通过事务封装您的数据执行。这样,无论有多少其他用户正在创建记录,您始终是安全的:
vba 代码:
Private Sub btn_add_new_Click()
Dim MyDB As DAO.Database
Dim MyRs As DAO.Recordset
Set MyDB = CurrentDb
Dim Last_ID As Long
Dim SQL_GET As String
Dim SQL_SET As String
DBEngine.BeginTrans
On Error GoTo ERROR_TRANS:
SQL_SET = "INSERT INTO TBL_Customer(C_name,C_contact) VALUES('Second Customer','Second Contact')"
MyDB.Execute SQL_SET, dbFailOnError
SQL_GET = "SELECT MAX(Customer_id) AS LAST_ID FROM TBl_Customer"
Set MyRs = MyDB.OpenRecordset(SQL_GET)
Last_ID = Nz(MyRs("LAST_ID"), 0)
'Since access does not provide triggers we update the customer unique id manually
If Not Last_ID = 0 Then
SQL_SET = "UPDATE TBL_Customer SET C_ID = 'C_" & Last_ID & "' WHERE TBL_Customer.Customer_id = " & Last_ID
MyDB.Execute SQL_SET, dbFailOnError
End If
'Now add the address record via vba
SQL_SET = "INSERT INTO TBL_Address(Owner_ID, type, Address_line1, Address_line2, city) VALUES('C_" & Last_ID & "','Billing','01 Main Street','Flat 2','London');"
MyDB.Execute SQL_SET, dbFailOnError
DBEngine.CommitTrans
MsgBox "Customer inserted. New customer ID = C_" & Last_ID, vbInformation, "Success"
EXIT_ROUTINE:
On Error Resume Next
Set MyDB = Nothing
Set MyRs = Nothing
Exit Sub
ERROR_TRANS:
On Error Resume Next
DBEngine.Rollback
MsgBox "Sorry there was a problem while creating new customer record", vbExclamation, "Unable to insert"
Err.Clear
GoTo EXIT_ROUTINE
End Sub
我希望你已经理解并更好地利用这个答案。