创建了一个数据库,其中的记录为空,在MDI窗体上放置了一个data控件和数据库相连,当我按下MDI窗体上的“设置单价”后,怎样才能实现以后每增加一条记录单价就自动是我设置好的值?
Private Sub mnuprice_Click()
Dim msg
msg = "please enter the price:"
answer = InputBox(msg)
......(这中间应该怎样处理)
End Sub

解决方案 »

  1.   

    Private Sub mnuprice_Click()  Dim msg as string
      dim answer as string  msg = "please enter the price:"
      answer = InputBox(msg)  if isnumeric(answer) then
        with recordset
          .addnew
          .fields(0).value=...
          .fields(1).value=...
          ...
          .fields("price").value=val(answer)
          ...
          .update
        end with
       else
        msgbox "单价格式不正确"
      end ifEnd Sub
      

  2.   

    有两种方法,一是用公共变量存储依个单价,一个是把它存放在文件上,每次增加记录,都读依个文件来赋值。
    代码:如下。
    在标准模快下,声明一个变量。dim price as string
    Private Sub mnuprice_Click()
    Dim msg
    msg = "please enter the price:"
    answer = InputBox(msg)
    price=answer
    End Sub
    private sub addnew_click()
     data1.addnew
     data1.recordset.field("单价")=price
    data1.update
    end sub
    第二种方法:
    Private Sub mnuprice_Click()
    Dim msg
    msg = "please enter the price:"
    answer = InputBox(msg)
    open app.paht & "\price.txt" for output as #1
     print #1,answer
    close #1
    End Sub
     private sub addnew()
      dim price as string
      data1.recordset.addnew
       open app.path & "\price.txt" for input as #1
        lineinput #1,price
    close #1
      data1.recordset.field("单价")=price
    data1.record.update
    end sub
    第二种方法,就算你下次再启动程序时也能读出你上次设置的单价。第一种就不能了。