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

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

[VB6] แจกโค้ดการใช้งานตารางกริด SharpGrid UnBound Data Control ในแบบ Run Time

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

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6052




มารู้จักกับคำว่า Bound Data และ UnBound Data Control
- Bound Data Control คือ การผูก (Bound) ตารางข้อมูล (RecordSet) เข้ากับพวก Component หรือ Control ต่างๆ ผ่านทาง DataSource กรณีที่นำมาใช้แสดงผลลงในตารางกริด มันจะอ่านค่าฟิลด์ต่างๆ เพื่อแสดงผลในแต่ละหลัก เรียงตามลำดับจากการ Query ที่เราเขียนไว้ และ ต้องแสดงทุกๆฟิลด์ออกมาทั้งหมด (หากไม่ต้องการแสดงผล จะต้องทำการซ่อนหลักของตัวกริดเอง) มีข้อดี คือ ทำงานได้เร็ว มักนำไปแสดงผลข้อมูลอย่างเดียว (อ่านรายละเอียดและดาวน์โหลดโค้ดได้ที่นี่)


- UnBound Data Control ก็จะไม่มีการผูก (Unbound) ตารางข้อมูลใดๆเข้ากับ Component หรือ Control การนำไปใช้ในตารางกริด ไม่จำเป็นต้องแสดงผลออกมาทุกฟิลด์ และ จะเอาตัวไหนมาแสดงในหลักใดๆก่อนหลังก็ได้ มีข้อดี คือ ยืดหยุ่นมากกว่า และ มักใช้กับการแก้ไขข้อมูลของตารางกริดในแต่ละ Cell ได้นั่นเอง กรณีของ UnBound Data จะไม่ผูกแหล่งจ่าย หรือ DataSource เข้ากับตัว SharpGrid แต่จะอาศัยการวนลูป หรือการทำซ้ำแทน  

Download SharpGrid ActiveX/COM Grid Control (เฉพาะสมาชิกเท่านั้น) ...


เพิ่มเติมจากวิธีการ BoundData วิธีการในการเข้าถึงข้อมูลในแต่ละเซลล์ เราสามารถใช้ Method CellAt(แถว, หลัก) เพื่อการเข้าถึงข้อมูลได้อย่างรวดเร็ว ...
  1. ' / -----------------------------------------------------------------------------------------------
  2. ' / อ่านค่า Primary Key จากหลัก 0 ที่ถูกซ่อนเอาไว้ เพื่อนำไปใช้งานอย่างอื่นต่อไป
  3. Private Sub SGGridEmployee_DblClick()
  4. ' / -----------------------------------------------------------------------------------------------
  5.     '// การเข้าถึงข้อมูลในแต่ละเซลล์ SharpGrid.CellAt(แถวที่เลือก, หลักที่ต้องการ)
  6.     MsgBox "EmployeePK = " & SGGridEmployee.CellAt(SGGridEmployee.Row, 1) & vbCrLf & _
  7.         "EmployeeName = " & SGGridEmployee.CellAt(SGGridEmployee.Row, 3)
  8. End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดส่วนของฟอร์มหลัก ...
  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 only)
  6. ' / Facebook: https://www.facebook.com/commonindy (World Wide)
  7. ' /
  8. ' / Purpose : Sample to use SharpGrid ActiveX for UnBound Data Control.
  9. ' / Microsoft Visual Basic 6.0 Service Pack 6 + MS Access 2003
  10. ' / -----------------------------------------------------------------------------------------------
  11. Option Explicit

  12. ' / -----------------------------------------------------------------------------------------------
  13. Private Sub Form_Load()
  14. ' / -----------------------------------------------------------------------------------------------
  15. '/ Trap Error
  16. On Error GoTo ErrorHandler
  17.     '// การตั้งฟอร์มไว้กึ่งกลางจอภาพในแบบ Run Time
  18.     Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  19.     '// ทำการติดต่อกับฐานข้อมูลก่อน (modDataBase.bas)
  20.     Call OpenDataBase
  21.     '// เคลียร์ค่า TextBox
  22.     txtSearch.Text = ""
  23.    
  24.     '// ตั้งค่าการแสดงผล SharpGrid
  25.     Call InitSGGrid
  26.     '// ส่งค่า False เพื่อแจ้งโปรแกรมย่อยว่าแสดงผลข้อมูลออกมาทั้งหมด
  27.     Call SGGridEmployeeBound(False)
  28.    
  29. ExitProc:
  30.     Exit Sub
  31.    
  32. ErrorHandler:
  33.     MsgBox "Error : " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "รายงานความผิดพลาด"
  34.     Resume ExitProc
  35.     '
  36. End Sub

  37. ' / -----------------------------------------------------------------------------------------------
  38. Sub InitSGGrid()
  39. ' / -----------------------------------------------------------------------------------------------
  40.     ' Initialize grid properties
  41.     With SGGridEmployee
  42.         '/ ++++++++++++++++++++++++++++++++++++++++
  43.         '/ กำหนดการแสดงผล GroupHeader หรือไม่
  44.         '/ False คือ ไม่ต้องแสดงผล
  45.         .GroupByBoxVisible = False
  46.         '/ True จะแสดงผล GroupHeader ซึ่งจะทำให้ผู้ใช้งานลาก Column ไปจัดกลุ่มได้
  47.         '/ ++++++++++++++++++++++++++++++++++++++++
  48.         
  49.         .FitLastColumn = True
  50.         .Appearance = sg3D
  51.         .SpecialMode = sgModeListBox
  52.         .CellsBorderVisible = True
  53.         .AutoResize = sgAutoResizeColumns
  54.         .GroupIndentation = 225
  55.         
  56.         .DefaultRowHeight = 390
  57.         .RowHeightMin = 390
  58.         .HeadingColCount = 1
  59.         
  60.         .HeadingGridLinesColor = vbBlack
  61.         .HeadingGridLines = sgGridLineFlat
  62.         
  63.         '/ สลับสีแถว
  64.         .EvenOddStyle = sgEvenOddRows
  65.         .ColorOdd = &HEFEFE0
  66.         
  67.         .CellTips = sgCellTipsFloat
  68.         .CellTipsDelay = 400
  69.         .ScrollBarTips = sgScrollTipsVertical
  70.         
  71.         .CacheAllRecords = True
  72.         .ColumnClickSort = True
  73.         
  74.         '/ การแสดงผลปกติทั่วไป
  75.         With .Styles("Normal")
  76.             .BkgStyle = sgCellBkgSolid
  77.             .Font.Name = "Tahoma"
  78.             .Font.Size = 8
  79.             .Padding = 18
  80.         End With
  81.       
  82.       '/ ส่วนหัวของ Header กรณีการแสดงผลทั่วไป
  83.         With .Styles("Heading")
  84.             .BackColor = RGB(0, 153, 255)
  85.             .ForeColor = vbWhite
  86.             .Font.Name = "Tahoma"
  87.             .Font.Size = 9
  88.             .Font.Bold = True
  89.             .Padding = 75
  90.         End With
  91.         
  92.         '/ ส่วนหัวของ GroupHeader กรณีมีการจัดกลุ่ม
  93.         With .Styles("GroupHeader")
  94.             .Font.Size = 9
  95.             .Font.Bold = True
  96.             .BackColor = RGB(241, 239, 226)
  97.             .BkgStyle = sgCellBkgSolid
  98.             .Padding = 30
  99.             .BorderColor = RGB(241, 207, 0)
  100.             .Borders = sgCellBorderBottom
  101.             .BorderSize = 1
  102.         End With
  103.       
  104.         '/ ส่วนของ GroupFooter กรณีมีการจัดกลุ่ม
  105.         With .Styles("GroupFooter")
  106.             .Font.Size = 9
  107.             .Font.Name = "Tahoma"
  108.             .ForeColor = vbBlue
  109.             .BackColor = RGB(255, 255, 224)
  110.             .BkgStyle = sgCellBkgSolid
  111.             .Padding = 75
  112.             .BorderColor = RGB(255, 207, 0)
  113.             .Borders = sgCellBorderBottom
  114.             .BorderSize = 50
  115.             .TextAlignment = sgAlignLeftCenter
  116.         End With
  117.       
  118.         With .Styles("Tip")
  119.             .Font.Size = 10
  120.             .Padding = 40
  121.         End With
  122.       
  123.        '/ กำหนดสีให้แถบแสงที่เราเลือกแถว
  124.         With .Styles("Selection")
  125.             .BackColor = RGB(0, 170, 0)
  126.             .ForeColor = vbWhite
  127.             .BkgStyle = sgCellBkgSolid
  128.         End With
  129.       
  130.       '/ กำหนดสีให้แถบแสง กรณีที่เราไม่ได้เลือกแถวนั้น
  131.         With .Styles("InactiveSelection")
  132.             .BackColor = RGB(0, 170, 0)
  133.             .ForeColor = vbWhite
  134.             .BkgStyle = sgCellBkgSolid
  135.         End With
  136.     End With

  137. End Sub

  138. ' / -----------------------------------------------------------------------------------------------
  139. ' / อ่านค่า Primary Key จากหลัก 0 ที่ถูกซ่อนเอาไว้ เพื่อนำไปใช้งานอย่างอื่นต่อไป
  140. Private Sub SGGridEmployee_DblClick()
  141. ' / -----------------------------------------------------------------------------------------------
  142.     '// การเข้าถึงข้อมูลในแต่ละเซลล์ SharpGrid.CellAt(แถวที่เลือก, หลักที่ต้องการ)
  143.     MsgBox "EmployeePK = " & SGGridEmployee.CellAt(SGGridEmployee.Row, 1) & vbCrLf & _
  144.         "EmployeeName = " & SGGridEmployee.CellAt(SGGridEmployee.Row, 3)
  145. End Sub

  146. ' / -----------------------------------------------------------------------------------------------
  147. ' / ตัวแปร blnSearch เป็นออพชั่น หากไม่มีการส่งค่ามา ให้ถือว่ามันมีค่าเป็น FALSE
  148. ' / หาก blnSearch = False เป็นการแสดงผลข้อมูลทั้งหมด โดยไม่ต้องทำการค้นหา
  149. ' / หาก blnSearch = True เป็นการค้นหาข้อมูล ตามเงื่อนไขที่อยู่ใน TextBox
  150. Sub SGGridEmployeeBound(Optional blnSearch As Boolean = False)
  151. ' / -----------------------------------------------------------------------------------------------
  152.     With SGGridEmployee
  153.         '.Columns.RemoveAll False
  154.         '/ +++++++++++++++++++++++++++++++++++++++++++++++
  155.         '/ ต้องกำหนดจำนวนหลักให้กับ SharpGrid กรณีที่ใช้งานแบบ UnBound
  156.         '/ หากไม่ต้องการกำหนดจำนวน เราสามารถใช้ Method Add Column แทน
  157.         .DataColCount = 7
  158.         '/ +++++++++++++++++++++++++++++++++++++++++++++++
  159.         .DataRowCount = 0
  160.         ' / หลัก 0 นี่คือหลักที่แสดงหมายเลขแถวครับ
  161.         .Rows.At(0).Height = 420
  162.         .ColumnClickSort = True
  163.         .Columns(0).Width = 400
  164.         .Columns(0).AllowSizing = False
  165.         .Columns(0).Hidden = True
  166.         .RowNumbering = False
  167.     End With
  168.    
  169.     '/ สร้าง Instance ขึ้นมาใหม่ พร้อมตัดการเชื่อมต่อเดิมทิ้ง (หากลืม)
  170.     Set RS = New ADODB.Recordset
  171.    '/ blnSearch = True คือการค้นหาข้อมูล
  172.     If blnSearch Then
  173.         '// Bound Data จะแสดงผลโดยจัดเรียงข้อมูลตามฟิลด์ ที่อยู่ใน Query
  174.         '// หากฟิลด์ใดที่เราไม่ต้องการให้แสดงผล ต้องไปตั้งค่าหลักนั้นใน SharpGrid ให้ Hidden = True
  175.         Statement = _
  176.             " SELECT tblEmployee.EmployeePK, tblEmployee.EmployeeID, tblEmployee.EmployeeName, tblPosition.PositionName, " & _
  177.             " tblDepartment.DepartmentName, tblEmployee.DateStart, tblEmployee.Salary " & _
  178.             " FROM (tblEmployee INNER JOIN tblDepartment ON tblEmployee.DepartmentFK = tblDepartment.DepartmentPK) INNER JOIN " & _
  179.             " tblPosition ON tblEmployee.PositionFK = tblPosition.PositionPK " & _
  180.             " WHERE " & _
  181.             " ([EmployeeID] " & " Like '%" & Trim(txtSearch.Text) & "%'" & " OR " & _
  182.             " [EmployeeName] " & " Like '%" & Trim(txtSearch.Text) & "%'" & " OR " & _
  183.             " [PositionName] " & " Like '%" & Trim(txtSearch.Text) & "%'" & " OR " & _
  184.             " [DepartmentName] " & " Like '%" & Trim(txtSearch.Text) & "%'" & ")" & _
  185.             " ORDER BY EmployeeID, EmployeeName "
  186.    
  187.     '// blnSearch = False คือ แสดงผลทั้งหมด
  188.     Else
  189.         Statement = _
  190.             " SELECT tblEmployee.EmployeePK, tblEmployee.EmployeeID, tblEmployee.EmployeeName, tblPosition.PositionName, " & _
  191.             " tblDepartment.DepartmentName, tblEmployee.DateStart, tblEmployee.Salary " & _
  192.             " FROM (tblEmployee INNER JOIN tblDepartment ON tblEmployee.DepartmentFK = tblDepartment.DepartmentPK) INNER JOIN " & _
  193.             " tblPosition ON tblEmployee.PositionFK = tblPosition.PositionPK " & _
  194.             " ORDER BY DepartmentName, EmployeeID "
  195.     End If
  196.    
  197.     ' ============ IMPORTANT ==========
  198.     ' / ระบุให้ทำงานในโหมด UNBOUND DATA
  199.     SGGridEmployee.DataMode = sgUnbound
  200.     ' ==================================
  201.     RS.CursorLocation = adUseClient
  202.     '// adOpenForwardOnly และ adLockReadOnly เป็นการอ่านข้อมูลเดินหน้าอย่างเดียว จะมีความเร็วในการนำข้อมูลมาแสดงผล
  203.     RS.Open Statement, ConnDB, adOpenForwardOnly, adLockReadOnly, adCmdText
  204.    
  205.     '/ =============== ผูกเข้ากับตาราง RecordSet ================
  206.     '/ BOUND DATA
  207.     '/ เหมือนการกำหนด DataSource ใน VB.NET
  208.     'Set SGGridEmployee.DataSource = RS
  209.     '/ ====================================================
  210.     '/ แสดงจำนวนรายการ
  211.     SGGridEmployee.DataRowCount = RS.RecordCount
  212.     If RS.RecordCount >= 0 Then
  213.         lblSearch.Caption = "[จำนวน : " & RS.RecordCount & " รายการ.]"
  214.     Else
  215.         lblSearch.Caption = "[จำนวน : 0" & " รายการ.]"
  216.     End If
  217.    
  218.     '// จัดรูปแบบหลักใหม่
  219.     Call SetupSGGrid
  220.    
  221.     '// ++++++++++++++++++ UNBOUND DATA +++++++++++++++++++
  222.     '// การ UnBound ก็คือการทำ Loop หรือการทำซ้ำ (Repetitive) นั่นเอง
  223.     '// โดยที่เราเลือกมาเฉพาะบางฟิลด์ก็ได้ ไม่จำเป็นต้องนำมาจากการ Query ทั้งหมด
  224.     Dim Rec As Long
  225.     '// ที่เลือก FOR ก็เพราะว่ามันมีค่าเริ่มต้น - สิ้นสุด และมีการนับเพิ่ม (หรือลด) แบบคงที่
  226.     '// ลองแก้ไขโดยใช้ Do While หรือ Do Until แทนครับผม
  227.     For Rec = 1 To SGGridEmployee.DataRowCount
  228.         With SGGridEmployee
  229.             .CellAt(Rec, 1) = RS("EmployeePK")
  230.             ' / การใส่ "" ไว้ด้านหน้าฟิลด์ข้อมูลแบบ String เพื่อป้องกัน Error หากมีค่าเป็น Null
  231.             .CellAt(Rec, 2).Value = "" & RS("EmployeeID")
  232.             .CellAt(Rec, 3).Value = "" & RS("EmployeeName")
  233.             .CellAt(Rec, 4) = "" & RS("PositionName")
  234.             .CellAt(Rec, 5) = "" & RS("DepartmentName")
  235.             .CellAt(Rec, 6) = "" & Format(RS("DateStart"), "dd/MM/yyyy")
  236.             .CellAt(Rec, 7) = Format(RS("Salary"), "#,##0.00")
  237.         End With
  238.         RS.MoveNext
  239.     Next

  240.     ' / Always must be disconnect RecordSet
  241.     RS.Close:   Set RS = Nothing
  242. End Sub

  243. ' / -----------------------------------------------------------------------------------------------
  244. ' / การตั้งค่าหลักต่างๆ ต้องให้ตรงกันกับฟิลด์ข้อมูลในการ Query จากโปรแกรมย่อย SGGridEmployeeBound
  245. Private Sub SetupSGGrid()
  246. ' / -----------------------------------------------------------------------------------------------
  247.     '/ การกำหนดโหมด Bound Data ทำให้เกิดการนับจำนวนหลักเอาไว้ให้เรียบร้อย
  248.     ' ================ Setup SGGrid ====================
  249.     With SGGridEmployee
  250.         '// Start Columns 1 โดยใช้ค่า Index ที่เรียงมาจาก Query
  251.         With .Columns(1)
  252.             '/ แสดงผลหัวข้อหลัก
  253.             .Caption = "EmployeePK"
  254.             '/ ความกว้างเป็น 0
  255.             .Width = 0
  256.             '/ ไม่ยอมให้ปรับขนาดความกว้างของหลัก
  257.             .AllowSizing = False
  258.             '/ เอาไปซ่อนจากสายตาผู้ใช้ไปเลย กรณีที่เราไม่ต้องการให้หลักนี้มีการแสดงผลให้ผู้ใช้งานเห็น
  259.             .Hidden = True
  260.         End With
  261.         ' 2
  262.         With .Columns(2)
  263.             .Caption = "รหัสพนักงาน"
  264.             '/ ตั้งค่าการแสดงตำแหน่งของข้อมูล
  265.             .Style.TextAlignment = sgAlignLeftCenter
  266.             .HeadingStyle.TextAlignment = sgAlignLeftCenter
  267.             .AllowSizing = True
  268.         End With
  269.         ' 3
  270.         With .Columns(3)
  271.             .Caption = "ชื่อ - นามสกุล"
  272.             .Style.TextAlignment = sgAlignLeftCenter
  273.             .HeadingStyle.TextAlignment = sgAlignLeftCenter
  274.             .AllowSizing = True
  275.         End With
  276.         ' 4
  277.         With .Columns(4)
  278.             .Caption = "ตำแหน่ง"
  279.             .Style.TextAlignment = sgAlignLeftCenter
  280.             .HeadingStyle.TextAlignment = sgAlignLeftCenter
  281.             .AllowSizing = True
  282.         End With
  283.         ' 5
  284.         With .Columns(5)
  285.             .Caption = "แผนก"
  286.             .Style.TextAlignment = sgAlignLeftCenter
  287.             .HeadingStyle.TextAlignment = sgAlignLeftCenter
  288.             .AllowSizing = True
  289.         End With
  290.         ' 6
  291.         With .Columns(6)
  292.             .Caption = "วันที่เริ่มทำงาน"
  293.             .Style.TextAlignment = sgAlignRightCenter
  294.             .HeadingStyle.TextAlignment = sgAlignRightCenter
  295.             .Style.Format = "dd/mm/yyyy"
  296.             .AllowSizing = True
  297.         End With
  298.         ' 7
  299.         With .Columns(7)
  300.             .Caption = "เงินเดือน"
  301.             .Style.TextAlignment = sgAlignRightCenter
  302.             .HeadingStyle.TextAlignment = sgAlignRightCenter
  303.             .DataType = sgtCurrency
  304.             .Style.Format = "#,##0.00"
  305.             .AllowSizing = False
  306.         End With
  307.         '
  308.     End With
  309. End Sub

  310. ' / -----------------------------------------------------------------------------------------------
  311. ' / กดปุ่ม Refresh เพื่อทำการแสดงผลข้อมูลทั้งหมด
  312. Private Sub cmdRefresh_Click()
  313. ' / -----------------------------------------------------------------------------------------------
  314.     Call SGGridEmployeeBound(False)
  315.     With SGGridEmployee
  316.         .LeftCol = 0
  317.         .SetFocus
  318.         Sendkeys "{RIGHT}"
  319.     End With
  320.     txtSearch.Text = ""
  321. End Sub

  322. ' / -----------------------------------------------------------------------------------------------
  323. ' / เมื่อคลิ๊กที่ปุ่มค้นหา
  324. Private Sub cmdSearch_Click()
  325. ' / -----------------------------------------------------------------------------------------------
  326.    
  327.     ' / ตัดคำสงวน (Reserved Word) ที่มีปัญหากับฐานข้อมูลออกไป
  328.     txtSearch.Text = Replace(txtSearch.Text, "'", "")
  329.     txtSearch.Text = Replace(txtSearch.Text, "%", "")
  330.     txtSearch.Text = Replace(txtSearch.Text, "*", "")
  331.     If Trim(txtSearch.Text) = "" Or Len(txtSearch.Text) = 0 Then Exit Sub
  332.    
  333.     '/ ทำการค้นหาข้อมูล
  334.     Call SGGridEmployeeBound(True)
  335.    
  336. End Sub

  337. Private Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
  338.     If KeyCode = vbKeyDown Then Sendkeys "{TAB}"
  339.     If KeyCode = vbKeyUp Then Sendkeys "+{TAB}"
  340. End Sub

  341. Private Sub txtSearch_KeyPress(KeyAscii As Integer)
  342.     '// เกิดการกด Enter (ASCII Code มีค่าเท่ากับ 13 ฐาน 10)
  343.     If KeyAscii = 13 Then
  344.         Call cmdSearch_Click
  345.         '// ปิดเสียงด้วยการกำหนดให้ KeyAscii = 0 หรือไม่มีการกดคีย์ใดๆ
  346.         KeyAscii = 0
  347.     End If
  348. End Sub

  349. Private Sub cmdRefresh_KeyDown(KeyCode As Integer, Shift As Integer)
  350.     If KeyCode = vbKeyDown Or KeyCode = vbKeyRight Then Sendkeys "{TAB}"
  351.     If KeyCode = vbKeyUp Or KeyCode = vbKeyLeft Then Sendkeys "+{TAB}"
  352. End Sub

  353. Private Sub Form_Resize()
  354.     If Me.ScaleWidth < 120 Or Me.ScaleHeight < 120 Then Exit Sub
  355.     fraData.Height = Me.ScaleHeight - 60
  356.     fraData.Move 15, 0, Me.ScaleWidth - 15
  357.     SGGridEmployee.Move 15, 615, fraData.Width - (SGGridEmployee.Left) - 15, fraData.Height - 690
  358. End Sub

  359. Private Sub Form_Unload(Cancel As Integer)
  360. On Error Resume Next
  361.     If Dir$(App.Path & "\*.tmp") <> "" Then Kill App.Path & "\*.tmp"
  362.     '/ ปิดการเชื่อมต่อกับฐานข้อมูล
  363.     Call CloseDataBase
  364.     End
  365. End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดในส่วนของโมดูลหากิน (modDataBase.bas)
  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 only)
  6. ' / Facebook: https://www.facebook.com/commonindy (World Wide)
  7. ' /
  8. ' / Purpose : Function to connect the MS Access DataBase.
  9. ' / Microsoft Visual Basic 6.0 Service Pack 6 + MS Access 2003
  10. ' / -----------------------------------------------------------------------------------------------
  11. Option Explicit

  12. '// ก่อนใช้งานต้องเลือก MDAC 2.8 มาจาก Project --> Refernces ก่อน
  13. Global ConnDB As New ADODB.Connection
  14. Global RS As New ADODB.Recordset    ' / RecordSet หลัก
  15. Global DS As New ADODB.Recordset    ' / RecordSet สำรอง
  16. Global RstData As New ADODB.Recordset   ' / ใช้ในการพิมพ์รายงาน
  17. Global Statement As String
  18. Global SQLStmt As String

  19. ' / -----------------------------------------------------------------------------------------------
  20. Public Sub OpenDataBase()
  21. On Error GoTo Err_Handler
  22.     Dim DB_File As String
  23.     DB_File = App.Path
  24.     If Right$(DB_File, 1) <> "" Then DB_File = DB_File & ""
  25.     DB_File = DB_File & "DataBase.MDB"
  26.     ' Open a connection.
  27.     Set ConnDB = New ADODB.Connection
  28.     ConnDB.ConnectionString = _
  29.         "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  30.         "Data Source=" & DB_File & ";" & _
  31.         "Persist Security Info=False"
  32.         '" Jet OLEDB:Database Password=" & "password" & ";" '& _
  33.         '" Engine Type=5;"
  34.     ConnDB.Open
  35.     Exit Sub
  36. Err_Handler:
  37.     MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
  38.     End
  39. End Sub

  40. Public Sub CloseDataBase()
  41.     ' ตรวจสอบว่ามีการเชื่อมโยง - Connect ข้อมูลหรือไม่
  42.     If ConnDB.State = adStateOpen Then
  43.         ConnDB.Close
  44.         Set ConnDB = Nothing
  45.     End If
  46. End Sub

  47. ' / -----------------------------------------------------------------------------------------------
  48. ' / ฟังค์ชั่นแก้ไขในการ SendKeys ซึ่งใน Windows 8+ 64 บิท จะมีปัญหา
  49. Public Sub Sendkeys(Text As String, Optional Wait As Boolean = False)
  50.     Dim WshShell As Object
  51.     Set WshShell = CreateObject("Wscript.shell")
  52.     WshShell.Sendkeys Text, Wait
  53.     Set WshShell = Nothing
  54. End Sub
คัดลอกไปที่คลิปบอร์ด

Conclusion: ก็คงจะเห็นได้ชัดเจนว่า ทั้งแบบ Bound หรือ UnBound ต่างก็แสดงผลได้เหมือนกัน แต่วิธีการ UnBound Data เหมาะสมกับการนำข้อมูลมาเพื่อทำการแก้ไขในตัวตารางกริด หากเปรียบเทียบกับ VB.NET ก็คงเหมือนกับการใช้งาน Data Reader นั่นเอง


ดาวน์โหลดโค้ดต้นฉบับ VB6 + SharpGrid ได้ที่นี่

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

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

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

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

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

GMT+7, 2024-4-26 16:38 , Processed in 0.116227 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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