ผู้เขียน หัวข้อ: [VB.Net] ฟรีโค้ดต้นฉบับ VB.Net+MS Access และโปรแกรมรหัสไปรษณีย์ของประเทศไทย  (อ่าน 93 ครั้ง)

ออฟไลน์ ทองก้อน ทับทิมกรอบ

  • Administrator
  • *****
  • กระทู้: 245
  • เพศ: ชาย
  • Webmaster G2GNet
วันนี้แอดมินขี้เกียจพิมพ์คำอธิบายครับ 5555+

ดาวน์โหลดโค้ดต้นฉบับ VB.Net (2010) ได้ที่นี่ หรือ

ดาวน์โหลดแอพพลิเคชั่นนำไปรันโปรแกรมได้ทันทีที่นี่



มาดูโค้ดกันเถอะ

โค๊ด: [Select]
' / --------------------------------------------------------------------------------
' / 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 " & _
                " [Tumbon] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " [Amphur] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " [Province] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " [PostCode] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
                " [Remark] " & " 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 " & _
            " [Tumbon] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " [Amphur] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " [Province] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " [PostCode] " & " Like '%" & txtSearch.Text & "%'" & " OR " & _
            " [Remark] " & " 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 query from 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

โค้ดส่วนของการติดต่อฐานข้อมูล

โค๊ด: [Select]
' / --------------------------------------------------------------------------------
' / 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 & "ThailandPostCode.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

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

ออฟไลน์ pongnarak

  • Newbie
  • *
  • กระทู้: 3
อาจารย์ครับโปรแกรมมัน Error ครับ

บันทึกการเข้า

ออฟไลน์ ทองก้อน ทับทิมกรอบ

  • Administrator
  • *****
  • กระทู้: 245
  • เพศ: ชาย
  • Webmaster G2GNet
อาจารย์ครับโปรแกรมมัน Error ครับ




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

ออฟไลน์ Mr.Den

  • Jr. Member
  • **
  • กระทู้: 73
  • เพศ: ชาย
ขอบคุณมากครับ

บันทึกการเข้า