cyberdaemon
New member
- Joined
- Nov 18, 2010
- Messages
- 2
- Programming Experience
- Beginner
good day,
i have 2 table in access2007. my first table is sample and the other table is tbl_educ
sample table tbl_educ
pk emp_id emp_id
lname school_name
fname school_address
mname level
address
i have a 1 to many relation so may sample.emp_id is related to the tbl_educ.emp_id and other fields on the tbl_educ
here is my problem i cannot insert a record for both table using vb.net
here is my code
Imports System.Data
Imports System.Data.OleDb
Public Class education
Private con As New OleDb.OleDbConnection()
Private dtAdapter As OleDb.OleDbDataAdapter
Private cmdBuilder As OleDb.OleDbCommandBuilder
Private dtTable As New DataTable
Private rowpos As Integer = 0
Private Sub education_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\jesspr\Desktop\mydbase\sample.accdb"
'con.Open()
'dtAdapter = New OleDb.OleDbDataAdapter("SELECT sample.emp_ID, tbl_educ.School_Name, tbl_educ.School_Address FROM sample RIGHT JOIN tbl_educ ON sample.emp_ID = tbl_educ.emp_id;", con)
'cmdBuilder = New OleDb.OleDbCommandBuilder(dtAdapter)
'dtAdapter.Fill(dtTable)
End Sub
Private Sub srecord()
If dtTable.Rows.Count = 0 Then
txtEmp.Text = ""
txtSName.Text = ""
txtSAddress.Text = ""
txtLevel.Text = ""
'txtCert.Text = ""
'txtLicense.Text = ""
Exit Sub
End If
txtEmp.Text = dtTable.Rows(rowpos)("emp_ID").ToString()
txtSName.Text = dtTable.Rows(rowpos)("School_Name").ToString()
txtSAddress.Text = dtTable.Rows(rowpos)("School_Address").ToString
txtLevel.Text = dtTable.Rows(rowpos)("Level").ToString
'txtCert.Text = dtTable.Rows(rowpos)("Certificate").ToString
'txtLicense.Text = dtTable.Rows(rowpos)("LIcence").ToString
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim ds As New DataTable
Dim DAdapter As OleDb.OleDbDataAdapter
Dim dr As DataRow = ds.NewRow()
Dim cmd As New OleDbCommand
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\jesspr\Desktop\mydbase\sample.accdb"
con.Open()
'dtAdapter = New OleDb.OleDbDataAdapter("Insert into sample (emp_id)values('" & txtEmp.Text & "' )", con)
'DAdapter = New OleDb.OleDbDataAdapter("Insert into tbl_educ (school_name,school_address,level)values('" & txtSName.Text & "'," & txtSAddress.Text & "," & txtLevel.Text & " )", con)
dtAdapter = New OleDb.OleDbDataAdapter("Insert into sample (emp_id)values(@emp_id )", con)
DAdapter = New OleDb.OleDbDataAdapter("Insert into tbl_educ (school_name,school_address,level)values(@school_name,@school_address,@level)", con)
cmdBuilder = New OleDb.OleDbCommandBuilder(dtAdapter)
cmdBuilder = New OleDb.OleDbCommandBuilder(DAdapter)
'cmd = New OleDb.OleDbCommand(DAdapter)
dr("emp_id") = txtEmp.Text
dr("school_name") = txtSName.Text
dr("school_address") = txtSAddress.Text
dr("level") = txtLevel.Text
dtTable.Rows.Add(dr)
ds.Rows.Add(dr)
rowpos = dtTable.Rows.Count - 1
rowpos = ds.Rows.Count - 1
Me.srecord()
End Sub
End Class
can someone help me with this problem. how could i insert a record for both table? once i run the application it return an error emp_id does not belong to a table..
any help will be appreciated..
i have 2 table in access2007. my first table is sample and the other table is tbl_educ
sample table tbl_educ
pk emp_id emp_id
lname school_name
fname school_address
mname level
address
i have a 1 to many relation so may sample.emp_id is related to the tbl_educ.emp_id and other fields on the tbl_educ
here is my problem i cannot insert a record for both table using vb.net
here is my code
Imports System.Data
Imports System.Data.OleDb
Public Class education
Private con As New OleDb.OleDbConnection()
Private dtAdapter As OleDb.OleDbDataAdapter
Private cmdBuilder As OleDb.OleDbCommandBuilder
Private dtTable As New DataTable
Private rowpos As Integer = 0
Private Sub education_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\jesspr\Desktop\mydbase\sample.accdb"
'con.Open()
'dtAdapter = New OleDb.OleDbDataAdapter("SELECT sample.emp_ID, tbl_educ.School_Name, tbl_educ.School_Address FROM sample RIGHT JOIN tbl_educ ON sample.emp_ID = tbl_educ.emp_id;", con)
'cmdBuilder = New OleDb.OleDbCommandBuilder(dtAdapter)
'dtAdapter.Fill(dtTable)
End Sub
Private Sub srecord()
If dtTable.Rows.Count = 0 Then
txtEmp.Text = ""
txtSName.Text = ""
txtSAddress.Text = ""
txtLevel.Text = ""
'txtCert.Text = ""
'txtLicense.Text = ""
Exit Sub
End If
txtEmp.Text = dtTable.Rows(rowpos)("emp_ID").ToString()
txtSName.Text = dtTable.Rows(rowpos)("School_Name").ToString()
txtSAddress.Text = dtTable.Rows(rowpos)("School_Address").ToString
txtLevel.Text = dtTable.Rows(rowpos)("Level").ToString
'txtCert.Text = dtTable.Rows(rowpos)("Certificate").ToString
'txtLicense.Text = dtTable.Rows(rowpos)("LIcence").ToString
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim ds As New DataTable
Dim DAdapter As OleDb.OleDbDataAdapter
Dim dr As DataRow = ds.NewRow()
Dim cmd As New OleDbCommand
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\jesspr\Desktop\mydbase\sample.accdb"
con.Open()
'dtAdapter = New OleDb.OleDbDataAdapter("Insert into sample (emp_id)values('" & txtEmp.Text & "' )", con)
'DAdapter = New OleDb.OleDbDataAdapter("Insert into tbl_educ (school_name,school_address,level)values('" & txtSName.Text & "'," & txtSAddress.Text & "," & txtLevel.Text & " )", con)
dtAdapter = New OleDb.OleDbDataAdapter("Insert into sample (emp_id)values(@emp_id )", con)
DAdapter = New OleDb.OleDbDataAdapter("Insert into tbl_educ (school_name,school_address,level)values(@school_name,@school_address,@level)", con)
cmdBuilder = New OleDb.OleDbCommandBuilder(dtAdapter)
cmdBuilder = New OleDb.OleDbCommandBuilder(DAdapter)
'cmd = New OleDb.OleDbCommand(DAdapter)
dr("emp_id") = txtEmp.Text
dr("school_name") = txtSName.Text
dr("school_address") = txtSAddress.Text
dr("level") = txtLevel.Text
dtTable.Rows.Add(dr)
ds.Rows.Add(dr)
rowpos = dtTable.Rows.Count - 1
rowpos = ds.Rows.Count - 1
Me.srecord()
End Sub
End Class
can someone help me with this problem. how could i insert a record for both table? once i run the application it return an error emp_id does not belong to a table..
any help will be appreciated..