ประกาศตัวแปร
dim conn as new OleDbConnection("Provider = Microsoft.jet.OleDb.4.0;data source ="& server.mapPath("ตำแหน่งของ database"))
dim comm as new OleDbCommand(insert into ชื่อTable (ชื่อfield1,ชื่อfield2) Values (ชื่อTextbox1.text,ชื่อTextbox2.text),conn)
แล้วก็ใช้คำสั่ง
conn.open
comm.ExecuteNonQuery
conn.close
Public Class addst
Const database_file As String = "C:\database.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & _
"Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Code (VB.NET)
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If txtnum.Text <> "" And txtname.Text <> "" And txtaddress.Text <> "" And txtday.Text <> "" And txtid.Text <> "" And txtphone.Text <> "" And txtschool.Text <> "" Then
Dim cd As String = "SELECT * FROM studen"
Dim upd As String = "INSERT INTO student (Idcard,Lname,Idtest,Address,School,birthday,phone)" & _
" VALUES ( '" & txtid.Text & "'," & _
"'" & txtname.Text & "'," & _
"'" & txtnum.Text & "'," & _
"'" & txtaddress.Text & "'," & _
"'" & txtschool.Text & "'," & _
"'" & txtday.Text & "'," & _
"'" & txtphone.Text & "')"
Dim cm As New OleDbCommand(upd, conn)
conn.Open()
cm.ExecuteNonQuery()
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้วคะ", " my", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.Close()
Else
MsgBox("กรุณากรอกข้อมูลให้ครบค่ะ", MsgBoxStyle.Information, "Information")
End If
End Sub
Imports System.Data
Imports System.Data.OleDb
Public Class addst
Const database_file As String = "C:\ร้านขายยางรถยนต์.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & _
"Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
End Class
Public Class Form3
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Form4.Show()
Form4.Focus()
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Form5.Show()
Form5.Focus()
End Sub
Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click
Form6.Show()
Form6.Focus()
End Sub
Private Sub ToolStripButton11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton11.Click
Form7.Show()
Form7.Focus()
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Form8.Show()
Form8.Focus()
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
Form9.Show()
Form9.Focus()
End Sub
Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
Form10.Show()
Form10.Focus()
End Sub
Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
Form11.Show()
Form11.Focus()
End Sub
Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
Form12.Show()
Form12.Focus()
End Sub
Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
If MsgBox("คุณต้องการออกจากโปรแกรมนี้ ใช่หรือไม่?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "คำถาม") = MsgBoxResult.Yes Then
End
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("กรุณากรอกข้อมูล วัน/เดือน/ปี ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox1.Focus()
ElseIf TextBox3.Text = "" Then
MsgBox("กรุณากรอก ชื่อ ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox3.Focus()
ElseIf TextBox4.Text = "" Then
MsgBox("กรุณากรอก นามสกุล ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox4.Focus()
ElseIf ComboBox1.Text = "" Then
MsgBox("กรุณากรอก เพศ ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
ComboBox1.Focus()
ElseIf TextBox5.Text = "" Then
MsgBox("กรุณากรอก รหัสประจำตัวประชาชน ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox5.Focus()
ElseIf TextBox6.Text = "" Then
MsgBox("กรุณากรอก วันเกิด ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox6.Focus()
ElseIf TextBox7.Text = "" Then
MsgBox("กรุณากรอก บ้านเลขที่ ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox7.Focus()
ElseIf TextBox8.Text = "" Then
MsgBox("กรุณากรอก แขวง/ตำบล ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox8.Focus()
ElseIf TextBox9.Text = "" Then
MsgBox("กรุณากรอก เขต/อำเภอ ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox9.Focus()
ElseIf TextBox10.Text = "" Then
MsgBox("กรุณากรอก จังหวัด ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox10.Focus()
ElseIf TextBox11.Text = "" Then
MsgBox("กรุณากรอก รหัสไปรษณีย์", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox11.Focus()
ElseIf TextBox12.Text = "" Then
MsgBox("กรุณากรอก เบอร์โทรศัพท์", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox12.Focus()
ElseIf TextBox13.Text = "" Then
MsgBox("กรุณากรอกวันหมดอายุ", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "ข้อมูลไม่ครบ !")
TextBox13.Focus()
ElseIf MsgBox("คุณต้องการบันทึกใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, " SAVE! ") = MsgBoxResult.Yes Then
ElseIf TextBox1.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" And ComboBox1.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" And TextBox7.Text <> "" And TextBox8.Text <> "" And TextBox9.Text <> "" And TextBox10.Text <> "" And TextBox11.Text <> "" And TextBox12.Text <> "" And TextBox13.Text <> "" Then
Dim cd As String = "SELECT * FROM MEMBER_S"
Dim upd As String = "INSERT INTO MEMBERS (Id_Member,Date,Sex,NameF_member,NameL_meber,Code_People,Birthday,Address,Didtrict,Sub_Didtrict,Province,Code_post,Phone,Overday)"" VALUES ( '" & TextBox1.Text & "',""'" & TextBox3.Text & "',""'" & TextBox4.Text & "',""'" & ComboBox1.Text & "',""'" & TextBox5.Text & "',""'" & TextBox6.Text & "',""'" & TextBox7.Text & "',""'" & TextBox8.Text & "',""'" & TextBox9.Text & "',""'" & TextBox10.Text & "',""'" & TextBox11.Text & "',""'" & TextBox12.Text & "',""'" & TextBox13.Text & "')"
Dim cm As New OleDbCommand(upd, conn)
conn.Open()
cm.ExecuteNonQuery()
conn.Close()
End If
End Sub
Private Sub ขอมลลกคาปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลลกคาปลกToolStripMenuItem.Click
Form13.Show()
Form13.Focus()
Hide()
End Sub
Private Sub ขอมลลกคาสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลลกคาสงToolStripMenuItem.Click
Form14.Show()
Form14.Focus()
Hide()
End Sub
Private Sub ขอมลสนคาToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลสนคาToolStripMenuItem.Click
Form15.Show()
Form15.Focus()
Hide()
End Sub
Private Sub ขอมลขายปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลขายปลกToolStripMenuItem.Click
Form16.Show()
Form16.Focus()
Hide()
End Sub
Private Sub ขอมลขายสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลขายสงToolStripMenuItem.Click
Form17.Show()
Form17.Focus()
Hide()
End Sub
Private Sub ขอมลการเครมปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลการเครมปลกToolStripMenuItem.Click
Form18.Show()
Form18.Focus()
Hide()
End Sub
Private Sub ขอมลการเครมสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลการเครมสงToolStripMenuItem.Click
Form19.Show()
Form19.Focus()
Hide()
End Sub
Private Sub ProgrammerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgrammerToolStripMenuItem.Click
Form20.Show()
Form20.Focus()
Hide()
End Sub
End Class
Const database_file As String = "C:\ร้านขายยางรถยนต์.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & _
"Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
เอาโค๊ดข้างบนไปวา'ใน Public Class Form3
ของนาย นะ งืมๆ
Imports System.Data
Imports System.Data.OleDb
Public Class Form3
Const database_file As String = "C:\TireShop.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & _
"Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Form4.Show()
Form4.Focus()
Hide()
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Form5.Show()
Form5.Focus()
Hide()
End Sub
Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click
Form6.Show()
Form6.Focus()
Hide()
End Sub
Private Sub ToolStripButton11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton11.Click
Form7.Show()
Form7.Focus()
Hide()
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Form8.Show()
Form8.Focus()
Hide()
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
Form9.Show()
Form9.Focus()
Hide()
End Sub
Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
Form10.Show()
Form10.Focus()
Hide()
End Sub
Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
Form11.Show()
Form11.Focus()
Hide()
End Sub
Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
Form12.Show()
Form12.Focus()
Hide()
End Sub
Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
If MsgBox("คุณต้องการออกจากโปรแกรมนี้ ใช่หรือไม่?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "คำถาม") = MsgBoxResult.Yes Then
End
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text <> "" And TextBox2.Text <> "" And ComboBox1.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" And TextBox5.Text <> "" And TextBox6.Text <> "" Then
Dim cd As String = "SELECT * FROM MEMBER1"
Dim upd As String = "INSERT INTO MEMBER1 (Date1,Id_Member1,Sex,NameF_member,NameL_meber,Code_People,Birthday)" & _
" VALUES ( '" & TextBox1.Text & "'," & _
"'" & TextBox2.Text & "'," & _
"'" & ComboBox1.Text & "'," & _
"'" & TextBox3.Text & "'," & _
"'" & TextBox4.Text & "'," & _
"'" & TextBox5.Text & "'," & _
"'" & TextBox6.Text & "')"
Dim cm As New OleDbCommand(upd, conn)
conn.Open()
cm.ExecuteNonQuery() 'เเถวนี้เป็นแทบสีเหลืองครับ'
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", " my", MessageBoxButtons.OK, MessageBoxIcon.Information)
conn.Close()
Else
MsgBox("กรุณากรอกข้อมูลให้ครบ", MsgBoxStyle.Information, "Information")
End If
End Sub
Private Sub ขอมลลกคาปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลลกคาปลกToolStripMenuItem.Click
Form13.Show()
Form13.Focus()
Hide()
End Sub
Private Sub ขอมลลกคาสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลลกคาสงToolStripMenuItem.Click
Form14.Show()
Form14.Focus()
Hide()
End Sub
Private Sub ขอมลสนคาToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลสนคาToolStripMenuItem.Click
Form15.Show()
Form15.Focus()
Hide()
End Sub
Private Sub ขอมลขายปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลขายปลกToolStripMenuItem.Click
Form16.Show()
Form16.Focus()
Hide()
End Sub
Private Sub ขอมลขายสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลขายสงToolStripMenuItem.Click
Form17.Show()
Form17.Focus()
Hide()
End Sub
Private Sub ขอมลการเครมปลกToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลการเครมปลกToolStripMenuItem.Click
Form18.Show()
Form18.Focus()
Hide()
End Sub
Private Sub ขอมลการเครมสงToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ขอมลการเครมสงToolStripMenuItem.Click
Form19.Show()
Form19.Focus()
Hide()
End Sub
Private Sub ProgrammerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgrammerToolStripMenuItem.Click
Form20.Show()
Form20.Focus()
Hide()
End Sub
End Class
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Const database_file As String = "C:\database.mdb"
Dim strConnection As String = "provider=microsoft.Jet.Oledb.4.0;" & _
"Data Source=" & database_file
Dim conn As New OleDbConnection(strConnection)
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim WithEvents objCurrencymanager As CurrencyManager
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strsql As String = "SELECT Idtest FROM STUDENT"
da.SelectCommand = New OleDbCommand(strsql, conn)
Try
da.Fill(ds, "stu")
Catch ex As OleDbException
MsgBox("เกิดข้อผิดพลาด ", MsgBoxStyle.Critical, "Critical")
Exit Sub
End Try
TextBox1.DataBindings.Add("Text", ds.Tables("stu"), "FirstName")
TextBox2.DataBindings.Add("Text", ds.Tables("stu"), "LastName")
DateTimePicker1.DataBindings.Add("Value", ds.Tables("stu"), "Birthday")
DataGridView1.DataSource = ds.Tables("stu") 'ผูกตาราง กับ datadagrid
objCurrencymanager = Me.BindingContext(ds.Tables("stu"))
objCurrencymanager_PositionChanged(Nothing, Nothing)
End Sub
Private Sub objCurrencymanager_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles objCurrencymanager.PositionChanged
Label1.Text = "เรคอร์ดที่" & objCurrencymanager.Position + 1 & _
"จากทั้งหมด" & objCurrencymanager.Count & "เรคคอร์ด"
End Sub
Private Sub btadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btadd.Click
objCurrencymanager.AddNew()
TextBox1.Focus()
End Sub
Private Sub btdele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btdele.Click
If MsgBox("แน่ใจหรือว่าต้องการลบเรคอร์ดนี้?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
objCurrencymanager.RemoveAt(objCurrencymanager.Position)
End If
objCurrencymanager_PositionChanged(Nothing, Nothing)
End Sub
Private Sub btsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsave.Click
Try
Dim cb As New OleDbCommandBuilder(da)
objCurrencymanager.EndCurrentEdit()
Dim irecord As Integer = da.Update(ds, "stu")
MsgBox("บันทึกการเปลี่ยนแปลงเรียบร้อยแล้ว" & ControlChars.CrLf & _
"มีการเพิ่มข้อมูล" & irecord & "เรคอร์ด")
Catch ex As OleDbException
MsgBox(ex.Message)
End Try
End Sub
End Class