[VB.NET] แสดงผลรหัสไปรษณีย์ประเทศไทย ด้วย ComboBox เพื่อทำการเลือกรายการ
http://www.g2gnet.com/webboard/images/vbnet/PostCodeCombo.pngโปรเจคนี้แอดมินทำเอาไว้เมื่อประมาณ 3 ปีที่แล้ว ขุดกรุเอามาเล่าสู่กันฟังใหม่ จะเป็นการแสดงผลโดยให้เลือกจังหวัดก่อน ตามด้วยเลือกอำเภอ และตำบลตามลำดับ จากนั้นก็จะแสดงรหัสไปรษณีย์ออกมา ... การทำ Query ที่สำคัญคือ DISTINCT เป็นการคัดกรองข้อมูลที่ซ้ำกันให้เหลือเพียง 1 เดียว ...
มาดูโค้ดกันเถอะ ...
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Purpose: View the PostCode data in Thailand with ComboBox.
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
' / Microsoft Visual Basic .NET (2010) & MS Access 2003+
' / ----------------------------------------------------------------------------------------
Imports System.Data.OleDb
Public Class frmMain
Dim Conn As OleDb.OleDbConnection
Dim Cmd As OleDb.OleDbCommand
Dim DR As OleDb.OleDbDataReader
Dim Statement As String
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
lblPostCode.Text = ""
txtRemark.Text = ""
Dim strPath As String = Application.StartupPath
strPath = strPath.ToLower()
strPath = strPath.Replace("\bin\debug", "\")
Dim strConn As String = _
" Provider=Microsoft.ACE.OLEDB.12.0;" & _
" Data Source = " & strPath & "Data\PostCode2555.accdb; " & _
" Persist Security Info=False;"
Try
' เปิดการเชื่อมต่อไฟล์ฐานข้อมูล
Conn = New OleDb.OleDbConnection(strConn)
Conn.Open()
'// โหลดรายการจังหวัดเข้ามาก่อน
Call LoadProvice()
Catch ex As Exception
MsgBox(ex.Message)
Application.Exit()
End Try
End Sub
' / โหลดรายชื่อจังหวัดเข้าสู่ ComboBox
Sub LoadProvice()
lblPostCode.Text = ""
txtRemark.Text = ""
' DISTINCT คือ หากชื่อรายการมันซ้ำ ต้องตัดให้เหลือเพียงรายการเดียว
Statement = _
" SELECT DISTINCT PostCode.Province " & _
" From PostCode ORDER BY PostCode.Province "
Cmd = New OleDbCommand(Statement, Conn)
DR = Cmd.ExecuteReader
While DR.Read()
cmbProvince.Items.Add(DR.Item("Province").ToString)
End While
DR.Close()
Cmd.Dispose()
End Sub
' / เลือกรายชื่อจังหวัด และเอารายชื่ออำเภอเข้าสู่ ComboBox
Private Sub cmbProvince_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmbProvince.SelectedValueChanged
cmbAmphur.Items.Clear()
cmbAmphur.Text = ""
cmbTumbon.Items.Clear()
cmbTumbon.Text = ""
lblPostCode.Text = ""
txtRemark.Text = ""
Statement = _
" SELECT DISTINCT PostCode.Amphur, PostCode.Province " & _
" From PostCode" & _
" WHERE " & _
" Province = " & "'" & cmbProvince.Text & "'" & _
" ORDER BY PostCode.Amphur "
Cmd = New OleDbCommand(Statement, Conn)
DR = Cmd.ExecuteReader
While DR.Read()
cmbAmphur.Items.Add(DR.Item("Amphur").ToString)
End While
DR.Close()
Cmd.Dispose()
End Sub
' / เลือกรายชื่ออำเภอ และเอารายชื่อตำบลเข้าสู่ ComboBox
Private Sub cmbAmphur_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmbAmphur.SelectedValueChanged
cmbTumbon.Items.Clear()
cmbTumbon.Text = ""
lblPostCode.Text = ""
txtRemark.Text = ""
Statement = _
" SELECT DISTINCT PostCode.Amphur, PostCode.Province, PostCode.Tumbon " & _
" From PostCode" & _
" WHERE " & _
" Province = " & "'" & cmbProvince.Text & "'" & _
" AND " & _
" Amphur = " & "'" & cmbAmphur.Text & "'" & _
" ORDER BY PostCode.Tumbon "
Cmd = New OleDbCommand(Statement, Conn)
DR = Cmd.ExecuteReader
While DR.Read()
cmbTumbon.Items.Add(DR.Item("Tumbon").ToString)
End While
DR.Close()
Cmd.Dispose()
End Sub
' / เลือกรายชื่อตำบล และเอารหัสไปรษณีย์มาแสดงผลใน Label Control
Private Sub cmbTumbon_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cmbTumbon.SelectedValueChanged
lblPostCode.Text = ""
txtRemark.Text = ""
Statement = _
" SELECT PostCode.Province, PostCode.Amphur, PostCode.Tumbon, " & _
" PostCode.PostCode, PostCode.Remark " & _
" From PostCode" & _
" WHERE " & _
" Province = " & "'" & cmbProvince.Text & "'" & _
" AND " & _
" Amphur = " & "'" & cmbAmphur.Text & "'" & _
" AND " & _
" Tumbon = " & "'" & cmbTumbon.Text & "'" & _
" ORDER BY PostCode.Tumbon "
Cmd = New OleDbCommand(Statement, Conn)
DR = Cmd.ExecuteReader
While DR.Read()
' การนำเอา Double Quote มาวางขวางก่อน ก็เพราะเผื่อข้อมูลมันเป็น Null ก็จะเกิด Error
' ใช้หลักการเดียวกันกับ VB6 เลยครับท่านผู้ชม
lblPostCode.Text = DR.Item("PostCode").ToString
txtRemark.Text = "" & DR.Item("Remark")
End While
DR.Close()
cmd.Dispose()
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Conn.Close()
Me.Dispose()
Application.Exit()
End Sub
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่
ขอบคุณครับ
หน้า:
[1]