- 确保 Recordset 值不为空并存在。
 
- 使用 SQL 语句从数据库中检索数据并将数据加载到 Recordset 中。
 
- 在读取 Recordset 的值之前,将其转换为字符串。例如:CStr(rs.fields("fieldName").value)。
 
- 确保字段名正确拼写并与数据库中的字段名相同。
 
- 确保在连接数据库时使用了正确的驱动程序。
以下是一个示例代码,演示了如何使用 Recordset 从数据库读取数据并将其输出为字符串:
 
Sub readFromDatabase()
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim sql As String
    Dim output As String
    
    '连接数据库
    conn.ConnectionString = "Driver={SQL Server}; Server=myServerName\myInstanceName; Database=myDataBase; Trusted_Connection=yes;"
    conn.Open
    '执行 SQL 语句并将数据加载到 Recordset 中
    sql = "SELECT * FROM myTable"
    rs.Open sql, conn
    
    '读取 Recordset 值并输出为字符串
    output = CStr(rs.fields("fieldName").value)
    Debug.Print output
    
    '关闭连接和 Recordset
    rs.Close
    conn.Close
End Sub