|
<>总觉得ACCESS-VBA中少了这么一个函数,刚才既时写了一个请大家提出意见:<br>'--------------------------------------------------------------------------------------------------------------<br>'名称: 域更新函数<br>'功能: 同Dlookup等域函数<br>'参数:<br>' 同Dlookup等域函数,但Expr参数有点像Switch,以便更新多少字段.<br>' cn参数可以用来?行事??理<br>'用法:<br>' 1.先决条件:假如有表[BMB],且含有字段luoma,Int,ddate,check,mony<br>' 2.debug.Print DSet("luoma,aa,Int,3455,ddate,2009-9-9,check,-1,mony,5000 ","BMB","id=0")<br>'返回: True表示成功,False表示未成功或没有更新<br>'日期: 2007-07-26<br>'改正: 2007-07-26<br>'版本: 1.01<br>'作者: zhengjialon<br>'--------------------------------------------------------------------------------------------------------------<br>ublic Function DSet(Expr As String, Domain As String, Optional Criteria As String, Optional cn As ADODB.Connection) As Boolean<br>On Error GoTo lblErr:<br>Dim i As Integer, l As Integer<br>Dim ExprS As Variant<br>Dim rst As New ADODB.Recordset<br> <br> ExprS = Split(Expr, ",")<br> l = UBound(ExprS)<br> <br> rst.Open "Select " & "*" & " From " & Domain & IIf(Trim(Criteria) <> "", " Where " & Criteria, ""), CurrentProject.Connection, adOpenStatic, adLockOptimistic, adCmdText<br> <br> If Not rst.EOF Then<br> For i = 0 To l - 1 Step 2<br> Select Case rst(ExprS(i)).Type<br> Case adChar 'String<br> rst(ExprS(i)) = IIf(ExprS(i + 1) = "", Null, ExprS(i + 1))<br> Case adBoolean '<br> rst(ExprS(i)) = IIf(Trim(ExprS(i + 1)) = "" Or Trim(ExprS(i + 1)) = False Or Trim(ExprS(i + 1)) = 0, False, True)<br> Case Else<br> rst(ExprS(i)) = IIf(Trim(ExprS(i + 1)) = "", Null, ExprS(i + 1))<br> End Select<br> Next i<br> rst.Update<br> DSet = True<br> End If<br> <br> rst.Close<br> Set rst = Nothing<br> <br>lblExit:<br> Exit Function<br>lblErr:<br> If Err.Number = 3265 Then 'FieldName Is Noting<br> MsgBox "FieldName: [" & ExprS(i) & "] Is Noting ! ", vbCritical<br> ElseIf Err.Number = -2147352571 Then 'FieldValue <> FieldType Tip<br> MsgBox "FieldValue: [" & ExprS(i + 1) & "] Is Error ! ", vbCritical<br> Else 'ohter error<br> MsgBox Err.Number & Err.Description<br> End If<br>End Function<br>'增加了更多容错处理</P>
[此贴子已经被作者于2007-7-26 15:05:36编辑过]
|
|