VB6,MS Access,DAO - 显示所有记录WHERE列名IS NOT NULL

时间:2017-08-02 10:54:56

标签: ms-access vb6 dao

我需要一些帮助,所以我要做的是显示所有记录WHERE column-name IS NOT NULL。我所拥有的是:

Dim rs As Recordset Dim sqlStr As String

Set WS = DBEngine.Workspaces(0)
DbFile = (App.Path & "\mydb.mdb")

strSql = "SELECT * FROM MyDatabaseName"

Set rs = db.OpenRecordset(strSql)
rs.MoveFirst

Do While Not rs.EOF
lblResult.Caption = ("cust Name: " & rs!SurName & " cust Surname: " & rs!FirstName)
  rs.MoveNext
Loop

MsgBox ("End ")

我试过的是

strSql = "SELECT * FROM MyDatabaseName WHERE column-name IS NOT NULL"

但我收到错误3131.感谢任何帮助。另外,如何在标签的ListBox中显示结果。

1 个答案:

答案 0 :(得分:1)

简单修复:使用特殊字符时将列名括在方括号中,如-减法运算符:

strSql = "SELECT * FROM MyDatabaseName WHERE [column-name] IS NOT NULL"

SQL的其余部分似乎有效。

如果您想要一个包含此查询结果的列表框作为列表,只需将其行源设置为等于此查询,并将其行源类型设置为等于表/查询。不需要任何VBA。

如果您想将所有内容放入标签中,请使用:

Do While Not rs.EOF
  lblResult.Caption = lblResult.Caption & "cust Name: " & rs!SurName & " cust Surname: " & rs!FirstName & VbCrLf
  rs.MoveNext
Loop