|  | 
| งงกับคำถามพอสมควรครับ ก็พยายามจะหาคำตอบให้ล่ะกัน ในกรณี One To Many สำหรับตาราง Detail เราใช้ Primary Key (ProjectPK) จากตารางหลักได้เลยครับ โดยที่ไม่ต้องไปเสียเวลาหาค่า Foriegn Key ให้เสียเวลา เพราะมันเชื่อมความสัมพันธ์ให้อัตโนมัติ ... 
 คัดลอกไปที่คลิปบอร์ด    Dim Conn As New OleDb.OleDbConnection
    Function ConnectDataBase(ByVal DBFile As String) As System.Data.OleDb.OleDbConnection
        Try
            Dim strConn As String = _
                    "Provider = Microsoft.ACE.OLEDB.12.0;"
            strConn += _
                "Data Source = " & DBFile
            Conn = New OleDb.OleDbConnection(strConn)
            ' Create Connection
            Conn.ConnectionString = strConn
            ' Return
            Return Conn
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "รายงานความผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End
        End Try
    End Function
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '// สร้าง Primary Key ใหม่
        Dim PK As Long = SetupNewPK("SELECT MAX(ProjectPK) AS MaxPK FROM tblMaster")
        Dim sql As String = _
            "INSERT INTO tblMaster(ProjectPK, ProjectName) " & _
            " VALUES(" & _
            "'" & PK & "', " & _
            "'Project Name - " & PK & "'" & _
            ")"
        '// INSERT
        Call DoSQL(sql)
        '//
        For i = 1 To 10
            sql = _
                "INSERT INTO tblDetail(ProjectPK, DetailCode) " & _
                " VALUES(" & _
                "'" & PK & "', " & _
                "'" & GenID(i) & "'" & _
                ")"
            Call DoSQL(sql)
        Next
        MessageBox.Show("OK")
    End Sub
    Function GenID(ByVal iCount As Integer) As String
        '// PRO-0001 - 0010
        Return "PRO-" & Microsoft.VisualBasic.Right("0000" & iCount, 4)
    End Function
    '// UPDATE DATA
    Public Function DoSQL(ByVal Sql As String) As Boolean
        DoSQL = False
        Dim Cmd As New OleDb.OleDbCommand
        If Conn.State = ConnectionState.Closed Then Conn.Open()
        Try
            With Cmd
                .Connection = Conn
                .CommandType = CommandType.Text
                .CommandText = Sql
                .ExecuteNonQuery()
                .Dispose()
                DoSQL = True
            End With
        Catch ex As Exception
            MsgBox("Error Update: " & ex.Message)
        End Try
    End Function
    ' / Function to find and create the new Primary Key not to duplicate.
    Public Function SetupNewPK(ByVal Sql As String) As Long
        If Conn.State = ConnectionState.Closed Then Conn.Open()
        Dim Cmd As New OleDb.OleDbCommand(Sql, Conn)
        '/ Check if the information is available. And return it back
        If IsDBNull(Cmd.ExecuteScalar) Then
            '// Start at 1
            SetupNewPK = 1
        Else
            SetupNewPK = Cmd.ExecuteScalar + 1
        End If
    End Function
    Private Sub frmSampleOne2Many_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Conn = ConnectDataBase("I:\Project VBNET 2020\VB2010\SampleOneToMany.accdb")
    End Sub
End Class
 
 | 
 
  |