Khóa hàm thụ visual basic 6 0 chương 14

9 215 0
Khóa hàm thụ visual basic 6 0 chương 14

Đang tải... (xem toàn văn)

Thông tin tài liệu

Khóa Hàm Thụ Visual Basic 6.0 Chương Mười Bốn - Dùng Control Data Control Data Từ VB5, Visual Basic cho lập trình viên control để truy cập sỡ liệu, tên đơn sơ Data Như ta biết, có sỡ liệu Microsoft gói kèm ta mua VB6 - Jet Database Engine Jet Database Engine "phòng máy" MS Access Database Management System Cho đến thời VB5, Microsoft cho ta ba kỹ thuật chính: • • • DAO (Data Acess Objects): DAO kỹ thuật bí truyền Microsoft, để dùng với Jet Database Engine Nó dễ dùng, hiệu tiện, bị giới hạn phạm vi MS Access Dầu vậy, thịnh hành có lợi ích thực tiển ODBC (Open Database Connectivity): ODBC thiết kế phép users nối với đủ loại databases mà dùng method Điều nầy cất bớt gánh nặng cho lập trình viên, để cần học kỹ thuật lập trình mà làm việc với loại database Nhất sau nầy cần phải thay đổi loại database, nâng cấp từ Access lên SQLServer chẳng hạn, sửa đổi coding Khi dùng ODBC chung với DAO, ta cho Access Database nối với databases khác Có bất lợi ODBC rắc rối RDO (Remote Data Object): Một lý để RDO thiết kế giải khó khăn rắc rối ODBC Cách lập trình với RDO đơn giản DAO, thật dùng ODBC nên cho phép users nối với nhiều databases Tuy nhiên, RDO không thịnh hành VB6 tiếp tục hổ trợ kỹ thuật nói trên, cho thêm kỹ thuật truy cập database mới, quan trọng, ADO (ActiveX Data Objects) Trong tới ta học ADO với ưu điểm Tuy nhiên, DAO đơn giản hiệu nên ta tiếp tục dùng hữu hiệu hầu hết áp dụng Do nầy kế tập trung vào kỹ thuật lập trình phổ biến với DAO.Cách dùng giản tiện control Data đặt lên Form làm việc với Properties Bạn bắt đầu dự án VB6 mới, cho tên DataControl cách click tên project Project Explorer bên phải edit property Name Properties Window.DoubleClick lên Icon Control Data Toolbox Một Control Data tên Data1 Form Muốn cho nằm bên Form, giống StatusBar, set property Align Properties Window thành - Align Bottom.Click bên phải hàng property DatabaseName, kế click lên nút browse có ba chấm để chọn file Access dabase từ giao thoại cho Data1 Ở ta chọn E:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB , computer bạn nằm disk C hay D Trong chương trình nầy ta muốn làm việc với table Titles database BIBLIO.MDB, để xem edit records Để ý property DefaultType Data1 có trị số 2- UseJet, tức dùng kỹ thuật DAO, thay dùng kỹ thuật ODBC.Khi bạn click lên property Recordsource Data1, click lên tam giác nhỏ bên phải, ComboBox mở cho ta thấy danh sách tables database Bạn chọn Titles Để ý property RecordsetType Data1 có trị số - Table: Cái từ mà ta dùng thường xuyên truy cập liệu VB6 Recordset (bộ records) Recordset Set of records, chứa số records hay record Một record Recordset record lấy từ Table Trong trường hợp ta lấy tất records table hay records thỏa điều kiện, thí dụ ta muốn lấy records sách xuất trước năm 1990 (Year Published < 1990).Một Record Recordset tập hợp cột (columns) từ hai (hay ba) tables qua mối liên hệ one-to-one one-to-many Thí dụ lấy records từ table Titles, ta muốn có thêm chi tiết tên công ty (Company Name) điện thoại (Telephone) nhà xuất (table Publishers) cách dùng Foreign Key PubID table Titles làm Primary Key table Publishers để lấy chi tiết Nếu bạn chưa nắm vững ý niệm Foreign Key đọc lại Database.Trong trường hợp ta xem có virtual (ảo) table tập hợp hai tables Titles Publishers.Bây bạn đặt lên Form labels với captions: Title, Year Published, ISBN Publisher ID Kế cho thêm textboxes tương ứng đặt tên chúng txtTitle, txtYearPublished, txtISBN txtPublisherID.Chọn textbox txtTitle, set property Datasource Properties Window thành Data1 Khi click lên property Datafield txtTitle mở ComboBox bạn thấy liệt kê tên Fields table Titles Đó Data1 coi trung gian lấy table Titles từ database Ở ta chọn cột Title Lập lại công tác nầy cho textboxes kia, chọn cột Year Published (năm xuất bản), ISBN (số lý lịch thư viện quốc tế), PubID (số lý lịch nhà xuất bản) làm Datafield cho chúng Tới đây, chưa viết hàng code, ta chạy chương trình Nó hiển thị chi tiết record table Titles đây: Bạn bấm nút di chuyển Navigator Buttons để đến record đầu (first), trước (previous), kế (next) cuối (last) Mỗi lần bạn di chuyển đến record chi tiết record hiển thị Nếu không dùng Navigator Buttons, ta code để làm công tác tương đưong cách gọi Recordset methods MoveFirst, MovePrevious, MoveNext MoveLast.Khi record cuối Recordset hiển thị, ta gọi method MoveLast property EOF (End-Of-File) Recordset trở thành True Tương tự vậy, record thứ Recordset hiển thị, ta gọi method MovePrevious property BOF (Begin-Of-File) Recordset trở thành True Nếu Recordset chứa record hai properties EOF BOF True Đặc tính hiển thị liệu textboxex theo record thời (current record) gọi data binding hay data bound (buộc vào liệu) control TextBox hỗ trợ chức nầy nói Data Aware (biết bà liệu).Khi record hiển thị, bạn edit Year Published để đổi từ 1985 thành 1983 click Navigator button Next để hiển thị record thứ nhì, kế click Navigator button Previous để hiển thị lại record bạn thấy field Year Published record thật thay đổi (updated) thành 1983.Điều nầy có nghĩa Data1 navigates từ record nầy đến record khác record nầy có thay đổi user edited, lưu trữ thay đổi trước di chuyển Chưa bạn muốn điều nầy, đó, bạn không muốn user tình cờ edit record bạn set property Locked textboxes thành True để user edit textboxes hình đây: Bạn tải chương trình tài tử nầy từ Datacontrol.zip Chỉ định vị trí Database lúc chạy chương trình Cách định tên DatabaseName giai đoạn thiết kế (at design time) ta dùng trước tiện lợi nguy hiểm, ta cài chương trình nầy lên computer khách, chưa file database nằm folder có tên Thí dụ computer database nằm folder E:\Program Files\Microsoft Visual Studio\VB98, computer khách database nằm folder C:\VB6\DataControl chẳng hạn Do đó, chương trình khởi động ta nên xác định lại vị trí database Giả dụ ta muốn để database folder với chương trình chạy, ta dùng property Path Application Object App sau: Dim AppFolder As String Private Sub Form_Load() ' Fetch Folder where this program EXE resides AppFolder = App.Path ' make sure it ends with a back slash If Right(AppFolder, 1) "\" Then AppFolder = AppFolder & "\" ' Assign Full path database filename to Data1 Data1.DatabaseName = AppFolder & "BIBLIO.MDB" End Sub Với cách code nói ta đảm bảo chương trình tìm thấy file database chỗ, không cần biết người ta cài chương trình bạn đâu hard disk computer khách.Nếu bạn học VB6 từ xa, nộp database cho giám thị VB6 mà bạn hardcode (viết chết cứng) vị trí file database lúc thiết kế giám thị (tutor) gặp khó khăn nầy chưa giám thị chứa database folder có tên harddisk bạn Thêm bớt Records Chương trình dùng tạm đựợc, không cho ta phương tiện để thêm (add), bớt (delete) records Bây bạn để vào Form buttons tên: cmdEdit, cmdNew, cmdDelete, cmdUpdate cmdCancel.Mặc dầu bạn không thấy, thật Control Data Data1 có property Recordset ta dùng Navigator buttons di chuyển từ record nầy đến record khác Recordset Ta nói đến Notation (cách viết) Data1.Recordset, lần muốn lấy Recordset từ database ta dùng method Refresh Data1.Recordset.Refresh.Lúc chuơng trình khởi động, user xem (browsing) records hai buttons Update Cancel không cần phải làm việc Do ta Lock (khóa) textboxes disable (làm cho bất lực) hai buttons nầy không cần dùng chúng.Trong Sub SetControls đây, ta dùng parameter gọi Editing với trị số False hay True tùy theo user Browse hay Edit, ta gọi Browse mode Edit mode Trong Edit mode, Textboxes unlocked (mở khóa) nút cmdNew, cmdDelete cmdEdit trở nên bất lực: Sub SetControls(ByVal Editing As Boolean) ' Lock/Unlock textboxes txtTitle.Locked = Not Editing txtYearPublished.Locked = Not Editing txtISBN.Locked = Not Editing txtPublisherID.Locked = Not Editing ' Enable/Disable buttons CmdUpdate.Enabled = Editing CmdCancel.Enabled = Editing CmdDelete.Enabled = Not Editing cmdNew.Enabled = Not Editing CmdEdit.Enabled = Not Editing End Sub Trong Browse mode, Form có dạng sau: Sub SetControls gọi Sub Form_Load chương trình khởi động Sub CmdEdit user click nút Edit sau: Private Sub Form_Load() ' Fetch Folder where this program EXE resides AppFolder = App.Path ' make sure it ends with a back slash If Right(AppFolder, 1) "\" Then AppFolder = AppFolder & "\" ' Assign Full path database filename to Data1 Data1.DatabaseName = AppFolder & "BIBLIO.MDB" ' Place controls in Browse Mode SetControls (False) End Sub Private Sub CmdEdit_Click() ' Place controls in Edit Mode SetControls (True) End Sub Khi ta Delete record recordset, vị trí record (current record) không thay đổi Do đó, sau delete record ta phải MoveNext Khổ nổi, ta vừa delete record cuối Recordset sau MoveNext, property EOF Recordset thành True Thành ta phải kiểm tra điều đó, lại phải MoveLast để hiển thị record cuối Recordset code Sub cmdDelete_Click đây: Private Sub CmdDelete_Click() On Error GoTo DeleteErr With Data1.Recordset ' Delete new record Delete ' Move to next record MoveNext If EOF Then MoveLast Exit Sub End With DeleteErr: MsgBox Err.Description Exit Sub End Sub Trong lúc code, ta Update (cập nhật hóa) record Recordset method Update Nhưng ta gọi method Update Recordset Recordset Edit hay AddNew mode Ta đặt Recordset vào Edit mode cách gọi method Edit Recordset, thí dụ Data1.Recordset.Edit Tương tự vậy, ta đặt Recordset vào AddNew mode cách gọi method AddNew Recordset, thí dụ Data1.Recordset.AddNew Private Sub cmdNew_Click() ' Place Recordset into Recordset AddNew mode Data1.Recordset.AddNew ' Place controls in Edit Mode SetControls (True) End Sub Sau Recordset gọi method Update Recordset khỏi AddNew hay Edit modes Ta tự thoát khỏi AddNew hay Edit modes, hay nói cho hủy bỏ pending (đang chờ đợi) Update cách gọi method CancelUpdate, thí dụ Data1.Recordset.CancelUpdate.Bạn tải chương trình nầy từ DataEdit.zip Dùng DataBound Combo Trong chương trình ta hiển thị lý lịch nhà xuất (PubID) Title, thêm chi tiết Phải chi chương trình lưu trữ PubID, hiển thị Company Name nhà xuất cho ta làm việc để khỏi phải nhớ số hay Ta thực điều cách dùng Control DBCombo (Data Bound Combo) Bạn dùng IDE Menu Command Project | Components để chọn Microsoft Data Bound List Controls 6.0 click Apply Kế đó, thêm DBCombo tên DBCombo1 vào Form Vì ta cần Recordset khác để cung cấp Table Publisher cho DBCombo1, nên bạn thêm control Data thứ nhì tên Data2 vào Form Cho Data2, set property DatabaseName thành E:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB property RecordSource thành Publishers Để không cho người ta thấy hình Data2 lúc run-time, bạn set property Visible thành False Cái mục đích dùng DBCombo1 hiển thị Company Name nhà xuất bản, đằng sau lưng thay đổi, tức ta làm việc với PubID cho record Title Data1 Khi user click lên DBCombo1 để chọn nhà xuất bản, ta theo Company Name mà chứa PubID tương ứng record Title Data1 Do có nhiều thứ ta phải đặt cho DBCombo1 sau: Property Value Chú thích RowSource Data2 Đây datasource DBCombo1 Nó cung cấp table Publishers Listfield Company Name Khi RowSource phía chọn rồi, Combo property Listfield nầy hiển thị fields table Publishers Company Name field RowSource mà ta muốn hiển thị DBCombo1 DataSource Data1 Đây datasource record mà ta muốn edit, tức record table Titles Datafield PubID Field (của record Title) thay đổi PubID Field RowSource (table Publishers) tương ứng với item user chọn DBCombo1 (Company Name) BoundColumn Khi Edit mode user chọn Company Name khác DBCombo1 click nút Update bạn thấy Textbox txtPublisherID đổi theo hiển thị số lý lịch PubID Nếu trước Update bạn muốn thấy PubID hiển thị Textbox txtPublisherID bạn dùng Event Click DBCombo1 sau: Private Sub DBCombo1_Click(Area As Integer) ' Display new PuBID txtPublisherID.Text = DBCombo1.BoundText End Sub Property BoundText DBCombo1 trị số BoundColumn mà ta truy cập (viết hay đọc) Thí dụ bạn muốn thêm record Title default PubID 324, tức Company Name= "GLOBAL ENGINEERING" Bạn assign trị số 324 vào property BoundText DBCombo1 Sub cmdNew_Click sau: Private Sub cmdNew_Click() ' Place Recordset into Recordset AddNew mode Data1.Recordset.AddNew ' Default Publisher is "GLOBAL ENGINEERING", i.e PubID=324 DBCombo1.BoundText = 324 ' Place controls in Edit Mode SetControls (True) End Sub Bạn tải chương trình nầy từ DataBound.zip.Trong tới ta học thêm cách coding để dùng Recordset kỹ thuật DAO Học Microsoft Visual Basic 6.0 Vovisoft © 2000 All rights reserved Last Updated: May 2002 Webmaster ... tải chương trình nầy từ DataBound.zip.Trong tới ta học thêm cách coding để dùng Recordset kỹ thuật DAO Học Microsoft Visual Basic 6. 0 Vovisoft © 200 0 All rights reserved Last Updated: May 200 2... nói ta đảm bảo chương trình tìm thấy file database chỗ, không cần biết người ta cài chương trình bạn đâu hard disk computer khách.Nếu bạn học VB6 từ xa, nộp database cho giám thị VB6 mà bạn hardcode... Data1.Recordset.CancelUpdate.Bạn tải chương trình nầy từ DataEdit.zip Dùng DataBound Combo Trong chương trình ta hiển thị lý lịch nhà xuất (PubID) Title, thêm chi tiết Phải chi chương trình lưu trữ PubID,

Ngày đăng: 04/12/2015, 02:59

Từ khóa liên quan

Mục lục

  • Khóa Hàm Thụ Visual Basic 6.0

    • Chương Mười Bốn - Dùng Control Data

      • Control Data

      • Chỉ định vị trí Database lúc chạy chương trình

      • Thêm bớt các Records

      • Dùng DataBound Combo

Tài liệu cùng người dùng

Tài liệu liên quan