我要往dbf文件中添加记录,但屡次试验都不行,本人运行一下代码没有出现错误提示,但dbf中没存到数据,不晓得是何原因?恳请各位大虾帮忙帮忙!
本人使用的代码如下:
Dim iconc As String
Dim CWPzh As String
Dim tag As Integer
tag = 0
iconc = "Provider=MSDASQL.1;Persist Security Info=false;Extended Properties=Driver={Microsoft Visual FoxPro Driver};UID=;SourceDB=" & Module.CaiWuPath & ";SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;"Conn2.CursorLocation = adUseClient
Conn2.Open iconcrs.CursorLocation = adUseClientIf rs.State = adStateOpen Then rs.Close
rs.Open "select max(Pzh) as Pzh from L-PZT" & right$(Ds_Year, 2) & "", Conn2, adOpenKeyset, adLockBatchOptimistic  'L-PZT??是dbf文件名 打开凭证头表,获取凭证号
If rs.RecordCount = 0 Then
    CWPzh = 1
ElseIf Trim("" & rs.Fields("Pzh")) = "" Then         '获取凭证号
    CWPzh = 1
Else
    CWPzh = CStr(CLng(rs.Fields("Pzh")) + 1)
End IfConn2.BeginTrans
tag = 1If rs.State = adStateOpen Then rs.Close
rs.Open "select *  from L-PZT" & right$(Ds_Year, 2) & "", Conn2, adOpenKeyset, adLockBatchOptimistic 'L-PZT??是dbf文件名 打开凭证头表
rs.AddNew                                      '往财务系统中添加凭证头
With rs
    .Fields("Pzh") = CWPzh
    .Fields("Pzmc") = "" & DataEnv.rsComUse.Fields("PZLX")
    .Fields("Pzrq") = Date
    .Fields("Sr") = User_Name
    .Fields("Zynr") = left$("" & DataEnv.rsComUse.Fields("ZY"), 30)
    .Fields("Made") = "帐务"
    .Fields("Lsh") = Trim("" & DataEnv.rsComUse.Fields("LSH"))
    .Update
End WithIf rs.State = adStateOpen Then rs.Close
rs.Open "select *  from L-PZ" & right$(Ds_Year, 2) & "", Conn2, adOpenKeyset, adLockBatchOptimistic  'L-PZT??是dbf文件名 打开凭证头表
If Mrc.State = adStateOpen Then Mrc.Close
sql = "select * from CWPZ where LSH='" & Trim(DataEnv.rsComUse.Fields("LSH")) & "' AND BS='" & TermMark & "'"
Mrc.Open sql, DataEnv.Conn
While Mrc.EOF = False
    rs.AddNew                                 '往财务系统中添加凭证内容
    With rs
        .Fields("Pzh") = CWPzh
        .Fields("Pzmc") = Mrc.Fields("PZLX")
        .Fields("Pzrq") = Date
        .Fields("Sr") = User_Name
        .Fields("Zy") = left$("" & Mrc.Fields("ZY"), 30)
        .Fields("Made") = "帐务"
        .Fields("Lsh") = Trim(Mrc.Fields("LSH"))
        .Fields("Kmdm") = Mrc.Fields("KMDM")
        If Trim("" & Mrc.Fields("RMBJ")) <> "" Then .Fields("Rmbj") = CDbl(Mrc.Fields("RMBJ"))
        If Trim("" & Mrc.Fields("RMBD")) <> "" Then .Fields("Rmbd") = CDbl(Mrc.Fields("RMBD"))
        .Update
    End With
    Mrc.MoveNext
Wend
Conn2.CommitTrans
tag = 0If rs.State = adStateOpen Then rs.Close
Conn2.CloseExit Sub
err1:
If tag = 1 Then
    Conn2.RollbackTrans
End If
MsgBox err.Description, vbCritical, "错误"

解决方案 »

  1.   

    楼主的代码好繁啊,建议再进行数据更新、添加等操作的时候直接使用SQL语言来操作,比较简单也不容易出错。删除数据
    cnn.Execute "delete from disk_inf where id=" & Val(txtValue0(0).Text)修改数据
    cnn.Execute "update disk_inf set " & _
                "movie_name='" & Trim(txtValue0(3).Text) & "'," & _
                " where major_id='" & Trim(txtValue0(1).Text) & "'"添加数据
    cnn.Execute "insert into disk_classify values(1,'VCD')"这样子比较简单了啦,出错了也比较容易调试。
      

  2.   

    另外:如果dbf文件中有字段是日期型,应该怎么存储呢?
    cnn.Execute "insert into disk_classify(FDate) values("& Date &")"
    我按照上面的语句来做,当说类型不匹配。
      

  3.   

    日期型在ado里应该是要加#号的,
    应该写成
    cnn.Execute "insert into disk_classify(FDate) values(#"& format(Date,"yyyy-mm-dd") &"#)"
      

  4.   

    to : roger_xiong(风过无痕) (只要也不行啊,这样提示说语法错误。 :(请各位大虾帮帮忙。
      

  5.   

    我也这样试过,但也不行。
    cnn.Execute "insert into disk_classify(FDate) values('"& Date &"')"
    当说类型不匹配。