?? vb13.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.pconline.com.cn/experience/subject/VB/vb13.htm -->
<HTML><HEAD><TITLE>|><| 太平洋電腦信息網</TITLE><!-- #BeginTemplate "/Templates/0723.dwt" --><!-- #BeginEditable "doctitle" --><!-- #EndEditable -->
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="vb13.files/text.css" rel=stylesheet>
<STYLE type=text/css>.sfont {
LINE-HEIGHT: 18pt
}
</STYLE>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD vAlign=top width="100%">
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR bgColor=#303880>
<TD><A href="http://www.pconline.com.cn/"><IMG alt=太平洋電腦信息網 border=0
height=18 hspace=30 src="vb13.files/txtpconline.gif"
width=96></A><IMG height=10 src="vb13.files/blank.gif"
width=400></TD></TR></TBODY></TABLE></TD>
<TD align=right background=vb13.files/bw.gif noWrap width=130><IMG
height=26 src="vb13.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/sitemap/"><IMG alt=網站地圖 border=0
height=26 src="vb13.files/sitemap.gif" width=21></A><IMG height=26
src="vb13.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/"><IMG alt=關于太平洋 border=0 height=26
src="vb13.files/aboutus.gif" width=21></A><IMG height=26
src="vb13.files/bw.gif" width=10><A
href="http://www.pconline.com.cn/tpy/tariff.html"><IMG alt=與我們聯系 border=0
height=26 src="vb13.files/contact.gif" width=21></A><IMG height=26
src="vb13.files/bw.gif" width=10></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD> </TD>
<TD colSpan=2 noWrap><A
href="http://www.pconline.com.cn/experience/index.html"><IMG
align=absBottom alt=企業之窗 border=0 height=15
src="vb13.files/txtexperience.gif" width=68></A> <A
href="http://www.pconline.com.cn/">首 頁</A> <A
href="http://www.pconline.com.cn/market/">今日報價</A> <A
href="http://www.pconline.com.cn/recommend/">產品推介</A> <A
href="http://www.pconline.com.cn/yp/">企業之窗</A> <A
href="http://www.pconline.com.cn/news/">科技新聞</A> <A
href="http://www.pconline.com.cn/product/">產品查詢</A> <A
href="http://www.pconline.com.cn/download/">軟件下載</A></TD></TR>
<TR>
<TD><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
<TD bgColor=#ffa000 colSpan=2><IMG height=1 src="vb13.files/blank.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD bgColor=#cdcdcd><IMG height=5 src="vb13.files/blank.gif" width=1></TD>
<TD align=right colSpan=3><IMG height=6 src="vb13.files/ffa000.gif"
width=1></TD></TR>
<TR>
<TD bgColor=#666666><IMG height=1 src="vb13.files/blank.gif" width=1></TD>
<TD align=right><A href="http://www.pconline.com.cn/"><IMG alt=太平洋電腦信息網
border=0 height=35 src="vb13.files/pconline.com.cn.gif" vspace=10
width=162></A></TD>
<TD> </TD>
<TD vAlign=bottom><!-- #BeginEditable "important_news" --><IMG height=40
src="vb13.files/blank.gif" width=350><!-- #EndEditable --></TD>
<TD align=right vAlign=top><!-- #BeginEditable "adv_top" --><IMG alt=廣告位
height=43 src="vb13.files/icon.gif"
width=190><!-- #EndEditable --></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif"
width=1></TD>
<TD vAlign=top width=170><!-- #BeginEditable "addons" --><!--替代為附加信息 --><!-- #EndEditable --></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif"
width=1></TD>
<TD><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
<TD colSpan=3 vAlign=top width=566><!-- #BeginEditable "1" --><IMG
height=1 src="vb13.files/blank.gif" width=566><BR><IMG height=1
src="vb13.files/666666.gif" width=420><BR><IMG height=13
src="vb13.files/blank.gif" width=1><A
href="http://www.pconline.com.cn/experience/">經驗共享</A> > <SPAN
class=greenb><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">專題教程</A></SPAN><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">
VB專區</A>> <BR><IMG height=1 src="vb13.files/666666.gif" vspace=1
width=420><BR>
<P align=center><B><FONT color=#000066><SPAN
class=sfont>利用VB動態設計多“主細表”應用</SPAN></FONT></B><SPAN
class=sfont><BR></SPAN></P>
<P align=center><SPAN class=sfont>方云波 </SPAN></P>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
在開發人事管理軟件的工程中,當我們建立一個職工檔案記錄時,通常都會遇到設計職工履歷表、職工家屬情況的問題,我這里介紹一種用“主細表”方法的開發思路。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>一. 數據庫的建立</B>
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
一個設計合理的數據庫就是管理軟件成功的一半。因此我們在開發之初就應該先慎重設計好數據庫的結構。筆者在開發人事管理軟件時建立一個名為:HR.mdb的數據庫,該庫包括下面三個表:
</SPAN><PRE><SPAN class=sfont>
職工的自然情況一般是下面這些內容的:(Employee)
職工姓名 性別 出生日期 民族 政治面貌 婚否……
職工履歷表的內容一般是下面這些內容的:(Vita)
起始時間 終止時間 所在單位及部門 任職情況……
職工家屬情況一般是下面這些內容的:(Relative)
親人姓名 與本人關系 所在單位及部門 任職情況……
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 很顯然,Employee
與Vita和Relative表之間是“一對多”的關系。即一條Employee記錄對應多條Vita和Relative記錄,亦即Employee為主表,Vita和Relative為細表。由于在設計ACCESS數據庫時每個表必須要有一個主關鍵字(Primary
Key),因此筆者設計了一個“職工ID”字段作為三個表相互連接的字段,同時也作為Employee表的主關鍵字。而另外兩個表的主關鍵字就采用自動編號(AutoNum)字段了。
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> <B>二. 建立表之間的連接</B>
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
數據庫設計好之后就可以著手設計各種應用,但程序設計中關鍵的問題是如何建立“主細表”之間的連接。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 建立“主細表”之間的連接有兩種方法:
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 1、
利用VB的數據窗體向導生成“主細表”的連接。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
根據向導,可以生成一個主表和一個細表的連接。但是這種方法生成的連接有它的缺陷。首先是它只能生成兩個表的連接,對我們上面所說的情況就無能為力了;其次是開發者在為向導提供數據庫時,系統把數據庫的絕對路徑加入了DATA控件,非常不利于程序的分發。
下面介紹筆者使用的一種方法。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT> 2、 動態創建“主細表”的連接
</SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
新建一個Form,然后添加三個DATA控件和三個DBGRID控件。三個DATA控件的名稱分別為:datPrimaryRS,datSecondaryRS,DatThirdRS;三個DBGRID控件的名稱分別為:grdPrimaryRS,grdSecondaryRS,grdDatThirdRS。三個DATA控件Form上既沒有指定DatabaseName,也沒有指定RecordSource,只是在Form調用時才動態賦值。三個DBGRID控件倒是指定了所對應的DATA控件。下面是Form_Load()函數:
</SPAN><PRE><SPAN class=sfont>
Private Sub Form_Load()
datPrimaryRS.DatabaseName = App.path & “\HR.mdb”
datPrimaryRS.RecordSource =
"select * from [Employee] Order by [職工 ID]"
datSecondaryRS.DatabaseName = App.path & “\HR.mdb
datThirdRS.DatabaseName = App.path & “\HR.mdb
'創建主表網格記錄集
datPrimaryRS.Refresh
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
這里只是創建和顯示了主表的數據,那么兩個細表的數據如何創建和顯示呢?從主細表的概念我們知道,我們查看主表的任何一條記錄,細表必須同時顯示出對應的多條記錄。因此我們必須對主表的任何一條記錄成為當前記錄時觸發的事件編碼。而該事件就是Reposition。
</SPAN><PRE><SPAN class=sfont>
Private Sub datPrimaryRS_Reposition()
Screen.MousePointer = vbDefault
On Error Resume Next
datSecondaryRS.Refresh
DatThirdRS.Refresh
'使網格與主表記錄集同步
datSecondaryRS.RecordSource = "select [職工ID],
[起始日期],[終止日期],[所在單位及部門],[任職情況]
from [Vita] where [職工ID]=" & datPrimaryRS.Recordset!
[職工ID] & " Order by [起始日期]"
DatThirdRS.RecordSource = "select [職工ID],[親人姓名],
[關系],[所在單位及部門],[任職情況]
from [Relative] where [職工ID]=" &
datPrimaryRS.Recordset![職工ID] & ""
datSecondaryRS.Refresh
DatThirdRS.Refresh
'為 dynasets 和快照顯示當前記錄位置
datPrimaryRS.Caption = "當前記錄: " &
(datPrimaryRS.Recordset.AbsolutePosition + 1)
End Sub
</SPAN></PRE>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
很顯然,如果有三個、四個…等多個細表也可以如法炮制。 </SPAN>
<P><SPAN class=sfont><FONT color=#ffffff>----</FONT>
至此,一個靈活的,表數不限的主細表連接就建立起來了。只要將三個DBGRID的AllowAddNew、AllowDelete和AllowUpdate三個屬性設置為TRUE,那么就可以對這些主細表進行各種數據庫表作了。</SPAN>
<P align=right><A
href="http://www.pconline.com.cn/experience/subject/VB/index.html">----更多的文章</A>
<!-- #EndEditable --></P></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=760>
<TBODY>
<TR>
<TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif"
width=1></TD>
<TD align=right vAlign=bottom width=170><IMG height=10
src="vb13.files/blank.gif" width=170><BR></TD>
<TD bgColor=#666666 width=1><IMG height=1 src="vb13.files/blank.gif"
width=1></TD>
<TD width=21><IMG height=1 src="vb13.files/blank.gif" width=21></TD>
<TD bgColor=#eeeeee width=566><IMG height=10 src="vb13.files/blank.gif"
width=566><BR>版權所有©1999 太平洋電腦信息網<BR><FONT
face="Arial, Helvetica, sans-serif"><A
href="mailto:exp@pconline.com.cn">exp@pconline.com.cn</A></FONT> <IMG
height=1 src="vb13.files/hhvisit.html" width=1> </TD></TR></TBODY></TABLE><!-- #EndTemplate --></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -