SQL / Access对象需要错误

时间:2013-11-18 16:22:49

标签: sql ms-access access-vba

我打算使用下面的代码将记录插入数据库。但是,它抛出了一个对象所需的错误。我检查过,所有变量都包含数据。

Dim vblMealType As String
Dim vblMealQual As String


If txtMealID.ItemsSelected.Count = 0 Then

        MsgBox "Please Select a Meal Type", _
               vbOKOnly + vbInformation
Else



 MsgBox "Customer Charge Succesfull.", _
              vbOKOnly + vbInformation

Dim qdf As DAO.QueryDef

 ERROR ON THE VALUES LINE
Set 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

1 个答案:

答案 0 :(得分:1)

为了避免“对象需要”投诉,请先尝试声明并初始化dbs,然后再尝试将其与CreateQueryDef一起使用。

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef

Set dbs = CurrentDb
Set 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])")