Running a Module in Access

I am new to visual basic. I inserted a new module in an Access database, and I copied a sample VB code to that module and I saved it. I do not know how to run it. I am assuming that I need to create a macro that opens and runs that module. Would someone please walk me through the exact process to do that? Assume that the module is called Module_Test.

I very much appreciate your help.
Answer
Answer

Ah - you have mistakenly just copied in some code without putting it inside a procedure. 

 

First up we need to move that code into a procedure.  I'll use a Function as a function is callable from a macro but a sub is not.

 

So change your module to look like this:

 

' CODE STARTS HERE:

Option Compare Database

Option Explicit

 

Function DoImport()

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents and Settings\myName\My Documents\Access Test\"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

      strFile = Dir()
Loop

End Function

 

I would modify it further to make it generic but I fear it may confuse you too much at this point.

 

So to call this function, make sure you make the changes in the code to reflect the paths and names that are for your file and then you can call it from the macro by using

 

RunCode

 

and the argument is

 

DoImport()

-----
Microsoft Access MVP 2008, 2009, 2011
If a post was helpful click the FOUND THIS HELPFUL link

9 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 7, 2024 Views 18,089 Applies to: