thongkorn โพสต์ 2018-6-19 14:01:03

[VB.NET] แจกฟรีโค้ดโปรแกรมและโปรแกรม รหัสไปรษณีย์ทั่วไทย

http://www.g2gnet.com/webboard/images/vbnet/PostCodeThailand.png

ก่อนอื่นก็ต้องขอขอบคุณข้อมูลที่ได้รับมาจากคุณโกสินทร์ ไตรนิคม และ บริษัทไปรษณีย์ไทยจำกัด เป็นอย่างสูงมา ณ ที่นี้ด้วยครับ สำหรับโปรเจคนี้ก็ไม่มีคำอธิบายอะไรมาก เพราะเดิมเคยแจกเป็นโค้ดทั้ง VB6 และ VB.NET มาตั้งนานหลายปีแล้ว พอหลังจากย้ายทั้งโฮสต์ใหม่ เปลี่ยนทั้งเว็บบอร์ดใหม่ ก็เลยพึ่งได้นำมาลงใหม่อีกครั้งครับผม ...


สำหรับผู้ใช้งานทั่วไป ดาวน์โหลดโปรแกรมรหัสไปรษณีย์ทั่วไทย แบบไม่ต้องติดตั้งได้ที่นี่ ...


โค้ดจากฟอร์มหลัก ...
' / --------------------------------------------------------------------------------
' / 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: Thailand Postcode with VB.Net.
' / Microsoft Visual Basic .NET (2010) & MS Access 2007+
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports System.Data.OleDb

Public Class frmPostCode

    ' / --------------------------------------------------------------------------------
    ' / Collect all searches and impressions. Come in the same place
    ' / blnSearch = True, Show that the search results.
    ' / blnSearch is set to False, Show all records.
    Private Sub RetrieveData(Optional ByVal blnSearch As Boolean = False)
      strSQL = _
            " SELECT PostCode.PostCodeID, PostCode.Tumbon, PostCode.Amphur, " & _
            " PostCode.Province, PostCode.PostCode, PostCode.Remark " & _
            " FROM PostCode "
      '// blnSearch = True for Serach
      If blnSearch Then
            strSQL = strSQL & _
                " WHERE " & _
                " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " " & " Like '%" & txtSearch.Text & "%'" & _
                " ORDER BY PostCodeID "
      Else
            strSQL = strSQL & " ORDER BY PostCodeID "
      End If
      '//
      If Conn.State = ConnectionState.Closed Then Conn.Open()
      DA = New OleDb.OleDbDataAdapter(strSQL, Conn)
      DS = New DataSet
      DS.Clear()
      DA.Fill(DS, "PostCode")
      dgvData.DataSource = DS.Tables("PostCode")
      lblRecordCount.Text = "[จำนวน : " & dgvData.RowCount & " รายการ]"
      '//
      Call InitializeGrid()
      '//
      DA.Dispose()
      DS.Dispose()
      Conn.Close()
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Quick search data by specifying keyword.
    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
      '// Undesirable characters for the database ex.', * or %
      txtSearch.Text = Replace(Trim(txtSearch.Text), "'", "")
      txtSearch.Text = Replace(Trim(txtSearch.Text), "%", "")
      txtSearch.Text = Replace(Trim(txtSearch.Text), "*", "")
      If Trim(txtSearch.Text) = "" Or Len(Trim(txtSearch.Text)) = 0 Then Return
      '//
      If Conn.State = ConnectionState.Closed Then Conn.Open()
      strSQL = _
            " SELECT PostCode.PostCodeID, PostCode.Tumbon, PostCode.Amphur, " & _
            " PostCode.Province, PostCode.PostCode, PostCode.Remark " & _
            " FROM PostCode " & _
            " WHERE " & _
            " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " " & " Like '%" & txtSearch.Text & "%'" & _
            " ORDER BY PostCodeID "
      Cmd = New OleDbCommand(strSQL, Conn)
      DR = Cmd.ExecuteReader
      DT = New DataTable
      DT.Load(DR)
      dgvData.DataSource = DT
      lblRecordCount.Text = "[จำนวน : " & dgvData.RowCount & " รายการ]"
      Call InitializeGrid()
      '//
      DT.Dispose()
      DR.Close()
      Conn.Close()
      '//
    End Sub

    ' / --------------------------------------------------------------------------------
    Private Sub InitializeGrid()
      With dgvData
            .Columns(0).HeaderText = "PostCodeID"
            .Columns(0).Visible = False
            '//
            .Columns(1).HeaderText = "ตำบล"
            .Columns(2).HeaderText = "อำเภอ"
            .Columns(3).HeaderText = "จังหวัด"
            .Columns(4).HeaderText = "รหัสไปรษณีย์"
            .Columns(5).HeaderText = "หมายเหตุ"
            .Columns(5).Visible = False
            ' Autosize Column
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            '// Even-Odd Color
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            ' Adjust Header Styles
            With .ColumnHeadersDefaultCellStyle
                .BackColor = Color.Navy
                .ForeColor = Color.Black ' Color.White
                .Font = New Font("Tahoma", 9, FontStyle.Bold)
            End With
      End With
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Clear screen
    Private Sub SetupScreen()
      txtTumbon.Clear()
      txtAmphur.Clear()
      txtProvince.Clear()
      txtPostCode.Clear()
      txtRemark.Clear()
      txtSearch.Clear()
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Double click to edit item.
    ' / By pulling data from the DataGridView to display. Do not go to the database again.
    Private Sub dgvData_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
      Call SetupScreen()
      '//
      Dim iRow As Integer
      '// Read the value of the focus row.
      iRow = dgvData.CurrentRow.Index
      '// Column 0 --> PostCodeID (Primary Key) but hidden it.
      txtTumbon.Text = "" & dgvData.Item(1, iRow).Value
      txtAmphur.Text = "" & dgvData.Item(2, iRow).Value
      txtProvince.Text = "" & dgvData.Item(3, iRow).Value
      txtPostCode.Text = "" & dgvData.Item(4, iRow).Value
      txtRemark.Text = "" & dgvData.Item(5, iRow).Value
      '//
      txtTumbon.Focus()
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Copy from Textbox to clipboard.
    Private Sub btnClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClipboard.Click
      Clipboard.SetText(txtTumbon.Text & vbCrLf & txtAmphur.Text & vbTab & txtProvince.Text & vbTab & txtPostCode.Text)
    End Sub

    Private Sub frmPostCode_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Call ConnectDataBase()
      lblRecordCount.Text = ""
    End Sub

    Private Sub frmPostCode_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      Application.Exit()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
      Call RetrieveData(False)
      txtSearch.Clear()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
      Me.Close()
    End Sub

    Private Sub txtTumbon_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTumbon.KeyPress
      If e.KeyChar = Chr(13) Then
            '// No beep
            e.Handled = True
            '// Focus to next control.
            SendKeys.Send("{TAB}")
      Else
            '// Prevent any key press (Lock)
            e.Handled = True
      End If
    End Sub

    Private Sub txtAmphur_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmphur.KeyPress
      If e.KeyChar = Chr(13) Then
            '// No beep
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            '// Prevent any key press (Lock)
            e.Handled = True
      End If
    End Sub

    Private Sub txtProvince_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtProvince.KeyPress
      If e.KeyChar = Chr(13) Then
            '// No beep
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            '// Prevent any key press (Lock)
            e.Handled = True
      End If
    End Sub

    Private Sub txtPostCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPostCode.KeyPress
      If e.KeyChar = Chr(13) Then
            '// No beep
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            '// Prevent any key press (Lock)
            e.Handled = True
      End If
    End Sub

    Private Sub txtRemark_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemark.KeyPress
      If e.KeyChar = Chr(13) Then
            '// No beep
            e.Handled = True
            SendKeys.Send("{TAB}")
      Else
            '// Prevent any key press (Lock)
            e.Handled = True
      End If
    End Sub
End Class

โมดูลหากิน modDataBase.vb ...
' / --------------------------------------------------------------------------------
' / 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)
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports System.Data.OleDb
Imports Microsoft.VisualBasic

Module modDataBase
    '// Declare variable one time but use many times.
    Public Conn As OleDbConnection
    Public Cmd As OleDbCommand
    Public DS As DataSet
    Public DR As OleDbDataReader
    Public DA As OleDbDataAdapter
    Public DT As DataTable
    Public strSQL As String '// Major SQL
    Public strStmt As String    '// Minor SQL

    '// Data Path
    Public strPathData As String = MyPath(Application.StartupPath)

    Public Function ConnectDataBase() As System.Data.OleDb.OleDbConnection
      strPathData = MyPath(Application.StartupPath)
      Dim strConn As String = _
                "Provider = Microsoft.ACE.OLEDB.12.0;"
      strConn += _
            "Data Source = " & strPathData & "PostCodeThailand.accdb"

      Conn = New OleDb.OleDbConnection(strConn)
      ' Create Connection
      Conn.ConnectionString = strConn
      ' Return
      Return Conn
    End Function

    ' / --------------------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with "\"
    ' / Return : C:\My Project\
    Function MyPath(AppPath As String) As String
      '/ MessageBox.Show(AppPath);
      AppPath = AppPath.ToLower()
      '/ Return Value
      MyPath = AppPath.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\")
      '// If not found folder then put the \ (BackSlash) at the end.
      If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    End Function
End Module

ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่ ...

g2gsoftuser โพสต์ 2022-10-25 19:11:57

ขอบคุณครับ
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] แจกฟรีโค้ดโปรแกรมและโปรแกรม รหัสไปรษณีย์ทั่วไทย