下面的代码应该在事务表中插入一条新记录。但是,它导致SET QDF行上的编译错误“无效使用属性”。什么可能导致这样的错误?
Private Sub cmdCharge_Click()
Dim vblMealType As String
Dim vblMealQual As String
Dim dbs As DAO.Database
If txtMealID.ItemsSelected.Count = 0 Then
MsgBox "Please Select a Meal Type", _
vbOKOnly + vbInformation
Else
MsgBox "Customer Charge Succesfull.", _
vbOKOnly + vbInformation
Set dbs = CurrentDb
Dim QDF As DAO.QueryDef
QDF = dbs.CreateQueryDef("", _
"PARAMETERS prmCustomerID Text(255), prmMealID Text(255), prmTransactionAmount Currency, prmTransactionDate Text(255);" & _
"INSERT INTO dbo_Transactions (CustomerID, MealID, TransactionAmount, TransactionDate) " & _
"VALUES ([prmCustomerID], [prmMealID], [prmTransactionAmount], [prmTransactionDate])")
QDF!prmCustomerID = txtCustomerID.Value
QDF!prmMealID = txtMealID.Value
QDF!prmTransactionAmount = txtCharge.Value
QDF!prmTransactionDate = Date
QDF.Execute dbFailOnError
MsgBox "Customer Charge Succesfull.", _
vbOKOnly + vbInformation
Set QDF = Nothing
Set dbs = Nothing
DoCmd.OpenForm "Charge Form"
DoCmd.Close acForm, Me.Name
End If
End Sub
答案 0 :(得分:0)
由于QDF
是对象变量,因此在为其指定值时必须使用Set
关键字。
Set dbs = CurrentDb
Dim QDF As DAO.QueryDef
Dim strInsert As String
strInsert = "PARAMETERS prmCustomerID Text(255), prmMealID Text(255), prmTransactionAmount Currency, prmTransactionDate Text(255);" & _
"INSERT INTO dbo_Transactions (CustomerID, MealID, TransactionAmount, TransactionDate) " & _
"VALUES ([prmCustomerID], [prmMealID], [prmTransactionAmount], [prmTransactionDate])"
Debug.Print strInsert
Set QDF = dbs.CreateQueryDef("", strInsert)