Results 1 to 7 of 7
  1. #1
    kloun04 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Wisconsin
    Posts
    33

    How do I attach a function to a form button

    I have the following code for my Function that I want to assign to a button on my form.

    ' ImportDay2SurveyResults
    '
    '------------------------------------------------------------
    Public Function ImportDay2SurveyResults()
    On Error GoTo ImportDay2SurveyResults_Err


    DoCmd.TransferSpreadsheet acImport, 10, "APP Orientation - Day 2", "U:\UWHealth\Nrsg-PCS\SpecialShares\APP Director\Education and Professional Development\Orientation\3. Orientation Program Day 2\Evaluations\APP Orientation Day 2 Survey Data Entry Macro.xlsm", True, "Survey Responses!A2:T50"

    'I added the following code to run a delete query b/c the above command keeps adding 3-4 blank rows to my table. It's probably not the right code, but I couldn't figure out how to run the query versus open it.
    DoCmd.SetWarnings False
    DoCmd.OpenQuery "Qry DeleteBlankRows-Day2", acViewNormal, acReadOnly

    ImportDay2SurveyResults_Exit:
    Exit Function


    ImportDay2SurveyResults_Err:


    MsgBox Error$
    Resume ImportDay2SurveyResults_Exit


    End Function

    I thought the following code would work, but it doesn't.
    'Button Properties: Event>On Click>[Event Procedure]

    Private Sub Command214_Click()
    mdl_ImportDay2SurveyResults.ImportDay2SurveyResult s
    End Sub

    How do I get the event procedure to run the VBA code in my module?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You don't include the module name, just the function. See:

    http://www.baldyweb.com/Function.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    I only ever used a Function if I wanted to return a value, else it was just a Sub?

    Don't forget to set warnings back on either.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    kloun04 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Wisconsin
    Posts
    33
    Quote Originally Posted by pbaldy View Post
    You don't include the module name, just the function. See:

    http://www.baldyweb.com/Function.htm
    I read your htm file, renamed my Function so it's not the same name as the module, re-wrote my code and now I get an error telling me an object is required. What is it looking for now? I obviously have no idea what I'm doing.

    Private Sub Command214_Click() mdl_ImportDay2SurveyResults.ImportDay2Survey
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Again, you don't include the name of the module. Try just:

    ImportDay2Survey
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    kloun04 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Wisconsin
    Posts
    33
    Thank you so much, it works perfectly! I was reading your initial comment to mean I wasn't including the module name vs I needed to include the module name.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 24
    Last Post: 08-07-2022, 11:22 AM
  2. Replies: 2
    Last Post: 10-25-2016, 04:09 PM
  3. Form : Query button not function
    By suzeq in forum Forms
    Replies: 2
    Last Post: 03-10-2016, 03:43 AM
  4. Replies: 7
    Last Post: 10-09-2014, 03:06 PM
  5. Form command button to do something/call function
    By shubhamgandhi in forum Programming
    Replies: 2
    Last Post: 07-27-2011, 04:45 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