Private Sub Com单位名称_NotInList(NewData As String, Response As Integer)
On Error GoTo catch
Dim result
If MsgBox("单位不在列表中,要把这个新单位加进数据库中吗?", vbOKCancel + vbQuestion, "提示") = vbOK Then
Response = acDataErrAdded
DoCmd.RunSQL "INSERT INTO [单位] ([单位名称]) VALUES ('" & NewData & "')"
End If
result = DLookup("[单位ID]", "[单位]", "[单位名称]='" & NewData & "'")
If IsNull(result) Then
Response = acDataErrContinue
MsgBox "添加新单位没有成功,请重试。", vbCritical, "添加失败"
[单位ID].Undo
Else
Response = acDataErrAdded
End If
finally:
Exit Sub
catch:
MsgBox Err.Number & vbNewLine & Err.Description
Resume finally
End Sub作者: 阿罗 时间: 2003-12-2 06:40
[attach]2652[/attach]作者: LYP6160 时间: 2003-12-2 07:00
啊罗:代码中还有不足的地方,就是不想增加某个单位的时候按“退出“键不能正确退出,希望加点“料“进去,呵呵。作者: 阿罗 时间: 2003-12-2 07:20
是的,有一个错误。第二个if..then..else中[单位id].undo应该改为控件名称
Private Sub Com单位名称_NotInList(NewData As String, Response As Integer)
On Error GoTo catch
Dim result
If MsgBox("单位不在列表中,要把这个新单位加进数据库中吗?", vbOKCancel + vbQuestion, "提示") = vbOK Then
Response = acDataErrAdded
DoCmd.RunSQL "INSERT INTO [单位] ([单位名称]) VALUES ('" & NewData & "')"
End If
result = DLookup("[单位ID]", "[单位]", "[单位名称]='" & NewData & "'")
If IsNull(result) Then
Response = acDataErrContinue
MsgBox "添加新单位没有成功,请重试。", vbCritical, "添加失败"
[Com单位名称].Undo
Else
Response = acDataErrAdded
End If
finally:
Exit Sub
catch:
MsgBox Err.Number & vbNewLine & Err.Description
Resume finally
End Sub作者: 阿罗 时间: 2003-12-2 07:35
也可在第一个if..中添加
else
[Com单位名称].Undo
exit sub
end if
反正原理如此,具体用法自行解决吧。 作者: LYP6160 时间: 2003-12-2 07:38
呵呵,你换得真快作者: 阿罗 时间: 2003-12-3 07:01
再次修改
Private Sub Com单位名称_NotInList(NewData As String, Response As Integer)
On Error GoTo catch
If MsgBox("单位不在列表中,要把这个新单位加进数据库中吗?", vbOKCancel + vbQuestion, "提示") = vbOK Then
DoCmd.RunSQL "INSERT INTO [单位] ([单位名称]) VALUES ('" & NewData & "')"
If Nz(DLookup("[单位ID]", "[单位]", "[单位名称]='" & NewData & "'"), 0) Then
Response = acDataErrAdded
Else
Response = acDataErrContinue
MsgBox "添加新单位没有成功,请重试。", vbCritical, "添加失败"
[Com单位名称].Undo
End If
End If
finally:
Exit Sub
catch:
MsgBox Err.Number & vbNewLine & Err.Description
Resume finally
End Sub 作者: 旺 时间: 2003-12-5 03:13
太感谢阿罗和LYP6160了,我先试试,还得麻烦各位呢。
再次表示感谢!