简体   繁体   English

访问INSERT查询不适用于SQL Server链接表

[英]Access INSERT query not working on SQL Server linked table

The code below is supposed to insert a record into the transactions table. 下面的代码应该在交易表中插入一条记录。 The code runs with no errors, however when looking at the transactions table no new row has been inserted. 该代码运行没有错误,但是在查看事务处理表时,没有插入新行。

If txtMealID.ItemsSelected.Count = 0 Then

        MsgBox "Please Select a Meal Type", _
               vbOKOnly + vbInformation
Else
Dim vblMealType As String
vblMealType = txtMealID.Value
MsgBox " " & vblMealType & " ", vbCritical + vbApplicationModal
Set dbs = CurrentDb

Dim qdf As DAO.QueryDef
Set qdf = dbs.CreateQueryDef("", _
        "PARAMETERS prmCustomerID Long, prmMealID Text(255), prmTransactionAmount Currency, prmTransactionDate DateTime;" & _
        "INSERT INTO dbo_Transactions (CustomerID, MealID, TransactionAmount, TransactionDate) " & _
        "VALUES ([prmCustomerID], [prmMealID], [prmTransactionAmount], [prmTransactionDate]) ")


qdf!prmCustomerID = txtCustomerID.Value
qdf!prmMealID = vblMealType
txtCharge.SetFocus
qdf!prmTransactionAmount = txtCharge.Value

qdf!prmTransactionDate = Format(Date, "yyyy-mm-dd")
qdf.Execute dbFailOnError
 MsgBox "Customer Charge Succesfull.", _
              vbOKOnly + vbInformation

Set qdf = Nothing
Set dbs = Nothing

It appears that the problem is with the Set qdf Line. 看来问题出在Set qdf Line。 My MsgBox " " & vblMealType & " will not trigger when the Set qdf line is present. 当存在设置qdf行时,我的MsgBox“&vblMealType&”将不会触发。

You should test this workaround, avoiding using DAO query object, but rather using SQL execution provided by DAO::Database. 您应该测试此解决方法,避免使用DAO查询对象,而应使用DAO :: Database提供的SQL执行。

Private Sub sofInsert20036438_Click()

  If txtMealID.ItemsSelected.Count = 0 Then
    MsgBox "Please Select a Meal Type", _
      vbOKOnly + vbInformation
  Else

    Dim strSQL As String, vblMealType As String
    Dim dbs

    vblMealType = txtMealID.Value
    MsgBox " " & vblMealType & " ", vbCritical + vbApplicationModal
    Set dbs = CurrentDb

    'Dim qdf As DAO.QueryDef
    'Set qdf = dbs.CreateQueryDef("", _
    '  "PARAMETERS prmCustomerID Long, prmMealID Text(255), prmTransactionAmount Currency, prmTransactionDate DateTime;" & _
    '  "INSERT INTO dbo_Transactions (CustomerID, MealID, TransactionAmount, TransactionDate) " & _
    '  "VALUES ([prmCustomerID], [prmMealID], [prmTransactionAmount], [prmTransactionDate]) ")

    'qdf!prmCustomerID = txtCustomerID.Value
    'qdf!prmMealID = vblMealType
    'txtCharge.SetFocus
    'qdf!prmTransactionAmount = txtCharge.Value

    'qdf!prmTransactionDate = Format(Date, "yyyy-mm-dd")
    'qdf.Execute dbFailOnError

    strSQL = "INSERT INTO dbo_Transactions (CustomerID, MealID, TransactionAmount, TransactionDate) " _
      & "VALUES (" & txtCustomerID.Value _
      & ", '" & vblMealType & "'" _
      & ", " & txtCharge.Value _
      & ", #" & Format(Date, "yyyy-mm-dd") & "#);"

    dbs.Execute strSQL, dbSeeChanges

    If (dbs.RecordsAffected > 0) Then
      strSQL = "Customer Charge Succesfull."
    Else
      strSQL = "Error inserting."
    End If
    '
    MsgBox strSQL, vbOKOnly + vbInformation

'   Set qdf = Nothing
    Set dbs = Nothing
  End If
End Sub

Here the syntax of SQL string is supposed to use these fields definitions: 在这里,SQL字符串的语法应该使用以下字段定义:

CustomerID: INT,
MealID: VARCHAR(255),
TransactionAmount: FLOAT,
TransactionDate: DATETIME.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM