Imports System.Data
Imports System.Data.OleDb
Public Class Classdata
Private conn As New OleDbConnection
Private dtread As OleDbDataReader
Private sqlcom As String
Dim _name As String
Dim _age As String
Public Sub Connect()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database11.accdb"
.Open()
End With
End Sub
Public Sub add(ByVal _name As String, ByVal _age As Integer)
Connect()
'MessageBox.Show(_name)
sqlcom = "INSERT INTO TestDB (name,age) VALUES ('" & _name & "','" & _age & "')"
'MessageBox.Show(sqlcom)
Dim olecom As New OleDbCommand(sqlcom, conn)
dtread = olecom.ExecuteReader
conn.Close()
End Sub
End Class
Form1.vb Form1.vb
Option Explicit On
Option Strict On
Public Class Form1
Private condata As New Classdata
Public Sub Button1_Click() Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("กรุณาใส่ข้อมูล")
End If
Dim Result As MsgBoxResult
Result = CType(MessageBox.Show("คุณต้องการบันทึกข้อมูลใช่หรือไม่", "คำเตือน", MessageBoxButtons.YesNo), MsgBoxResult)
If Result = MsgBoxResult.Yes Then
condata.add(_name:=TextBox1.Text, _age:=CInt(TextBox2.Text))
MessageBox.Show("ข้อมูลถูกบันทึกเรียบร้อย")
'MessageBox.Show(TextBox1.Text)
Me.Close()
ElseIf Result = MsgBoxResult.No Then
MessageBox.Show("การบันทึก ถูกยกเลิก")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Tag : .NET, Ms Access, VBScript, Win (Windows App), VB.NET