ҡդӶ ʵŧ 纺촨շ٨絴͵ ᷹ФѺ

͵Դҷҧ Inbox Ѻ

˹ѡ
-
VB 6/VB.Net
ASP/ASP.Net
Ѻ¤
¹ҹ Flash Movie
ռͺش  
 RSS Feeds
 ǹŴ RSS Reader ...   Download  RSS Reader

Forum - www.g2gnet.com
Webmaster - www.g2gnet.com
Visitors - Session views
 5 6 1 6 1 6 5

7 ѹҤ ..2549
120 Users On-Line.
Visitors - Page views
 8 9 7 3 9 0 8
1 Ҿѹ ..2551

Google   
g2gnet.com
Ҵѡ:  

¹ Visual Basic 6.0 Ѻ ҹ MS Access Ҥ 3

Category »  VB 6/VB.Net
: Webmaster 9/11/2549 13:55:00
(ҹ : 77998) 

͹͡Ẻ Visual Basic 6.0 ǤѺ


ҡҤ 2 ҹ Ըա͵ҧ 2 ҧ ҡѹ繷ºǤѺ ѺҤйҹ ѡѺ͡Ẻ Ѳ MS Visual Basic 6.0 ٻẺͧ Run-Time (ͧӧҹ͹ Ҩ֧繼Ѿͧ÷ӧҹ) Ҩᵡҧҡ˹ѧͷ ͺйҹ ¹ ѲٻẺͧ Data Control ѡѺͧ
Data Control Design Time Ըաü١Դ͹ (Bound Control) չФѺ ʹѹմѧ
    ʹ
  • ӧҹǡԸաẺ Run-Time
  • ѺҴѺѺ͹ҡѡ
  • Ѻ¹ͧ ǹҹͧ ...

躷 (Чҹҧͧ) еͧ͡Ẻ ¹鴡ѹẺͧ Run-Time ١͹ ѺŴͧҧ Ѻ ... ͹Դ Visual Basic 6.0 ѹҤѺͧ ; ѹѺáͧ¡ҹ References - ҧԧ դ ͧҧԧҹҹżҹҧ MDAC ѧҾҹҧ


仡͡ Microsoft ActiveX Data Objects 2.8 Library Version ѹ٧شФѺ ...


Ѻ MS Windows XP ѹеԴ ActiveX (Version 2.8) ǹ
ǹ Windows 蹷ӡҹ餧ͧ Download MDAC ҵԴͧ


ҹ繨ҡҾշ Version 2.5, 2.6 2.7 Ẻ ... Դ MDAC ͧФѺ ЧҹͧѲҢҹ շ駧ҹ ѧͧҹ Windows 蹤Ѻ Ҩ 95, 98 Win ME ǷҹЧҷͧͧҹѹ ...

Ѻǹͧ Componentsѵ Object Control Ẻ鹰ҹ ФѺ 繵ͧ¡ҹ Microsoft ADO Data Control (OLEDB) ¤Ѻ ... ͡Ըչ繡͡Ẻͧ Run-Time ѹšᵡҧ仨ҡҹѹҫ͡ФѺ ... ͧ

 

鹵͹ ... ͡ҧѵ (Object) Control 躹ФѺ

    觨лСͺ仴
  • Label ӹǹ 7 (˹ Index Ѻ lblCustomer(0)=١ lblCustomer(1)=١ 繵)
  • TextBox ӹǹ 6 ͧ˹ Index ФѺ ФسШӪҡ ҡ TextBox Ժ
  • ComboBox ӹǹ 1 ѺªͨѧѴ (Province)
  • CommandButton ӹǹ 1 (͹) Դ
  • ǹ MS Flex Grid Control 㹡 List ª١͡ҹ ҨйҡǶ֧Ҥ ... ѺҤ͢仡͹ФѺ


    Properties سѵԷӤѭѺ TextBox е մѧ (Design Time)
      ١
    1. Name: txtCustomerID
    2. Text: txtCustomerID
    3. Locked: True

    1. Name: txtFirstname
    2. Text: txtFirstname
    3. MaxLength: 40 (ҧԧǨҡҧ)

      ʡ
    1. Name: txtLastname
    2. Text: txtLastname
    3. MaxLength: 40 (ҧԧǨҡҧ)

    1. Name: txtAddress
    2. Text: txtAddress
    3. MaxLength: 150 (ҧԧǨҡҧ)

    1. Name: txtAmphur
    2. Text: txtAmphur
    3. MaxLength: 50 (ҧԧǨҡҧ)

      ɳ
    1. Name: txtPostCode
    2. Text: txtPostCode
    3. MaxLength: 5 (ҧԧǨҡҧ)
ͧѧࡵ¹ФѺ سѵԢͧ Text .... ͧŧ仴¹ ͪҤ͹ŵǹ ѹժúҧ TextBox ҡ աҧ ҡҵͧ͹Ѻ䢧ҹաͺ ѧҡҼҹѡ͹֧٫ 觧ͤѺ ... ͧ ѹ礧ͧ֡繹´ǡѹ 价ӵ˹ѧͷҺ͡ҡѡ Ẻ Index ѧ Name(0), Name(1), ... 仨֧ Name(30) Ǩ Name(17) ѹ ????? ... ԴѹǤѺͧ
    Properties سѵԷӤѭѺ ComboBox մѧ
      ʴªͨѧѴ
    1. Name: cmbProvince
    2. Text: cmbProvince ˵ؼ͹Ѻ TextBox
    3. Style: 2 - Drop Down List
      ѹ繡úѧѺ ComboBox ʴҹҧ (Read only) ͧ ҹԧ ͡Ẻ蹹͡Ѻ ҤҡѹҤ令Ѻ ... Ѻ¹Ѻ к͡Ƿҧҧ ѧ 㹡õǨͺͧФѺ

 

͹ҹ鴤觵ҧ仹㹿 á鹵͹ӤѭաС˹ 蹤 ûСȵ ͧҹ 件֧Ẻ Public Module ¡á㹪ͧ Project Add --> Module ҡ͹ФѺ ѧҾҹҧ



Save Ҩ繹ʡ BAS Ѻ
Option Explicit

Global ConnMyDB As New ADODB.Connection ' ҧ Connection ҡѺҹ MS Access
Global RS As New ADODB.Recordset ' ҧҡѺҧ (ѡ)
Global DS As New ADODB.Recordset ' ҧҡѺҧ (ͧ)
Global Statement As String ' ѺûС SQL Statement
'
Global blnNewData As Boolean ' ѺҿҧŢ 繡
'
Public Sub OpenDataBase() ' Դҹ
On Error GoTo Err_Handler
Dim DB_File As String
    DB_File = App.Path
 ' Ǩͺ˹觢ͧ
    If Right$(DB_File, 1) <> "\" Then DB_File = DB_File & "\"
    DB_File = DB_File & "MyDB.MDB"
 ' ҧ Connection
    Set ConnMyDB = New ADODB.Connection
    ConnMyDB.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & DB_File & ";" & _
        "Persist Security Info=False"

    ConnMyDB.Open ' Դº
    Exit Sub
Err_Handler:
    MsgBox "Error : " & Err.Number & " " & Err.Description ' ´ѡͼԴҴ - Trap Error
    End
End Sub

Public Sub CloseDataBase() ' Դҹ
    If ConnMyDB.State = adStateOpen Then 
        ConnMyDB.Close
        Set ConnMyDB = Nothing
    End If
End Sub
ѧ Save (ʡ BAS) º Ҥسҧҹਤ ö֧ǹҹ 繵ͧҹҤѴ͡¤Ѻ ... ˵ؼ

鹡ŧʴ


ФѺ ... ͹ѺҾԨóҨҡҧ Concept ѡͧǷҧẺ Run-Time ѡѧ仹
  1. SetupScreen Clear ˹Ҩ ǧ ҧǡ TextBox ѹ繪ͧҧҹФѺ ;Ѻ Ҩ繡 䢢š
  2. RecordToScreen ͹Ӣʴ Ҩա䢢ŵ
  3. ʴŨҡҧ (Detail) ComboBox 㹵ҧ DisplayProvince ʴªͨѧѴ
Sub SetupScreen() ' Clear ˹Ҩ
    txtCustomerID.Text = "" ' ١
    txtFirstname.Text = "" ' ١
    txtLastname.Text = "" ' ʡ
    txtAddress.Text = "" 
    txtAmphur.Text = "" 
    cmbProvince.Clear ' ѧѴ
    txtPostCode.Text = "" ' ɳ
End Sub

Sub RecordToScreen() ' Ӣʴ
Set RS = New Recordset
    ' SQL Statement ǹФѺ ҵѴҨҡ MS Access
    Statement = "SELECT tblCustomer.*, tblProvince.ProvinceName " & _
        " FROM tblCustomer INNER JOIN tblProvince ON tblCustomer.ProvinceID = tblProvince.ProvinceID " & _
        " WHERE [CustomerID] = 2 " ' صҵͧʴŢŢͧ CustomerID դ = 2

    RS.Open Statement, ConnMyDB, adOpenForwardOnly, , adCmdText ' 㹡ʴŹӡҹ (Read Only) 仢ҧ˹ҧ (Forward) ͪ觡ʴҹͧ
    txtCustomerID.Text = "" & RS("CustomerID")
    txtFirstname.Text = "" & Trim(RS("Firstname"))
    txtLastname.Text = "" & Trim(RS("Lastname"))
    txtAddress.Text = "" & Trim(RS("Address"))
    txtAmphur.Text = "" & Trim(RS("Amphur"))
    cmbProvince.Text = RS("ProvinceName") ' ҨҪͨѧѴʴŤѺ
    txtAmphur.Text = "" & Trim(RS("Amphur"))
    txtPostCode.Text = "" & Trim(RS("PostCode"))
 ' Դ͵ҧ͹
RS.Close
Set RS = Nothing
End Sub

Sub DisplayProvince() ' ӢªͨѧѴ ComboBox
Dim Add$
    Set DS = New ADODB.Recordset ' ͡õͧҹѺ ... ͹Ѻ ͧѹԴҴҡõѡѧԴҹ
    Statement = "SELECT * FROM tblProvince ORDER BY ProvinceID"
    Set DS = ConnMyDB.Execute(Statement, , adCmdText)
    cmbProvince.Clear
    Do Until DS.EOF 
        Add$ = "" & Trim(DS("ProvinceName")) ' 蹹лͧѹͼԴҴҡҧ tblProvince 㹿Ŵ ProvinceName ҡѹդ Null
        cmbProvince.AddItem Add$ '  Method ͧùӢ ComboBox Թ硫áդ 0 (͹Ѻ仴ٵҧŢͧ tblProvince)
        DS.MoveNext ' 樹Ҩ
    Loop
 ' Դ͵ҧ͹
    DS.Close
    Set DS = Nothing
End Sub

' չҷͺ÷ӧҹ ʹ٫Ҽѹ͡ҧäѺͧ
Private Sub Form_Load() ' ͺ㹿ѡФѺ
OpenDataBase ' ҧŷҡ˹ Module
Call SetupScreen ' Clear ʴʴͤҧ
Call DisplayProvince ' ŴªͨѧѴ ComboBox
Call RecordToScreen ' ʴŢ͡
End Sub

SetupScreen


DisplayProvince


RecordToScreen


Ҿҡ§ҧ


ͧ CustomerID դҡѺ 8

ͧٹФѺҡ ComboBox 㹿ѡ 10 ʴ ҵͧ ¡Ѻ DisplayProvince աѺԺǵ仴 ѹо֡ҴٹҪѧ ... Ҩ䢴¡ "ѧ" ᷹Ѻ سͧ˹㹵ҧѹçҧ¡ѹ ͤ дǡ ... ͧ令Դ繡úҹ觫ԤѺ ... ͧ

˵: DisplayProvince ѹԴ§ŪͨѧѴҡҡº 㹡óշѹѴ§ѡ ҨԸա㹡ѭҹҧ ??????

ͺ: Ҿ令Դҡ¤Ѻ к§ͧسǷҧ㹡ùӢ͡ʴŴԸա Run-Time ҹͧѺ ѹѧ§Ѻҹԧö͡Ң¤Ѻ ... ͧ ... ͧԴѹǡѹ
͵ - g2gNet Dot Com
Ţ¹ҳԪ硷͹ԡ 0407314800231
CopyLeft © 2004 - 2099 g2gNet.Com All rights reserved.
Email: [email protected] . 08-6862-6560