Results 1 to 12 of 12
  1. #1
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58

    Get Function to run as a macro

    I am not sure how to get the following code to run under a macro. When I go into new macro and select Run Code and enter the Function GetUserName(). I try to run the code and receive a message stating that the function can not be found. I know that it is not found since it under a Private Declared Function but I am not sure how to change the code to get it to run as a macro. Please help.


    Code:
    Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Function GetUserName() As String
    ' Returns the network login name
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
        strUserName = String$(254, 0)
        lngLen = 255
        lngX = apiGetUserName(strUserName, lngLen)
        If (lngX > 0) Then
            fOSUserName = Left$(strUserName, lngLen - 1)
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel19, "Random_Provider_List", "E:\Documents and Settings\" & fOSUserName & "\Desktop\Random_Provider_List.xls", , "Random_Provider_List"
        Else
            fOSUserName = vbNullString
        
        End If
    End Function


  2. #2
    llkhoutx is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Location
    Houston, Texas USA
    Posts
    373
    Use the RunCode action in a macro, specifyin the function.

    If you can write the subject code, you should avoid macros unless absolutely necessary and graduate to VBA.

    VBA is instantly viewable, were macros have to be drilled through, which you'll find is a significant dowside of macros.

  3. #3
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Quote Originally Posted by llkhoutx View Post
    Use the RunCode action in a macro, specifyin the function.

    If you can write the subject code, you should avoid macros unless absolutely necessary and graduate to VBA.

    VBA is instantly viewable, were macros have to be drilled through, which you'll find is a significant dowside of macros.
    When I use the RunCode action in the macro and try to run the macro it says the function can not be found since it is a Private Function...I have also tried it as Public function and it still will not work.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The function needs to be in a standard module and not in a class module of a form. Is it? You might also create another Public Function in that standard module that invokes this function and returns the result.

  5. #5
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Quote Originally Posted by RuralGuy View Post
    The function needs to be in a standard module and not in a class module of a form. Is it? You hight also create another Public Function in that standard module that invokes this function and returns the result.

    I am not sure how to tell if it is a standard or class module...I created the module under the module tab (Tables, Queries, Forums, Reports, Pages, Macros, Modules) How would I create another Public Function in that standard module that will invoke the function and return the result...I am new and still learning so thank you for your patience.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    This is the Function:
    Function GetUserName() As String
    and the default is Public but it couldn't hurt to make sure.
    Public Function GetUserName() As String
    ...it should work! I don't use macros so I'm not real familiar with how or if they work.

  7. #7
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Quote Originally Posted by RuralGuy View Post
    This is the Function:
    Function GetUserName() As String
    and the default is Public but it couldn't hurt to make sure.
    Public Function GetUserName() As String
    ...it should work! I don't use macros so I'm not real familiar with how or if they work.
    Thank you. I tried to change it to Public Function GetUserName() As String and it still would not work. I am trying to get this function to run on a button click and have tried to get the function to run directly without the use of a macro and received a error message stating that the Function GetUserName() could not be found. I am at a loss and ready to just give up.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How about switching to code for this event? A Function needs some place to put the results. Does a macro allow for that? It is different than just running a Sub that has no return value. Anyway, if you want to switch to code there are pleanty of us that can help you write it.

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    BTW have you eliminated corruption as a possible problem by importing your db into a new, empty db and see if the problem follows the import?

  10. #10
    ChrisO is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2005
    Location
    Brisbane, Australia
    Posts
    27
    I will assume that the module name is also GetUserName and that should be fixed first..

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Thanks Chris! I completely overlooked that possibility. D'oh!

  12. #12
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Wow...Never would have thought of that. As soon as I changed the Module Name it worked like a charm. That is good to know. I am very impressed it is the little things that get me each and every time.

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

Similar Threads

  1. Running a VB function in a Macro
    By JuuJuu in forum Access
    Replies: 1
    Last Post: 10-27-2009, 02:50 AM
  2. import using Macro
    By johnny1g in forum Import/Export Data
    Replies: 1
    Last Post: 09-15-2009, 04:21 AM
  3. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM
  4. error in macro
    By nawaray in forum Access
    Replies: 0
    Last Post: 02-10-2009, 12:06 PM
  5. Macro for Indexed Value
    By jversiz in forum Access
    Replies: 0
    Last Post: 10-19-2007, 01:16 PM

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