访问VBA - 识别文本 [英] Access VBA - Identifying text

查看:325
本文介绍了访问VBA - 识别文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个按钮来删除子窗体某些记录。然而即时得到语法错误(缺少操作员)在查询前pression'KEY_ID =1。

我知道这个问题是:属性,因此文本的价值需要由单引号包围。我只是不知道怎么写的VBA来做到这一点。

 私人小组cmdDelete_Click()
如果不是(Me.subKey.Form.Recordset.EOF而Me.subKey.Form.Recordset.BOF)然后
    如果MSGBOX(确认删除?,vbYesNo)= vbYes然后
        昏暗STRSQL作为字符串
        STRSQL =DELETE FROM KEYS&放大器; _
            WHE​​RE KEY_ID ='&放大器; Me.subKey.Form.Recordset.Fields(KEY_ID)
        Debug.Print STRSQL'<  - 打印到立即窗口
        CurrentDb.Execute STRSQL,dbFailOnError

    结束如果
结束如果
 

解决方案

  STRSQL =DELETE FROM KEYS&放大器; _
    WHE​​RE KEY_ID ='&放大器; Me.subKey.Form.Recordset.Fields(KEY_ID)及'
 

...不过,请注意,使用引号作为一个ASCII字符是安全的,或者至少更灵活或便携式,当这类事情变得更加密集。

  STRSQL =DELETE FROM KEYS&放大器; _
    WHE​​RE KEY_ID =&放大器; _
    字符(32)及Me.subKey.Form.Recordset.Fields(KEY_ID)及字符(32)
 

我不知道这是 CHR(32)但我认为这是正确的。

Im trying to create a button to delete certain records in a subform. However im getting "syntax error (missing operator) in query expression 'KEY_ID="1'.

I know what the problem is: The attribute is text therefore the value needs to be surrounded by single quotes. I just don't know how to write the VBA to accomplish this.

Private Sub cmdDelete_Click()
If Not (Me.subKey.Form.Recordset.EOF And Me.subKey.Form.Recordset.BOF) Then
    If MsgBox("Confirm Deletion?", vbYesNo) = vbYes Then
        Dim strSql As String
        strSql = "DELETE FROM KEYS" & _
            " WHERE KEY_ID='" & Me.subKey.Form.Recordset.Fields("KEY_ID")
        Debug.Print strSql ' <- prints to Immediate window
        CurrentDb.Execute strSql, dbFailOnError

    End If
End If

解决方案

strSql = "DELETE FROM KEYS" & _
    " WHERE KEY_ID='" & Me.subKey.Form.Recordset.Fields("KEY_ID") & "'"

... though, mind you, using the quote as an ASCII character is safer, or at least more versatile or portable, when these kinds of things get more intensive.

strSql = "DELETE FROM KEYS" & _
    " WHERE KEY_ID=" & _
    Chr(32) & Me.subKey.Form.Recordset.Fields("KEY_ID") & Chr(32)

I'm not sure it's Chr(32) but I think that is correct.

这篇关于访问VBA - 识别文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆