在MDI窗体上放置一个data控件连接了一个数据库,请问怎样才能在我单击菜单栏上的“设置单价”的时候,数据库中的单价默认值变为用户输入的值(此时数据库中一条记录也没有)。还有顺便问一下,如果数据库中已经有了几条记录,重新设置后,其他的字段(如金额)的数值就应该改变,请问这段代码又应该怎样写?

解决方案 »

  1.   

    我写的代码如下:
    Private Sub mnuprice_Click()
    Dim msg, title, answer
    msg = "请输入本月水单价"
    title = "单价设置"
    answer = InputBox(msg, title)
    Data1.Recordset.Fields("单价").DefaultValue = answer
    Data1.UpdateRecord
    End Sub
      

  2.   

    SQL = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[default_China]') and OBJECTPROPERTY(id, N'IsDefault') = 1)" & _
              "drop default [dbo].[default_China]"
        Conn.Execute SQLSQL = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[default_Now]') and OBJECTPROPERTY(id, N'IsDefault') = 1)" & _
              "drop default [dbo].[default_Now]"
        Conn.Execute SQLSQL = "create default [default_China] as '中华人民共和国'"
        Conn.Execute SQLSQL = "create default [default_Now] as GetDate()"
        Conn.Execute SQL
      

  3.   

    我建議你用Trigger實現“其他的字段(如金额)的数值就应该改变”比較好。
      

  4.   

    设置缺省值的例子
    看看对你有没有什么帮助Sub DefaultValueX()   Dim dbsNorthwind As Database
       Dim tdfEmployees As TableDef
       Dim strOldDefault As String
       Dim rstEmployees As Recordset
       Dim strMessage As String
       Dim strCode As String   Set dbsNorthwind = OpenDatabase("Northwind.mdb")
       Set tdfEmployees = dbsNorthwind.TableDefs!Employees   ' Store original DefaultValue information and set the
       ' property to a new value.
       strOldDefault = tdfEmployees.Fields!PostalCode.DefaultValue
       
       tdfEmployees.Fields!PostalCode.DefaultValue = "98052"   Set rstEmployees = _
          dbsNorthwind.OpenRecordset("Employees", _
          dbOpenDynaset)   With rstEmployees
          ' Add a new record to the Recordset.
          .AddNew
          !FirstName = "Bruce"
          !LastName = "Oberg"      ' Get user input. If user enters something, the field
          ' will be filled with that data; otherwise, it will be
          ' filled with the DefaultValue information.
          strMessage = "Enter postal code for " & vbCr & _
             !FirstName & " " & !LastName & ":"
          strCode = DefaultPrompt(strMessage, !PostalCode)
          If strCode <> "" Then !PostalCode = strCode
          .Update      ' Go to new record and print information.
          .Book = .LastModified
          Debug.Print "  FirstName = " & !FirstName
          Debug.Print "  LastName = " & !LastName
          Debug.Print "  PostalCode = " & !PostalCode      ' Delete new record because this is a demonstration.
          .Delete
          .Close
       End With   ' Restore original DefaultValue property because this is a
       ' demonstration.
       tdfEmployees.Fields!PostalCode.DefaultValue = _
          strOldDefault   dbsNorthwind.CloseEnd Sub