注冊 登錄
Office中國論壇/Access中國論壇 返回首頁

的個人空間 http://www.mzhfr.cn/?0 [收藏] [復(fù)制] [分享] [RSS]

日志

[原創(chuàng)]域更新函數(shù)

已有 2448 次閱讀2007-7-26 22:48 |個人分類:個人資料

總覺得ACCESS-VBA中少了這么一個函數(shù),剛才既時寫了一個請大家提出意見:
'--------------------------------------------------------------------------------------------------------------
'名稱: 域更新函數(shù)
'功能: 同Dlookup等域函數(shù)
'參數(shù):
' 同Dlookup等域函數(shù),但Expr參數(shù)有點像Switch,以便更新多少字段.
' cn參數(shù)可以用來?行事??理
'用法:
' 1.先決條件:假如有表[BMB],且含有字段luoma,Int,ddate,check,mony
' 2.debug.Print DSet("luoma,aa,Int,3455,ddate,2009-9-9,check,-1,mony,5000 ","BMB","id=0")
'返回: True表示成功,False表示未成功或沒有更新
'日期: 2007-07-26
'改正: 2007-07-26
'版本: 1.01
'作者: zhengjialon
'--------------------------------------------------------------------------------------------------------------
Public Function DSet(Expr As String, Domain As String, Optional Criteria As String, Optional cn As ADODB.Connection) As Boolean
On Error GoTo lblErr:
Dim i As Integer, l As Integer
Dim ExprS As Variant
Dim rst As New ADODB.Recordset

ExprS = Split(Expr, ",")
l = UBound(ExprS)

rst.Open "Select " & "*" & " From " & Domain & IIf(Trim(Criteria) <> "", " Where " & Criteria, ""), CurrentProject.Connection, adOpenStatic, adLockOptimistic, adCmdText

If Not rst.EOF Then
For i = 0 To l - 1 Step 2
Select Case rst(ExprS(i)).Type
Case adChar 'String
rst(ExprS(i)) = IIf(ExprS(i + 1) = "", Null, ExprS(i + 1))
Case adBoolean '
rst(ExprS(i)) = IIf(Trim(ExprS(i + 1)) = "" Or Trim(ExprS(i + 1)) = False Or Trim(ExprS(i + 1)) = 0, False, True)
Case Else
rst(ExprS(i)) = IIf(Trim(ExprS(i + 1)) = "", Null, ExprS(i + 1))
End Select
Next i
rst.Update
DSet = True
End If

rst.Close
Set rst = Nothing

lblExit:
Exit Function
lblErr:
If Err.Number = 3265 Then 'FieldName Is Noting
MsgBox "FieldName: [" & ExprS(i) & "] Is Noting ! ", vbCritical
ElseIf Err.Number = -2147352571 Then 'FieldValue <> FieldType Tip
MsgBox "FieldValue: [" & ExprS(i + 1) & "] Is Error ! ", vbCritical
Else 'ohter error
MsgBox Err.Number & Err.Description
End If
End Function
'增加了更多容錯處理

[此貼子已經(jīng)被作者于2007-7-26 15:05:36編輯過]

評論 (0 個評論)

facelist doodle 涂鴉板

您需要登錄后才可以評論 登錄 | 注冊

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-17 00:35 , Processed in 0.052852 second(s), 14 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部