1

I am intending to use the code below to insert a record into the database. However, it is throwing an object required error. I have checked and all the variables contain data.

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
4
  • Was that a complete self-contained code example? The reason I ask is because you have If and Else but no End If. Does your code compile? Commented Nov 18, 2013 at 16:46
  • @HansUp If you check their recent questions you'll see that the last three are all related. They're flailing about trying to implement the answer here. Commented Nov 18, 2013 at 17:36
  • @GordThompson Yes, I see your point. I think OP should include Option Explicit, then run Debug->Compile and clean up anything the compiler complains about. Then see what problem remains and post a question about that problem. Commented Nov 18, 2013 at 17:44
  • @user1698144 You have txtMealID.ItemsSelected.Count and later txtMealID.Value. So what is txtMealID: text; combo; or list box? Commented Nov 18, 2013 at 17:48

1 Answer 1

1

In order to avoid that "object required" complaint, first declare and initialize dbs before you attempt to use it with 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])")
Sign up to request clarification or add additional context in comments.

1 Comment

Invalid Use of Property ("Set QDF")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.