PowerPoint / MS Access VBA帮助

时间:2018-10-30 19:50:28

标签: vba ms-access powerpoint-vba

我正在尝试通过将MS Access数据库链接到PowerPoint来自动执行报告编写过程。我不知道如何使代码将查询输出到文本框中。该代码运行良好,直到带有“ ******”的行为止。有谁知道如何使label1.value等于查询输出?谢谢!

Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recordset1 As ADODB.Recordset

'Database path info
 Dim FD As FileDialog
 Dim vrtSelectedItem As Variant
 Dim fileName As String

 Set FD = Application.FileDialog(msoFileDialogFilePicker)

With FD
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "The path is: " & vrtSelectedItem

 'Open the connection
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data Source=" & vrtSelectedItem & ";"
Connection.Open ConnectionString:=Connect

'Set RecordSet
Set Recordset1 = New ADODB.Recordset

With Recordset1
    Source = "SELECT [xxx] FROM [yyy] WHERE [zzz] = '12345'"
.Open Source:=Source, ActiveConnection:=Connection
********
***Label1.Value = Recordset1***
********
End With

Next vrtSelectedItem

End If
End With
End Sub

1 个答案:

答案 0 :(得分:1)

Do
    Label1.Value = Label1.Value & vbCrLf & Recordset1.Fields("[YOUR FIELD]").Value
    Recordset1.MoveNext
Loop Until Recordset1.EOF

此代码将显示您查询中的所有记录([您的字段]字段)。

  

但是您需要添加Recordset1.RecordCount以避免错误。

If Recordset1.RecordCount > 0 Then
    Do
        Label1.Value = Label1.Value & vbCrLf & Recordset1.Fields("[YOUR FIELD]").Value
        Recordset1.MoveNext
    Loop Until Recordset1.EOF
End If