Results 1 to 3 of 3
  1. #1
    Join Date
    May 2013
    Posts
    2

    Running Code from a Module in a Macro


    Hi
    I have a module that I have created that imports data from a spreadsheet into a table through a subroutine and that works fine.
    I am now trying to run that subroutine using a macro but do not seem to be successful.
    I have tried "OpenModule" but that just opens the VBA code, does not run it
    I have tried "RunMacro" but the subroutine is not shown
    I have tried "RunCode" but that is looking for a function, but not sure I have that. I am still a novice at this game.
    The code I have at the moment is as follows:

    Code:
    
    
    Code:
    Sub TestImport()
    Dim strPathFile As String
    Dim strTable As String, strBrowseMsg As String
    Dim strFilter As String, strInitialDirectory As String
    Dim blnHasFieldNames As Boolean
    
    
    blnHasFieldNames = True
    strBrowseMsg = "Select the EXCEL file:"
    
    
    strInitialDirectory = "C:\Documents and Settings\"
    
    
    strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")
    strPathFile = ahtCommonFileOpenSave(InitialDir:=strInitialDirectory, _
          Filter:=strFilter, OpenFile:=False, _
          DialogTitle:=strBrowseMsg, _
          Flags:=ahtOFN_HIDEREADONLY)
    If strPathFile = "" Then
          MsgBox "No file was selected.", vbOK, "No Selection"
          Exit Sub
    End If
    
    
    
    
    strTable = "Test"
    
    
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
          strTable, strPathFile, blnHasFieldNames
    
    
    Kill strPathFile
    End Sub


    Any help would be very gratefully received
    Thanks

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,537
    Instead of using a macro you could use code to run your sub.
    Alternatively, change your sub to a function so that it can be run with "RunCode"
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Join Date
    May 2013
    Posts
    2
    Thanks Bob

    I have changed it to a Function and it worked perfectly. Many thanks for your help

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 01:41 PM
  2. Running a module from a button click
    By adacpt in forum Modules
    Replies: 4
    Last Post: 12-03-2012, 01:09 PM
  3. Running a module from a button in a form
    By sardamil in forum Modules
    Replies: 3
    Last Post: 05-01-2012, 10:59 PM
  4. Running a module
    By KevinMCB in forum Modules
    Replies: 2
    Last Post: 03-09-2011, 02:38 PM
  5. Running module from Macro
    By Harley Guy in forum Modules
    Replies: 6
    Last Post: 10-27-2010, 11:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums