0% found this document useful (0 votes)
49 views

Latihan Pemrograman Visual Database

This document provides code examples for connecting a Visual Basic application to an Access database and performing basic CRUD (create, read, update, delete) operations. The code shows how to: 1) Establish a connection to an Access database using ODBC and display connection details 2) Create two forms - one for data entry and another for viewing data 3) Write code to insert, update, delete and retrieve data from database tables

Uploaded by

Muhamad Ramli
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Latihan Pemrograman Visual Database

This document provides code examples for connecting a Visual Basic application to an Access database and performing basic CRUD (create, read, update, delete) operations. The code shows how to: 1) Establish a connection to an Access database using ODBC and display connection details 2) Create two forms - one for data entry and another for viewing data 3) Write code to insert, update, delete and retrieve data from database tables

Uploaded by

Muhamad Ramli
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LATIHAN PEMROGRAMAN VISUAL DATABASE KONEKSI KE DATABASE Buat Form sebagai berikut :

Kode :
Imports System.Data.Odbc Public Class Form1 Private Sub BtnConnStr_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConnStr.Click Dim Conn As OdbcConnection Dim ConnStr As String = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & My.Application.Info.DirectoryPath & "\Mhs.mdb" Try Conn = New OdbcConnection(ConnStr) Conn.Open() MsgBox("Koneksi ke : " & Conn.DataSource & vbCrLf & _ "Database : " & Conn.Database & vbCrLf & _ "Driver : " & Conn.Driver.ToString & vbCrLf & _ "Connection String : " & Conn.ConnectionString, MsgBoxStyle.Information,

"Connected")

Conn.Close() Conn = Nothing Catch ex As Exception MsgBox("ODBC Error : " & ex.Message) End Try End Sub

Private Sub BtnConnStrUsing_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConnStrUsing.Click Dim ConnStr As String = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & My.Application.Info.DirectoryPath & "\Mhs.mdb" Try Using Conn = New OdbcConnection(ConnStr) Conn.Open() MsgBox("Koneksi ke : " & Conn.DataSource & vbCrLf & _ "Database : " & Conn.Database & vbCrLf & _ "Driver : " & Conn.Driver.ToString & vbCrLf & _

"Connection String : " & Conn.ConnectionString, MsgBoxStyle.Information, "Connected dengan Using") End Using Catch ex As Exception MsgBox("ODBC Error : " & ex.Message) End Try End Sub End Class

LATIHAN 1 database dengan menggunakan koneksi odbc Buat form1 sebagai berikut :

Buat Form2 sebagai berikut :

Buat database access sebagai berikut :

Contoh Koding Form 1 :


Imports System.Data.Odbc Public Class Form1 Dim ConnStr As String = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & My.Application.Info.DirectoryPath & "\Mhs.mdb" Dim Conn As OdbcConnection Dim SQL As String Dim RecCount As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Conn = New OdbcConnection(ConnStr) Conn.Open() Catch MessageBox.Show("Database Tidak Dapat Diproses ... !!!", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End End Try TxtNIM.MaxLength = 9 TxtNama.MaxLength = 50 BtnBaru_Click(Nothing, Nothing) End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Conn.Dispose() Conn = Nothing End Sub Private Sub BtnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnKeluar.Click Me.Close() End Sub Private Sub BtnBaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBaru.Click TxtNIM.Clear() TxtNama.Clear() TxtNama.Enabled = False TxtTeori.Clear() TxtTeori.Enabled = False

TxtPraktek.Clear() TxtPraktek.Enabled = False BtnSimpan.Enabled = False BtnUpdate.Enabled = False BtnHapus.Enabled = False TxtNIM.Focus() End Sub Private Sub BtnDaftar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDaftar.Click Form2.ShowDialog() TxtNIM.Focus() End Sub Private Sub TxtNIM_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtNIM.KeyPress If Not (Asc(e.KeyChar) >= Keys.D0 And Asc(e.KeyChar) <= Keys.D9 Or Asc(e.KeyChar) = Keys.Back) Then e.KeyChar = Chr(Keys.None) End If End Sub Private Sub Nilai_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtTeori.KeyPress, TxtPraktek.KeyPress If Not (Asc(e.KeyChar) >= Keys.D0 And Asc(e.KeyChar) <= Keys.D9 Or Asc(e.KeyChar) = Keys.Back Or e.KeyChar = ",") Then e.KeyChar = Chr(Keys.None) End If End Sub Private Sub TxtNIM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNIM.TextChanged SQL = "Select * From Nilai Where NIM = '" & TxtNIM.Text & "'" Using Cmd As New OdbcCommand(SQL, Conn) Using Reader = Cmd.ExecuteReader() RecCount = 0 While Reader.Read() RecCount += 1 End While End Using Using Reader = Cmd.ExecuteReader() If RecCount >= 1 Then TxtNama.Enabled = True TxtTeori.Enabled = True TxtPraktek.Enabled = True BtnSimpan.Enabled = False BtnUpdate.Enabled = True BtnHapus.Enabled = True While Reader.Read() TxtNama.Text = Reader.GetString(1) TxtTeori.Text = Reader.GetString(2) TxtPraktek.Text = Reader.GetString(3) End While ElseIf TxtNIM.Text.Length > 0 Then TxtNama.Clear() TxtNama.Enabled = True TxtTeori.Clear() TxtTeori.Enabled = True

TxtPraktek.Clear() TxtPraktek.Enabled = True BtnSimpan.Enabled = True BtnUpdate.Enabled = False BtnHapus.Enabled = False Else TxtNama.Clear() TxtNama.Enabled = False TxtTeori.Clear() TxtTeori.Enabled = False TxtPraktek.Clear() TxtPraktek.Enabled = False BtnSimpan.Enabled = False BtnUpdate.Enabled = False BtnHapus.Enabled = False End If End Using End Using End Sub Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click SQL = "Insert Into Nilai Values ('" & TxtNIM.Text & _ "', '" & TxtNama.Text & "', '" & Val(TxtTeori.Text) & _ "', '" & Val(TxtPraktek.Text) & "')" Using Cmd As New OdbcCommand(SQL, Conn) RecCount = Cmd.ExecuteNonQuery() If RecCount > 0 Then BtnBaru_Click(New System.Object, New System.EventArgs) Else MessageBox.Show("Data Tidak Dapat Di-Simpan ... !!!", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Using End Sub Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click SQL = "Update Nilai Set Nama = '" & TxtNama.Text & _ "', Teori = '" & Val(TxtTeori.Text) & "', Praktek = '" & _ Val(TxtPraktek.Text) & "' Where NIM = '" & TxtNIM.Text & "'" Using Cmd As New OdbcCommand(SQL, Conn) RecCount = Cmd.ExecuteNonQuery() If RecCount > 0 Then BtnBaru_Click(New System.Object, New System.EventArgs) Else MessageBox.Show("Data Tidak Dapat Di-Update ... !!!", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Using End Sub Private Sub BtnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHapus.Click Dim Hasil As DialogResult SQL = "Delete From Nilai Where NIM = '" & TxtNIM.Text & "'" Using Cmd As New OdbcCommand(SQL, Conn) Hasil = MessageBox.Show("Apakah Anda Yakin Menghapus NIM = " & _

TxtNIM.Text & " ?", "Hapus Data", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) If Hasil = Windows.Forms.DialogResult.Yes Then RecCount = Cmd.ExecuteNonQuery() If RecCount > 0 Then BtnBaru_Click(New System.Object, New System.EventArgs) Else MessageBox.Show("Data Tidak Dapat Di-Hapus ... !!!", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End If End Using End Sub End Class

Contoh coding form2 :


Imports System.Data.Odbc Public Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ConnStr As String = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & My.Application.Info.DirectoryPath & "\Mhs.mdb" Dim Conn As OdbcConnection Dim Ds As DataSet Dim Da As OdbcDataAdapter Dim SQL As String = "Select * From Nilai" Conn = New OdbcConnection(ConnStr) Ds = New DataSet Da = New OdbcDataAdapter(SQL, Conn) Da.Fill(Ds, "Nilai") DGVNilai.DataSource = Ds.Tables("Nilai") DGVNilai.Dock = DockStyle.Fill Catch MessageBox.Show("Tabel Tidak Dapat Ditampilkan ... !!!", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Me.Close() End Try End Sub Private Sub DGVNilai_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVNilai.CellDoubleClick Form1.TxtNIM.Text = DGVNilai.CurrentRow.Cells(0).Value Me.Close() End Sub End Class Try

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy