我正在为 Access 数据库编写一条 sql 语句,无论输入如何,该语句都会返回唯一的值。我正在使用此代码,但是我在执行语句上遇到类型不匹配的情况。
strSQL = "SELECT FilePath " _
& "FROM ToolFiles " _
& "WHERE Project_Num = '" & theSelectedProj & "'" _
& "AND Tool_Name = '" & theSelectedProjName & "'"
filePath = cn.Execute(strSQL)
有没有办法从sql语句中返回字符串?
谢谢
最佳答案
答案是否定的。ADO Execute()
方法返回一个记录集对象,您需要将其读入字符串变量。像这样的事情应该可以做到:
Dim rs As ADODB.Recordset
....
Set rs = cn.Execute(strSQL)
If Not (rs Is Nothing) Then
With rs
If Not (.BOF) And Not (.EOF) Then
strFilePath = Format$(.Fields(1).Value)
End If
End With
End If
关于excel vba sql语句返回单个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9668381/