VB访问MsgBox以返回多个结果的SQL查询

时间:2016-08-17 14:58:17

标签: ms-access access-vba ms-access-2010

我正在尝试使用VB将多个值返回到MS Access中的消息框输出,到目前为止我已成功使用下面的代码,我现在需要扩展,以便消息框输出逗号分隔的不同月份名称。

Dim rs As DAO.Recordset
Set rs=CurrentDB.OpenRecordset("Qry_MonthName")
 If Not rs.EOF Then
MsgBox "Records Loaded Are For The Month Of: " & rs.Fields(0)
End if

有人可以提出建议吗?

2 个答案:

答案 0 :(得分:0)

您可以在循环中创建逗号分隔的字符串,然后使用以下内容将msgbox字符串创建:

Dim rs As DAO.Recordset
Dim mthString as string
mthString=""
Set rs=CurrentDB.OpenRecordset("Qry_MonthName")
 If Not rs.EOF Then
    rs.firstMove
      Do Until rs.EOF
      mthString = mthString & rs.Fields(0) & ", "
      rs.MoveNext ' if you forget this an infinite loop will occur
     Loop
   End if
' remove last comma
mthString=left(mthString,len(mthString)-2)
msgbox "Records loaded for the months: " & mthString

答案 1 :(得分:0)

试试这个:

Dim rs As DAO.Recordset
Dim strOutput As String
Dim i As Integer

Set rs = CurrentDb.OpenRecordset("Qry_MonthName")

With rs
    'test for empty recordset
    If Not .EOF And Not .BOF Then
        .MoveLast
        .MoveFirst

        For i = 0 To (.RecordCount - 1)
            'test for last record
            If i = (.RecordCount - 1) Then
                'last record
                strOutput = strOutput + !yourField
            Else
                'all other records
                strOutput = strOutput + !yourField + ", "
            End If

            .MoveNext
        Next
    End If
    .Close
End With
    'execute messagebox
    MsgBox strOutput
Set rs = Nothing
相关问题