帮忙看看这个“添加”代码有什么问题Private Sub cmd_add_Click()
DataGrid1.AllowAddNew = True
Adodc1.Recordset.AddNew
Text1.Text = ""
Text2.Text = ""
cmd_save.Enabled = True
cmd_add.Enabled = False
Dim temp1
Dim sql$
temp1 = Format(Now, "yyyymmdd")
sql = "select * from order_books where dh like '%" + temp1 + "%' order by dh"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
Adodc1.Recordset.MoveLast
Text1.Text = Adodc1.Recordset.Fields("dh")
Text1.Text = Val(Adodc1.Recordset.Fields("dh")) + 1
Else
Text1.Text = Format(Now, "yyyymmdd")
Text1.Text = Text1.Text + "0000"
End If
Adodc1.RecordSource = "select * from order_books order by dh"
Adodc1.Refresh
End Sub
比如我添加了一条记录200611180001,按"添加"按钮后,新的记录会覆盖原来的,也就是永远只有一条记录。帮忙看看这代码有什么问题

解决方案 »

  1.   

    从程序上看,这认为是这样的,程序执行顺序错了DataGrid1.AllowAddNew = True
    Text1.Text = ""
    Text2.Text = ""
    cmd_save.Enabled = True
    cmd_add.Enabled = False
    Dim temp1
    Dim sql$temp1 = Format(Now, "yyyymmdd")
    sql = "select * from order_books where dh like '%" + temp1 + "%' order by dh"
    Adodc1.Refresh
    If Adodc1.Recordset.RecordCount > 0 ThenAdodc1.Recordset.MoveLast
    Text1.Text = Adodc1.Recordset.Fields("dh")      
    Text1.Text = Val(Adodc1.Recordset.Fields("dh")) + 1
      '上面两句是不是可以写成一句就够了(重复),此处是好不要向text1.text赋值,建立一个变量A
    Else
    Text1.Text = Format(Now, "yyyymmdd")   ‘注释同上
    Text1.Text = Text1.Text + "0000"
    End IfAdodc1.Recordset.AddNew  ’将此句移到此处’下面在对写
    text1.text=A
    Adodc1.Recordset.Update  '最后别忘了
    Adodc1.RecordSource = "select * from order_books order by dh"
    Adodc1.Refresh
      

  2.   

    text1.text 和 ADODC 进行数据关联了吗?
      

  3.   

    是SQL无法调试,
    但你没有将text1和数据库关联,即text1.datasource=adodc1(在设计时设)
      

  4.   

    我想应该是错在 cmd_add_Click中最后两句
    Adodc1.RecordSource = "select * from order_books order by dh"
    Adodc1.Refresh
    如果在添加状态,刷新数据是不是意味着放弃添加呢(我也不确定),去掉它们试试,
      

  5.   

    谢谢gguueesstt,就是最后那句惹的祸