Results 1 to 4 of 4
  1. #1
    sardamil is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Posts
    7

    Running a module from a button in a form

    I'm making a small database to help a friend. In this database he needs to track customers and their appointments. For this I created a form to enter custumerdetails and appointments. Both are added to their respective tables using a macro. The customernumber is an automatic number of the table customer. The max number is shown on the form, using a query to select that number. Every time the macro is executed I need to requery te form to refresh the max number. This works when using shift-F9, but I couldn't get this working so I converted the macro and added a requery statement to the code. Now I'm looking for a way to run my module when clicking a button on my form. Unless somebody can show me a way to requery my form after executing the macro.

  2. #2
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    visual basic is the way forward over macros in my opinion at least, and the good news is that most of the work has been done for you.

    ensure all the events are clear on the command button, then right click, build event, code builder and past your code in there, however if you fancy posting your code, i might be able to help you tidy it up a little

  3. #3
    sardamil is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Posts
    7
    Thanks for the quick reply.
    As it says above, I'm a bit of a novice. Creating a button to run a macro was easy. But after converting it, I tried changing the code and run it again. The button would still run the old macro and not the module of the converted macro. I was trying to run the module. I guess I should change the command DoCmd.RunMacro stDocName to RunModule or something.
    On a different note, what would be the best way to requery my form? Should I run a requery in the macro or can I put some code in the form?

    Here's the code:

    Button on form:
    Private Sub uitvoeren_macro_nieuwe_patient_Click()
    On Error GoTo Err_uitvoeren_macro_nieuwe_patient_Click
    Dim stDocName As String
    stDocName = "Macro toevoegen nieuwe patient"
    DoCmd.RunMacro stDocName

    Exit_uitvoeren_macro_nieuwe_patient_Clic:
    Exit Sub
    Err_uitvoeren_macro_nieuwe_patient_Click:
    MsgBox Err.Description
    Resume Exit_uitvoeren_macro_nieuwe_patient_Clic

    End Sub

    Converted Macro:

    Function Macro_toevoegen_nieuwe_patient()
    On Error GoTo Macro_toevoegen_nieuwe_patient_Err
    DoCmd.SetWarnings False
    ' Toevoegen nieuwe patient
    DoCmd.OpenQuery "Query toevoegen nieuwe patient", acViewNormal, acEdit
    ' Berekenen wachttijd
    DoCmd.OpenQuery "Query berekenen wachttijd", acViewNormal, acEdit
    ' refresh screen
    DoCmd.RunCommand acCmdRefresh
    ' Toevoegen hoogste patientnummer aan tabel afspraak
    DoCmd.OpenQuery "Query toevoegen hoogste patientnummer aan tabel afspraak", acViewNormal, acEdit
    ' Afspraak nieuwe patient toevoegen
    DoCmd.OpenQuery "Query toevoegen afspraak", acViewNormal, acEdit
    ' DoCmd.Requery "Formulier_afspraak_nieuwe_patient"
    Formulier_afspraak_nieuwe_patient.Requery
    Macro_toevoegen_nieuwe_patient_Exit:
    Exit Function
    Macro_toevoegen_nieuwe_patient_Err:
    MsgBox Error$
    Resume Macro_toevoegen_nieuwe_patient_Exit
    End Function

  4. #4
    sardamil is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    May 2012
    Posts
    7
    I created a macro with the command runcode to run the module. That seems to do the trick. Now I need to find out how to refresh the form, but I'll create a new thread for that.

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

Similar Threads

  1. Replies: 4
    Last Post: 05-16-2011, 04:58 PM
  2. Running exe's or Powershell from an Access form button
    By HoustonIT in forum Programming
    Replies: 7
    Last Post: 04-14-2011, 02:57 PM
  3. Running a module
    By KevinMCB in forum Modules
    Replies: 2
    Last Post: 03-09-2011, 02:38 PM
  4. Running module from Macro
    By Harley Guy in forum Modules
    Replies: 6
    Last Post: 10-27-2010, 11:05 AM
  5. Replies: 1
    Last Post: 04-06-2009, 12:05 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