设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 6107|回复: 28
打印 上一主题 下一主题

[模块/函数] 帮忙看看这段新建记录代码有没有问题

[复制链接]
1#
发表于 2012-6-12 19:29:41 | 显示全部楼层
If DCount("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'") <> 0 ThenDCount会降低效率,建议使用dlookup来检查
,其他问题你都没说,如果运行正常,就算了吧,等基础好了再来检查,毕竟每个人都编写习惯都不一样
2#
发表于 2012-6-12 20:47:55 | 显示全部楼层
        fm.FilterOn = True
        fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
调换一下顺序看看
fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
fm.FilterOn = True
==========
我觉得你的代码效率不高,可能这样要好点:
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
3#
发表于 2012-6-12 20:48:07 | 显示全部楼层
       fm.FilterOn = True
        fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
调换一下顺序看看
fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
fm.FilterOn = True
==========
我觉得你的代码效率不高,可能这样要好点:
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
4#
发表于 2012-6-12 20:50:04 | 显示全部楼层
回复需要审核?
5#
发表于 2012-6-12 20:57:43 | 显示全部楼层
Public coTBuyInvoice As New Collection

Public Sub NewTBuyInvoice()
       fm.FilterOn = True
        fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
调换一下顺序看看
fm.Filter = "Me.InvoiceId='" & mId & "'"   ‘过滤
fm.FilterOn = True
==========
我觉得你的代码效率不高,可能这样要好点:
Public Sub NewTBuyInvoice()
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
end sub
6#
发表于 2012-6-12 22:48:58 | 显示全部楼层
我觉得你的代码效率不高,可能这样要好点:
Public Sub NewTBuyInvoice()
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
end sub
7#
发表于 2012-6-12 22:49:36 | 显示全部楼层
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
8#
发表于 2012-6-12 22:49:52 | 显示全部楼层
On Error GoTo NewTBuyInvoice_Err
Dim mId As String
mId = InputBox("请输入发票号", "提示")
If mId = "" Then Exit Sub

If Nz(DLookup("InvoiceId", "tbTBuyInvoice", "InvoiceId='" & mId & "'"), "") = mId Then
    MsgBox "此发票号已经存在,请重新输入!", vbExclamation, "提示"
    Exit Sub
End If

'新建发票
'====================
Dim StrSql As String '不清楚你的字段类型,'UserName'为文本, #BuildDate#为日期
StrSql = "insert into tbTBuyInvoice(InvoiceId,BuildDate,BuildMan) "
StrSql = StrSql & " select '" & mId & "', #" & BuildDate & "#, '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

CurrentProject.Connection.Execute (StrSql)



'打开新记录编辑
        Dim fm As Form
        Set fm = New Form_fmTBuyInvoice  '打开新窗口
        fm.RecordSource = "select * from tbTBuyInvoice where InvoiceId='" & mId & "'"
        fm.AllowAdditions = False  '表头不可编辑
        fm.Caption = "编辑发票" & mId
        fm.Visible = True
        coTBuyInvoice.Add Item:=fm, Key:=CStr(fm.Hwnd)
        Set fm = Nothing
NewTBuyInvoice_Exit:
    Exit Sub
NewTBuyInvoice_Err:
    MsgBox Err.Description
    Resume NewTBuyInvoice_Exit
9#
发表于 2012-6-15 21:34:13 | 显示全部楼层
你先把( fm.AllowAdditions = False  ‘表头不可编辑)删了,看看是什么情况
然后看看以V开头的表头数据是否已经已经写入表中,如果没有写入的话,那你看看是不是字段类型不对还是字符长度不够。
==========
这些问题要自己多调试调试,看看究竟出问题在哪行代码
10#
发表于 2012-6-16 10:17:43 | 显示全部楼层
原来是ADP啊
StrSql = StrSql & " select '" & mId & "', '" & BuildDate & "', '" & DLookup("EmployeeId", "tbEmployee", "UserName='" & mUserName & "'") & "'"

日期要把#改为'
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-21 17:07 , Processed in 0.117550 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表