完成了:
'Copyright 2005.10 by j13583@www.office-cn.net
'联系:j13583@sohu.com
Dim CT As Integer 'ControlType
Function SetCtlDefaultValue(ctl As Control, Optional GetOrSet As Boolean = True) As Boolean
On Error Goto Er
Dim ctlStr As String '连接符
Dim ctlValue As String '值
ctlValue = Nz(ctl, "")
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * FROM ctlDefaultValue " & _
"WHERE [ctlForm]=""" & ctl.Parent.Name & """ AND [ctlName]=""" & ctl.Properties("name") & """;", DB_OPEN_DYNASET)
'中间部分代码省略
ToGetDefaultValue:
If ctl.Properties("ControlSource") = "" Then
CT = VarType(ctlValue)
Else
CT = VarType(ctl)
End If
Select Case CT
Case vbString
If InStr(1, ctlValue, """") Then
ctlStr = "'"
Else
ctlStr = """"
End If
Case vbDate
ctlStr = "#"
Case Else
ctlStr = ""
End Select
ctl.Properties("DefaultValue") = ctlStr & ctlValue & ctlStr
rst.Close
Ex: Exit Function
Er:
MsgBox Error$
Resume Ex
End Function
'遍历所有控件
Function FormCtlScan(Frm As Form) As Boolean
On Error GoTo Er
Dim FrmCtl As Control
For Each FrmCtl In Frm.Controls
CT = FrmCtl.Properties("ControlType")
If CT = acCheckBox Or CT = acComboBox Or CT = acListBox Or CT = acOptionGroup Or CT = acTextBox Or CT = acToggleButton Then
SetCtlDefaultValue FrmCtl
End If
Next
Ex: Exit Function
Er:
MsgBox Error$
Resume Ex
End Function
'如果要遍历窗体所有控件,在[打开]事件设置 =FormCtlScan([Form])
'如果要完整源码,请与我联系
[此贴子已经被作者于2005-10-26 17:26:05编辑过]
|