我写的代码如下: Private Sub mnuprice_Click() Dim msg, title, answer msg = "请输入本月水单价" title = "单价设置" answer = InputBox(msg, title) Data1.Recordset.Fields("单价").DefaultValue = answer Data1.UpdateRecord End Sub
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
我建議你用Trigger實現“其他的字段(如金额)的数值就应该改变”比較好。
设置缺省值的例子 看看对你有没有什么帮助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
Private Sub mnuprice_Click()
Dim msg, title, answer
msg = "请输入本月水单价"
title = "单价设置"
answer = InputBox(msg, title)
Data1.Recordset.Fields("单价").DefaultValue = answer
Data1.UpdateRecord
End Sub
"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
看看对你有没有什么帮助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