Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I am new in VBA access database and my background programming language has been focused on C#

I have a task to launch a specific file name (all files in doc, xls, etc) from the same directory path when the user click on a button for each specific file name.

I have been googling and used different approach still it seemed not to work.

I tried this:
Private Sub FileMsReports_Click()

Dim f As Object

Set f = Application.FileDialog(3)

f.AllowMultiSelect = True

If f.Show Then
    For i = 1 To f.SelectedItems.Count
        sFile = FileName(f.SelectedItems(i), sPath)
        MsgBox sPath & "---" & sFile
    Next
End If
End Sub

Public Function FileName(ByVal strPath As String, sPath) As String
    sPath = Left(strPath, InStrRev(strPath, "\"))
    FileName = Mid(strPath, InStrRev(strPath, "\") + 1)

End Function


I ended up getting a file dialog show up but it is not actually what my supervisor wanted me to do!
I am still like less a year in programming and right now this is a task giving to me for the place of my Practicum.

Please I need someone to help me out and direction on how to go about this.

Thanks in advance.
Posted
Updated 18-Dec-13 10:15am
v3

1 solution

All you need to do is:

  1. Create table[^] to store file names
  2. SQL
    CREATE TABLE MyFiles (MyFile CHAR);

  3. Create new form[^] and drop on it ListBox control
  4. Drop a Button control and create OnClick event
  5. Bind ListBox[^] with MyFiles table
  6. Go to form module[^] and create new procedure EnumMyFiles
  7. VB
    Private Sub EnumMyFiles()
    'body of the procedure
    End Sub

  8. Inside the body of EnumMyFiles procedure:

    • Clear ListBox using RowSource[^] property
    • VB
      Listbox.RowSource = ""
      

    • Remove all data from table (How? See point no. 5 - last sentence)
    • VB
      DELETE * FROM MyFiles;

    • define variable to store initial directory - this databse path[^]
    • loop through the collection of files using Dir function (VB)[^] function.
    • Insert each file name which corresponds to the extension into database using DoCmd.RunSQL[^] method
    • VB
      DoCmd.RunSQL "INSERT INTO MyFiles (MyFile) VALUES('" & sFileName & "');"


  9. (re)Bind ListBox
  10. VB
    LisBox.RowSource = "MyFiles"

  11. Inside Button_Click procedure call EnumMyFiles procedure
  12. VB
    Privare Sub Button_Click()
        EnumMyFiles
    End Sub

  13. Save changes and test it
 
Share this answer
 
v2
Comments
Richard C Bishop 18-Dec-13 18:14pm    
+5
Maciej Los 18-Dec-13 18:15pm    
Thank you, Richard ;)
Chukse 20-Dec-13 11:50am    
Please I still need help!

How do I create the access table programmatically and also how to loop through the collection of files using Dir function (VB)[^] function.
I am very confused and this is not C sharp

Thanks

Chukse
Maciej Los 21-Dec-13 15:29pm    
How to create access table programmatically? Why? What's for? You don't need that. You can do it once - manually.
For Dir() function, follow the link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900