1、"&"置换"+"
2、变量命名大小写,语句错落有秩,源代码维护方面
3、请养成以下的「物件命名约定」良好习惯
4、在简单的选取条件情况下,使用IIf()函数
5、尽量使用Debug.Print进行除错
6、在重复对某一对象的属性进行修改时,尽量使用With....End With
7、MsgBox中尽量使用消息图示,这样程式比较有规范
8、在i能的情况下使用枚举
1、"&"置换"+"
在很多人的写程式语系中,用「+」来连线字串,这样容易导致歧义。良好的习惯是用「&」来连线字串.
不正确:
Dim sMessage As String
sMessage = "1" + "2"
正确:
Dim sMessage As String
sMessage = "1" & "2"
注意:"&"的后面有个空格
2、变量命名大小写,语句错落有秩,源代码维护方面
下面大家比较一下以下两段代码:
读懂难度很大的代码:
Dim SNAME As String
Dim NTURN As Integer
If NTURN = 0 Then
If SNAME = "vbeden" Then
Do While NTURN < 4
NTURN = NTURN + 1
Loop
End If
End If
容易读懂的代码:
Dim sName As String
Dim nTurn As Integer
If nTurn = 0 Then
If sName = "vbeden" Then
Do While nTurn < 4
nTurn = nTurn + 1
Loop
End If
End If
[返回索引]
3、请养成以下的「物件命名约定」良好习惯
推荐使用的控件前缀
控件类型 前缀 例子
3D Panel pnl pnlGroup
ADO Data ado adoBiblio
Animated button ani aniMailBox
Check box chk chkReadOnly
Combo box, drop-down list box cbo cboEnglish
Command button cmd cmdExit
Common dialog dlg dlgFileOpen
Communications com comFax
Control (当特定类型不详时,在过程中所使用的) ctr ctrCurrent
Data dat datBiblio
Data-bound combo box dbcbo dbcboLanguage
Data-bound grid dbgrd dbgrdQueryResult
Data-bound list box dblst dblstJobType
Data combo dbc dbcAuthor
Data grid dgd dgdTitles
Data list dbl dblPublisher
Data repeater drp drpLocation
Date picker dtp dtpPublished
Directory list box dir dirSource
Drive list box drv drvTarget
File list box fil filSource
Flat scroll bar fsb fsbMove
Form frm frmEntry
Frame fra fraLanguage
Gauge gau gauStatus
Graph gra graRevenue
Grid grd grdPrices
Hierarchical flexgrid flex flexOrders
Horizontal scroll bar hsb hsbVolume
Image img imgIcon
Image combo imgcbo imgcboProduct
ImageList ils ilsAllIcons
Label lbl lblHelpMessage
Lightweight check box lwchk lwchkArchive
Lightweight combo box lwcbo lwcboGerman
Lightweight command button lwcmd lwcmdRemove
Lightweight frame lwfra lwfraSaveOptions
Lightweight horizontal scroll bar lwhsb lwhsbVolume
Lightweight list box lwlst lwlstCostCenters
Lightweight option button lwopt lwoptIncomeLevel
Lightweight text box lwtxt lwoptStreet
Lightweight vertical scroll bar lwvsb lwvsbYear
Line lin linVertical
List box lst lstPolicyCodes
ListView lvw lvwHeadings
MAPI message mpm mpmSentMessage
MAPI session mps mpsSession
MCI mci mciVideo
Menu mnu mnuFileOpen
Month view mvw mvwPeriod
MS Chart ch chSalesbyRegion
MS Flex grid msg msgClients
MS Tab mst mstFirst
OLE container ole oleWorksheet
Option button opt optGender
Picture box pic picVGA
Picture clip clp clpToolbar
ProgressBar prg prgLoadFile
Remote Data rd rdTitles
RichTextBox rtf rtfReport
Shape shp shpCircle
Slider sld sldScale
Spin spn spnPages
StatusBar sta staDateTime
SysInfo sys sysMonitor
TabStrip tab tabOptions
Text box txt txtLastName
Timer tmr tmrAlarm
Toolbar tlb tlbActions
TreeView tre treOrganization
UpDown upd updDirection
Vertical scroll bar vsb vsbRate
--------------------------------------------------------------------------------
推荐使用的资料访问物件 (DAO) 的前缀
用下列前缀来指示资料访问物件
资料库物件 前缀 例子
Container con conReports
Database db dbAccounts
DBEngine dbe dbeJet
Document doc docSalesReport
Field fld fldAddress
Group grp grpFinance
Index ix idxAge
Parameter prm prmJobCode
QueryDef qry qrySalesByRe ..
访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容