ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4393|ตอบกลับ: 1

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

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6050

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6050



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


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


โค้ดจากฟอร์มหลัก ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose: Thailand Postcode with VB.Net.
  8. ' / Microsoft Visual Basic .NET (2010) & MS Access 2007+
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------
  13. Imports System.Data.OleDb

  14. Public Class frmPostCode

  15.     ' / --------------------------------------------------------------------------------
  16.     ' / Collect all searches and impressions. Come in the same place
  17.     ' / blnSearch = True, Show that the search results.
  18.     ' / blnSearch is set to False, Show all records.
  19.     Private Sub RetrieveData(Optional ByVal blnSearch As Boolean = False)
  20.         strSQL = _
  21.             " SELECT PostCode.PostCodeID, PostCode.Tumbon, PostCode.Amphur, " & _
  22.             " PostCode.Province, PostCode.PostCode, PostCode.Remark " & _
  23.             " FROM PostCode "
  24.         '// blnSearch = True for Serach
  25.         If blnSearch Then
  26.             strSQL = strSQL & _
  27.                 " WHERE " & _
  28.                 " [Tumbon] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  29.                 " [Amphur] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  30.                 " [Province] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  31.                 " [PostCode] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  32.                 " [Remark] " & " Like '%" & txtSearch.Text & "%'" & _
  33.                 " ORDER BY PostCodeID "
  34.         Else
  35.             strSQL = strSQL & " ORDER BY PostCodeID "
  36.         End If
  37.         '//
  38.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  39.         DA = New OleDb.OleDbDataAdapter(strSQL, Conn)
  40.         DS = New DataSet
  41.         DS.Clear()
  42.         DA.Fill(DS, "PostCode")
  43.         dgvData.DataSource = DS.Tables("PostCode")
  44.         lblRecordCount.Text = "[จำนวน : " & dgvData.RowCount & " รายการ]"
  45.         '//
  46.         Call InitializeGrid()
  47.         '//
  48.         DA.Dispose()
  49.         DS.Dispose()
  50.         Conn.Close()
  51.     End Sub

  52.     ' / --------------------------------------------------------------------------------
  53.     ' / Quick search data by specifying keyword.
  54.     Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
  55.         '// Undesirable characters for the database ex.  ', * or %
  56.         txtSearch.Text = Replace(Trim(txtSearch.Text), "'", "")
  57.         txtSearch.Text = Replace(Trim(txtSearch.Text), "%", "")
  58.         txtSearch.Text = Replace(Trim(txtSearch.Text), "*", "")
  59.         If Trim(txtSearch.Text) = "" Or Len(Trim(txtSearch.Text)) = 0 Then Return
  60.         '//
  61.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  62.         strSQL = _
  63.             " SELECT PostCode.PostCodeID, PostCode.Tumbon, PostCode.Amphur, " & _
  64.             " PostCode.Province, PostCode.PostCode, PostCode.Remark " & _
  65.             " FROM PostCode " & _
  66.             " WHERE " & _
  67.             " [Tumbon] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  68.             " [Amphur] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  69.             " [Province] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  70.             " [PostCode] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
  71.             " [Remark] " & " Like '%" & txtSearch.Text & "%'" & _
  72.             " ORDER BY PostCodeID "
  73.         Cmd = New OleDbCommand(strSQL, Conn)
  74.         DR = Cmd.ExecuteReader
  75.         DT = New DataTable
  76.         DT.Load(DR)
  77.         dgvData.DataSource = DT
  78.         lblRecordCount.Text = "[จำนวน : " & dgvData.RowCount & " รายการ]"
  79.         Call InitializeGrid()
  80.         '//
  81.         DT.Dispose()
  82.         DR.Close()
  83.         Conn.Close()
  84.         '//
  85.     End Sub

  86.     ' / --------------------------------------------------------------------------------
  87.     Private Sub InitializeGrid()
  88.         With dgvData
  89.             .Columns(0).HeaderText = "PostCodeID"
  90.             .Columns(0).Visible = False
  91.             '//
  92.             .Columns(1).HeaderText = "ตำบล"
  93.             .Columns(2).HeaderText = "อำเภอ"
  94.             .Columns(3).HeaderText = "จังหวัด"
  95.             .Columns(4).HeaderText = "รหัสไปรษณีย์"
  96.             .Columns(5).HeaderText = "หมายเหตุ"
  97.             .Columns(5).Visible = False
  98.             ' Autosize Column
  99.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  100.             .AutoResizeColumns()
  101.             '// Even-Odd Color
  102.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  103.             ' Adjust Header Styles
  104.             With .ColumnHeadersDefaultCellStyle
  105.                 .BackColor = Color.Navy
  106.                 .ForeColor = Color.Black ' Color.White
  107.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  108.             End With
  109.         End With
  110.     End Sub

  111.     ' / --------------------------------------------------------------------------------
  112.     ' / Clear screen
  113.     Private Sub SetupScreen()
  114.         txtTumbon.Clear()
  115.         txtAmphur.Clear()
  116.         txtProvince.Clear()
  117.         txtPostCode.Clear()
  118.         txtRemark.Clear()
  119.         txtSearch.Clear()
  120.     End Sub

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

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

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

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

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

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

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

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

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

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

  200.     Private Sub txtRemark_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemark.KeyPress
  201.         If e.KeyChar = Chr(13) Then
  202.             '// No beep
  203.             e.Handled = True
  204.             SendKeys.Send("{TAB}")
  205.         Else
  206.             '// Prevent any key press (Lock)
  207.             e.Handled = True
  208.         End If
  209.     End Sub
  210. End Class
คัดลอกไปที่คลิปบอร์ด


โมดูลหากิน modDataBase.vb ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Microsoft Visual Basic .NET (2010)
  8. ' /
  9. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  10. ' / You can modify and/or distribute without to inform the developer.
  11. ' / --------------------------------------------------------------------------------
  12. Imports System.Data.OleDb
  13. Imports Microsoft.VisualBasic

  14. Module modDataBase
  15.     '// Declare variable one time but use many times.
  16.     Public Conn As OleDbConnection
  17.     Public Cmd As OleDbCommand
  18.     Public DS As DataSet
  19.     Public DR As OleDbDataReader
  20.     Public DA As OleDbDataAdapter
  21.     Public DT As DataTable
  22.     Public strSQL As String '// Major SQL
  23.     Public strStmt As String    '// Minor SQL

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

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

  32.         Conn = New OleDb.OleDbConnection(strConn)
  33.         ' Create Connection
  34.         Conn.ConnectionString = strConn
  35.         ' Return
  36.         Return Conn
  37.     End Function

  38.     ' / --------------------------------------------------------------------------------
  39.     ' / Get my project path
  40.     ' / AppPath = C:\My Project\bin\debug
  41.     ' / Replace "\bin\debug" with ""
  42.     ' / Return : C:\My Project\
  43.     Function MyPath(AppPath As String) As String
  44.         '/ MessageBox.Show(AppPath);
  45.         AppPath = AppPath.ToLower()
  46.         '/ Return Value
  47.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  48.         '// If not found folder then put the \ (BackSlash) at the end.
  49.         If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
  50.     End Function
  51. End Module
คัดลอกไปที่คลิปบอร์ด


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

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 19:11:57 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-26 00:44 , Processed in 0.199814 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้