VBA example - Microsoft Access: VBA array

VBA array


In this example, you will learn VBA Array to store items(songs). The songs will be read from the array in to a list box.  The form of the program has a list box(lstSong), a window media player(WMPlayer), and two command buttons(Play, Stop). See the figure below:

 VBA example - Microsoft Access VBA Show Picture in List Box 

-Play command button is used to play the selected song in the list box.

-Stop command button is used to stop playing the song.

-When the form loads, all songs are stored in an array and then populated in the list box and the first item is selected.(See SongCollection form above).

-If you want to play any song, select the song in the list box and click Play command button.

- To stop playing the song, click Stop command button.

To have a form as the figure above, you have to create the form in Form Design. See the figure below:

VBA example - Microsoft Access VBA Array Form Design 

Note: Unselect Use Control Wizards before you drag these controls to the form.

Drag and drop a list box, a window media player, and two command buttons to the form.

-For Window Media Player did not display its shape in the Controls Group. You need to add it from Insert ActiveX Control. To add this control:

- Click Insert ActiveX Control in the Controls Group of  Design Tap.

VBA example - Microsoft Access Insert ActiveX Control 

-Then click Windows Media Play from the ActiveX Control list.

- Set Name properties of  a list box, a windows media player, and two command buttons:

Name: lstSong

Name:WMPlayer

Name: CmdPlay

Name: CmdStop

Then apply the VBA Code below:

  

Option Explicit

Option Compare Database

Dim WMP As WindowsMediaPlayer

Dim i, j As Integer

Dim SongData(18) As String

....................................................................................... 

Private Sub Form_Load()

    Set WMP = WMPlayer.Object

    lstSong.RowSourceType = "Value List"

    For j = 1 To 18

    ' All items(songs) store in an array variable

        SongData(j) = CurrentProject.Path & "\song2008\Track" & j & ".mp3"

        Me.lstSong.AddItem SongData(j)

    Next

    lstSong.Selected(0) = True ' Select first item

End Sub

 .......................................................................................

Private Sub CmdPlay_Click()

    On Error Resume Next

    lstSong.ListIndex = 0

    i = lstSong.ListIndex

    WMP.URL = lstSong.Column(0, i) ' Play selected item

End Sub

 .......................................................................................

Private Sub CmdStop_Click()

    On Error Resume Next

    If WMP.playState = wmppsPlaying Then

            WMP.Close ' Stop playing

    End If

End Sub




Comments

Name:
Comment:
Enter the text:
CAPTCHA image

.........................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word.
However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like