Results 1 to 7 of 7
  1. #1
    bonnielynnw is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2016
    Location
    Howland, Maine
    Posts
    15

    Easy Peasy Question...How to run VB code from event of form...

    I got a new PC and with it, new Microsoft 365 for 1 and my DB now gets an error that the field doesn't exist. Yes it does. Anyway, I converted the macro to VB and now would like to know how to run it from the OnCurrent event on a form. The macro and now the VB has several sections that I want all to run.



    Thank you to anyone who can offer help on this. The form worked great before. It opens to AddNew and when you select Vet;Customer;Shelter, the appropriate fields either appear or disappear.

    It appears that the Module is named ConvertedMacro-mShowHideSourceInfo

    Here is the code:

    Option Compare Database


    '------------------------------------------------------------
    ' mShowHideSourceInfo_ShowVetInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_ShowVetInfo()
    On Error GoTo mShowHideSourceInfo_ShowVetInfo_Err


    DoCmd.SetProperty "VetIdNumber", acPropertyVisible, "-1"
    DoCmd.SetProperty "VetCode", acPropertyVisible, "-1"
    DoCmd.SetProperty "bVetList", acPropertyVisible, "-1"
    DoCmd.SetProperty "bHideVetList", acPropertyVisible, "-1"
    DoCmd.SetProperty "fVetList", acPropertyVisible, "-1"
    DoCmd.SetProperty "OwnerName", acPropertyVisible, "-1"
    DoCmd.RunMacro "mShowHideSourceInfo.HideCustomerInfo", , ""
    DoCmd.RunMacro "mShowHideSourceInfo.HideShelterInfo", , ""




    mShowHideSourceInfo_ShowVetInfo_Exit:
    Exit Function


    mShowHideSourceInfo_ShowVetInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_ShowVetInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_HideVetInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_HideVetInfo()
    On Error GoTo mShowHideSourceInfo_HideVetInfo_Err


    DoCmd.SetProperty "fVetList", acPropertyVisible, "0"
    DoCmd.SetProperty "VetIdNumber", acPropertyVisible, "0"
    DoCmd.SetProperty "VetCode", acPropertyVisible, "0"
    DoCmd.SetProperty "bVetList", acPropertyVisible, "0"
    DoCmd.SetProperty "bHideVetList", acPropertyVisible, "0"
    DoCmd.SetProperty "OwnerName", acPropertyVisible, "0"




    mShowHideSourceInfo_HideVetInfo_Exit:
    Exit Function


    mShowHideSourceInfo_HideVetInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_HideVetInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_ShowCustomerInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_ShowCustomerInfo()
    On Error GoTo mShowHideSourceInfo_ShowCustomerInfo_Err


    DoCmd.SetProperty "fCustomerList", acPropertyVisible, "-1"
    DoCmd.SetProperty "CustomerIDNumber", acPropertyVisible, "-1"
    DoCmd.SetProperty "CustomerNameLast", acPropertyVisible, "-1"
    DoCmd.SetProperty "CustomerNameFirst", acPropertyVisible, "-1"
    DoCmd.SetProperty "bCustomerList", acPropertyVisible, "-1"
    DoCmd.SetProperty "bHideCustomerList", acPropertyVisible, "-1"
    DoCmd.SetProperty "bHuh", acPropertyVisible, "-1"
    DoCmd.SetProperty "bGoToCustInfo", acPropertyVisible, "-1"
    DoCmd.RunMacro "mShowHideSourceInfo.HideShelterInfo", , ""
    DoCmd.RunMacro "mShowHideSourceInfo.HideVetInfo", , ""




    mShowHideSourceInfo_ShowCustomerInfo_Exit:
    Exit Function


    mShowHideSourceInfo_ShowCustomerInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_ShowCustomerInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_HideCustomerInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_HideCustomerInfo()
    On Error GoTo mShowHideSourceInfo_HideCustomerInfo_Err


    DoCmd.SetProperty "fCustomerList", acPropertyVisible, "0"
    DoCmd.SetProperty "CustomerIDNumber", acPropertyVisible, "0"
    DoCmd.SetProperty "CustomerNameLast", acPropertyVisible, "0"
    DoCmd.SetProperty "CustomerNameFirst", acPropertyVisible, "0"
    DoCmd.SetProperty "bCustomerList", acPropertyVisible, "0"
    DoCmd.SetProperty "bHideCustomerList", acPropertyVisible, "0"
    DoCmd.SetProperty "bHuh", acPropertyVisible, "0"
    DoCmd.SetProperty "bGoToCustInfo", acPropertyVisible, "0"




    mShowHideSourceInfo_HideCustomerInfo_Exit:
    Exit Function


    mShowHideSourceInfo_HideCustomerInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_HideCustomerInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_ShowShelterInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_ShowShelterInfo()
    On Error GoTo mShowHideSourceInfo_ShowShelterInfo_Err


    DoCmd.SetProperty "fShelterList", acPropertyVisible, "-1"
    DoCmd.SetProperty "ShelterIDNumber", acPropertyVisible, "-1"
    DoCmd.SetProperty "ShelterCode", acPropertyVisible, "-1"
    DoCmd.SetProperty "bShelterList", acPropertyVisible, "-1"
    DoCmd.SetProperty "bHideShelterList", acPropertyVisible, "-1"
    DoCmd.RunMacro "mShowHideSourceInfo.HideCustomerInfo", , ""
    DoCmd.RunMacro "mShowHideSourceInfo.HideVetInfo", , ""




    mShowHideSourceInfo_ShowShelterInfo_Exit:
    Exit Function


    mShowHideSourceInfo_ShowShelterInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_ShowShelterInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_HideShelterInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_HideShelterInfo()
    On Error GoTo mShowHideSourceInfo_HideShelterInfo_Err


    DoCmd.SetProperty "fShelterList", acPropertyVisible, "0"
    DoCmd.SetProperty "ShelterIDNumber", acPropertyVisible, "0"
    DoCmd.SetProperty "ShelterCode", acPropertyVisible, "0"
    DoCmd.SetProperty "bShelterList", acPropertyVisible, "0"
    DoCmd.SetProperty "bHideShelterList", acPropertyVisible, "0"




    mShowHideSourceInfo_HideShelterInfo_Exit:
    Exit Function


    mShowHideSourceInfo_HideShelterInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_HideShelterInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_HideAll
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_HideAll()
    On Error GoTo mShowHideSourceInfo_HideAll_Err


    DoCmd.RunMacro "mShowHideSourceInfo.HideCustomerInfo", , ""
    DoCmd.RunMacro "mShowHideSourceInfo.HideShelterInfo", , ""
    DoCmd.RunMacro "mShowHideSourceInfo.HideVetInfo", , ""




    mShowHideSourceInfo_HideAll_Exit:
    Exit Function


    mShowHideSourceInfo_HideAll_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_HideAll_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_PickVetInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_PickVetInfo()
    On Error GoTo mShowHideSourceInfo_PickVetInfo_Err


    DoCmd.SetProperty "VetIdNumber", acPropertyVisible, "-1"
    DoCmd.SetProperty "VetCode", acPropertyVisible, "-1"




    mShowHideSourceInfo_PickVetInfo_Exit:
    Exit Function


    mShowHideSourceInfo_PickVetInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_PickVetInfo_Exit


    End Function




    '------------------------------------------------------------
    ' mShowHideSourceInfo_UnpickVetInfo
    '
    '------------------------------------------------------------
    Function mShowHideSourceInfo_UnpickVetInfo()
    On Error GoTo mShowHideSourceInfo_UnpickVetInfo_Err


    DoCmd.SetProperty "VetIdNumber", acPropertyVisible, "0"
    DoCmd.SetProperty "VetCode", acPropertyVisible, "0"




    mShowHideSourceInfo_UnpickVetInfo_Exit:
    Exit Function


    mShowHideSourceInfo_UnpickVetInfo_Err:
    MsgBox Error$
    Resume mShowHideSourceInfo_UnpickVetInfo_Exit


    End Function

  2. #2
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Please use Code Tags to make the code vaguely readable!! (The # mark in the editor)

    You haven't told us which bit of that very long code it errors on.
    Comment out all the error handlers by putting a ' in front of the On Error... lines

    You will then get an error message, press Debug and it will highlight the actual error line.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    The convert macro to VBA function creates standard modules in my experience. A standard module wouldn't have any knowledge of the fields/controls on the form. Typically after converting a macro to VBA you'd copy the code to the event procedure desired:

    http://www.baldyweb.com/FirstVBA.htm

    Only copy the code, not the "Function MacroName()" or "End Function" lines. See where that gets you.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    By the way, while that code will likely work, it's not commonly used in my experience. Things like:

    DoCmd.SetProperty "VetIdNumber", acPropertyVisible, "0"

    Would be more like this:

    Me.VetIdNumber.Visible = False

    You'll find coding like that easier, as intellisense will show you the available objects and properties as you type.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    bonnielynnw is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2016
    Location
    Howland, Maine
    Posts
    15
    Sorry but I don't even know what "code tags" are. My macro errored on the first field, VetIdNumber so I converted the macro to VB and just need to know how to run the VB module OnCurrent Event on my form.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Did you try post 3's suggestion?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    bonnielynnw is offline Novice
    Windows Vista Access 2007
    Join Date
    Nov 2016
    Location
    Howland, Maine
    Posts
    15
    Not yet, no. I've zipped the file and someone is looking at the macro I was using originally. Thank you!

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

Similar Threads

  1. Hopefully an easy question
    By KeithSinsa in forum Access
    Replies: 3
    Last Post: 02-18-2019, 11:13 AM
  2. Easy Question
    By BenjaminDz in forum Access
    Replies: 13
    Last Post: 05-06-2015, 02:58 PM
  3. Easy question
    By fyes in forum Programming
    Replies: 1
    Last Post: 12-20-2011, 12:08 AM
  4. Replies: 3
    Last Post: 05-02-2011, 07:34 AM
  5. Easy Form Question
    By B-dub in forum Forms
    Replies: 3
    Last Post: 12-26-2010, 07:04 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