Craig
Craig

Reputation: 153

PowerPoint/MS Access VBA assistance

I am trying to automate a report writing process by linking an MS Access database to PowerPoint. I do not know how to make the code output the query into a textbox. The code is running fine until the line with "******". Does anyone know how to make the label1.value equal to the query output? Thanks!

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

Upvotes: 0

Views: 175

Answers (1)

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

This code will display all records ([YOUR FIELD] Field) in your query.

But you need to add Recordset1.RecordCount to avoid the error.

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

Upvotes: 1

Related Questions