Results 1 to 11 of 11
  1. #1
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Public Functions for Command Buttons

    Hello All,
    Hope everyone had a great holiday!

    I am setting up some public function for my cmdButtons and need some assistance. I have most of them worked out but need some knowledge on the following: Here is what I have so far.

    Public Function GotoNew()
    'create new record
    Screen.ActiveForm.AllowAdditions = True
    Screen.ActiveForm.CmdPrint.Enabled = False
    Screen.ActiveForm.CmdEdit.Enabled = False
    Screen.ActiveForm.CmdCancel.Enabled = True
    Screen.ActiveForm.CmdClose.Enabled = False
    Screen.ActiveForm.CmdNew.Enabled = False
    Screen.ActiveForm.CmdFirst.Enabled = False
    Screen.ActiveForm.CmdPrev.Enabled = False
    Screen.ActiveForm.CmdNext.Enabled = False
    Screen.ActiveForm.CmdLast.Enabled = False
    Screen.ActiveForm.OnDirty.CmdUndo.Enabled = True (This does not work?)Enable only if Dirty


    Screen.ActiveForm.OnDirty.CmdSave.Enabled = True (This does not work?)
    Enable only if Dirty

    DoCmd.RunCommand acCmdRecordsGoToNew

    End Function

    I would also like some help in changing the caption on form to:
    Screen.ActiveForm.Caption. ??????
    If current form is Cities, then I would like it to read Cities:AddNew
    I just dont know how to call out the existing caption on active form and change the caption?

    Thank you,
    dave

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    You might find using the tag property a useful way of shortening repetitive code of this type.
    It can be used to manage the visible, enabled and locked states for groups of controls.
    See my example app at http://www.mendipdatasystems.co.uk/s...ols/4594398114
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi and thanks, This wasnt exactly what i was looking for as i have chosen a public function as i have a ton of forms very similiar in type and the several comand buttons work exactly the same on all forms, so with that, i can set up one time, copy and past my buttons to another form and they are set up the same. The above is just one example of a button to add a new record. I have many others and yes, dont want the private sub duplicating when they all do the same things, just different forms.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    That won't prevent you using the approach I suggested.
    In fact if you are copying the same buttons to multiple forms it would work well.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks, I will take a longer look at that and see where this leads.

    Any ideas on how i can change my FormCaption as specified in a public function? Example, if City form and caption is Cities, then when add, Cities: Add Mode, If form State caption is States, then States: Add Mode. I dont know how to call out the existing form caption and add the ADD Mode to it.
    Thanks again

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I would pass the form object to the public function. - call it with GotoNew(Me)
    Code:
    Public Function GotoNew(frm as Form)
    then you dont need screen.Activeform

    Code:
    frm.AllowAdditions = True
    frm.CmdPrint.Enabled = False
    
    ...
    frm.Caption = "Cities:AddNew "
    
    
    You could also add conditional statements

    Code:
    if frm.Name = "Cities" then ...
    
    or 
    
    select case frm.Name
    
    case "Cities"
        ' Some Code Here
    case "States"
        'Some Code Here
    end select
    This could also solve this problem
    Code:
    Screen.ActiveForm.OnDirty.CmdUndo.Enabled = True (This does not work?)Enable only if Dirty
    Screen.ActiveForm.OnDirty.CmdSave.Enabled = True (This does not work?)Enable only if Dirty
    
    If frm.Dirty then
         'some code
    Else
         'some code
    End if
    
    
    HTH

  7. #7
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks,
    I know the below doesn't work of coarse, but is there a method to do this? So, if the forms caption was Cities , then it would read Cities: Add Mode, If applied to a different form, such as state form, then it would be State: Add New Or Account: Add Mode.... and so on.

    Screen.ActiveForm.Caption.= FormCaption & "Add Mode"

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    When you pass the form object it exposes all the properties of the object.
    so if you pass the form object (frm as Form) you can use the variable frm pretty much the same way as you would use the Me keyword in the forms module.
    so instead of Me.Caption = "Cities : Add Mode" you would have frm.Caption = "Cities : Add Mode"

    Depending on your naming conventions you may be able to do something like below to make it more portable.
    I always use a prefix on forms like frmMain or frmCities.
    so using the form object you could use
    Code:
    frm.Caption = mid(frm.Name,4) & " : AddMode"
    and to answer your actual question,
    instead of
    Screen.ActiveForm.Caption.= FormCaption & "Add Mode"
    try
    Screen.ActiveForm.Caption = Screen.ActiveForm.Caption & "Add Mode"

  9. #9
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I tried this several ways, but non worked at all.
    Screen.ActiveForm.FormCaption = Mid(Form.Name, 4) & " : AddMode"

    I cant use the Me in the public functions and I think you are on to something with the frm but do I need to declare that form is frm? somehow?
    Also,while we are at this, I am trying to make a cancel button so that if I add a new record (CmdAdd) then change my mind, go back to the form as it was before clicking add? Suggestions?

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I tried this several ways, but non worked at all.
    Screen.ActiveForm.FormCaption = Mid(Form.Name, 4) & " : AddMode"
    note this is wrong Screen.ActiveForm.FormCaption

    Bottom of post#8
    Code:
    Screen.ActiveForm.Caption = Screen.ActiveForm.Caption & "Add Mode"
    but do I need to declare that form is frm? somehow?
    Code:
    Public Function GotoNew(frm as Form)
    frm as Form is declared as an argument

    I cant use the Me in the public functions
    correct. You can only use the Me keyword in the forms module.
    When you pass the form object to the Public Sub or Function as (frm as Form) you can use the variable "frm" instead of "Me"
    so "Debug.Print Me.Name" in a form module is the same as "Debug.Print frm.Name" in a standard module.
    BTW, since your not returning any value from your function you could make it a sub.

  11. #11
    d9pierce1 is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thank YOU,
    Now thats what i was looking for. Thank you for the all the advice too. Worked like a charm.
    Thanks

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

Similar Threads

  1. Replies: 4
    Last Post: 01-20-2017, 08:16 AM
  2. command buttons
    By hijack61 in forum Access
    Replies: 4
    Last Post: 11-19-2011, 04:59 PM
  3. Command Buttons
    By Rosier75 in forum Access
    Replies: 3
    Last Post: 03-09-2011, 11:59 AM
  4. command buttons
    By nashr1928 in forum Forms
    Replies: 23
    Last Post: 10-15-2010, 04:09 PM
  5. Command buttons
    By maintt in forum Forms
    Replies: 3
    Last Post: 08-03-2010, 09:52 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