繁体   English   中英

需要有关在 MS ACCESS 中使用 SQL 和 VBA 运行查询的建议

[英]Need advice for using SQL & VBA in MS ACCESS to run a query

请不要问问题,这是一个漫长而复杂的故事:-)

我只需要 SQL 字符串中Me.frmButtons.Form.Button01.caption的正确语法(带有所有引号)。 谢谢。 这个不行:

Private Sub Button01_Click()
             
Dim strsql As String
            
strsql = "SELECT * FROM table01 WHERE fldName = ""Me.bForm.Form.Button01.caption""    
ORDER BY FldName"

Me.mForm.Form.RecordSource = strsql
Me.mForm.Form.Requery
                
End Sub

在 VBA 中 SQL 是一个字符串,但不是变量和对象,所以这个对象必须连接到字符串,连接字符串的字符是“&”

SQL 语句应该是这样的:

strsql = "SELECT * FROM table01 WHERE fldName = '" & Me.bForm.Form.Button01.caption & "' ORDER BY FldName"

这应该有效:

Private Sub Button01_Click()
             
    Dim strsql As String
            
    strsql = "SELECT * FROM table01 " & _
    "WHERE fldName = '" & Me!bForm.Form!Button01.Caption & "' " & _
    "ORDER BY FldName"
    Me!mForm.Form.RecordSource = strsql
                
End Sub

另外,你应该给你的按钮起有意义的名字。

暂无
暂无

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

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