Office中国论坛/Access中国论坛

标题: zhuyiwen请回答 [打印本页]

作者: lisashar    时间: 2002-9-16 04:53
标题: zhuyiwen请回答
在组合框中添加新记录时出现错误
-2147217900:在此山下文中不允许使用“卧室888”。此处只允许常量、表达式或变量。不允许使用列名。
请讲解一下好吗?


Private Function AddToList(strTable As String, strField As String, _
strData As String) As Integer

    ' Add item to table
    ' Returns acDataErrAdded if successful,
    ' acDataErrDisplay on any error
   
    Dim cnCurrent As ADODB.Connection
    Dim strSQL As String
   
    Set cnCurrent = CurrentProject.Connection
        
    strSQL = "Insert into " & strTable & " (" & strField & ") Values " _
      & "(" & conQuote & strData & conQuote & ")"
    cnCurrent.Execute strSQL
    cnCurrent.Close
    Set cnCurrent = Nothing

ExitHere:
    AddToList = acDataErrAdded
    Exit Function

HandleErr:
    AddToList = acDataErrDisplay
    Select Case Err
        Case Else
            MsgBox Err & ": " & Err.Description, , _
              "Form_HouseholdInventory.AddToList()"
    End Select
    Resume ExitHere
End Function

Private Sub Room_NotInList(NewData As String, Response As Integer)
    ' Add item to list
On Error GoTo HandleErr

    If MsgBox("是否将此数据项添加到列表中?", _
     vbYesNo + vbQuestion, "数据项不在列表中") = vbYes Then
        Response = AddToList("房间", "房间", NewData)
    Else
        Response = acDataErrDisplay
    End If
   
ExitHere:
    Exit Sub

HandleErr:
    Select Case Err
        Case Else
            MsgBox Err & ": " & Err.Description, , _
              "Form_HouseholdInventory.Room_NotInList"
    End Select
    Resume ExitHere
    Resume
End Sub
作者: zhuyiwen    时间: 2002-9-16 08:54
conQuote 变量是不是为“'”(单引号)?
[em26]

程序中没看到定义!
作者: lisashar    时间: 2002-9-17 05:23
标题: 我用的是双引号
Const conQuote As String = """"
改成单引号后仍报错
数据源为:access中inventay升迁到adp
作者: zhuyiwen    时间: 2002-9-17 09:09
能否将你的ADP和数据库备份挂上来?要调试,这样很难做(不好意思)[em26]
作者: zhuyiwen    时间: 2002-9-17 09:38
就代码中的错误,作了一些更正,也不知对不对,试试吧。[em26]

Const conQuote As String = "'"  ' 这是常量定义

Private Function AddToList(strTable As String, strField As String, _
strData As String) As Integer
   On Error GoTo HandleErr  ' 这是要增加的错误处理语句

   ' Add item to table
   ' Returns acDataErrAdded if successful,
   ' acDataErrDisplay on any error

   Dim cnCurrent As ADODB.Connection
   Dim strSQL As String

   Set cnCurrent = CurrentProject.Connection

   strSQL = "Insert into " & strTable & " (" & strField & ") Values " _
            & "(" & conQuote & strData & conQuote & ")"
   cnCurrent.Execute strSQL

'    cnCurrent.Close  ' 不能关闭当前连接
'    Set cnCurrent = Nothing  

ExitHere:
'    AddToList = acDataErrAdded  
   Exit Function

HandleErr:
   AddToList = acDataErrDisplay
   Select Case Err
   Case Else
      MsgBox Err & ": " & Err.Description, , _
             "Form_HouseholdInventory.AddToList()"
   End Select

   Resume ExitHere
End Function

Private Sub Room_NotInList(NewData As String, Response As Integer)
   ' Add item to list
   On Error GoTo HandleErr

   If MsgBox("是否将此数据项添加到列表中?", _
        vbYesNo + vbQuestion, "数据项不在列表中") = vbYes Then
      Response = AddToList("房间", "房间", NewData)
   Else
      Response = acDataErrDisplay
   End If

ExitHere:
   Exit Sub

HandleErr:
   Select Case Err
   Case Else
      MsgBox Err & ": " & Err.Description, , _
             "Form_HouseholdInventory.Room_NotInList"
   End Select
   Resume ExitHere
'    Resume  
End Sub
[em28]
作者: lisashar    时间: 2002-9-17 22:11
谢谢你,

经过测试,如下部分不能取消,否则组合框类不更新值,但表中已输入数据
' cnCurrent.Close ' 不能关闭当前连接
' Set cnCurrent = Nothing

ExitHere:
' AddToList = acDataErrAdded

能解释一下,为什么要将conQuote 变量定义为="'",以前是""""


作者: zyz    时间: 2002-9-17 23:32
单引号是SQL语句的界定符,而双引号不是。
[em26]


[此贴子已经被作者于2002-9-17 15:32:23编辑过]






欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3