UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Run Public Sub From Command Button, Access 2016    
 
   
brastedhouse
post Aug 20 2019, 05:24 PM
Post#1



Posts: 130
Joined: 16-March 15
From: Chautauqua, NY


Greetings, I have googled this and I can find nothing that I understand to answer my question. I have a module (modCloseMeAndOpenMain) with a public sub:

CODE
Public Sub CloseAndOpenMain(frmMe As Form)
    DoCmd.Close acForm, frmMe.Name
    DoCmd.OpenForm "frmMain"
End Sub


that I want to access through a command button on a form. I am going to use this particular process a lot on a series of forms and I made sense to me to make it something easy to use. I have a button on my form named btnCloseOpenFrm. Then I created an event procedure for the button:

CODE
Private Sub btnCloseOpenFrm_Click()
    CloseAndOpenMain
End Sub


When I save and compile I get a "Compile Error Argument not optional" error. I get the same result when I put Call in front of CloseAndOpenMain.

Obviously I am missing something. Can anyone point me in the right direction. I am finding the material on the web about Public Subs and Public Functions completely confusing.

Thanks, Scott
Go to the top of the page
 
RJD
post Aug 20 2019, 05:35 PM
Post#2


UtterAccess VIP
Posts: 10,131
Joined: 25-October 10
From: Gulf South USA


Hi Scott: Your first Sub has ... Public Sub CloseAndOpenMain(frmMe As Form). So when you call the Sub it needs to have an argument after CloseandOpenMain to tell it which form you are referring to. That's why you get ... Argument not optional.

If you want to just close the active form and open frmMain, then you could try something like this, and not have to name the form ...

Public Sub CloseAndOpenMain()
DoCmd.Close acForm, Screen.ActiveForm.Name
DoCmd.OpenForm "frmMain"
End Sub

(untested)

HTH
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
brastedhouse
post Aug 20 2019, 06:28 PM
Post#3



Posts: 130
Joined: 16-March 15
From: Chautauqua, NY


Aha! That's the bomb. It worked a treat. Thanks so much. I am putting this in my little box of tricks I've learned.
Go to the top of the page
 
RJD
post Aug 20 2019, 07:19 PM
Post#4


UtterAccess VIP
Posts: 10,131
Joined: 25-October 10
From: Gulf South USA


You are very welcome. Glad that worked for you.

Continued success ...

Regards,
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
BruceM
post Aug 21 2019, 12:21 PM
Post#5


UtterAccess VIP
Posts: 7,985
Joined: 24-May 10
From: Downeast Maine


You could also have passed the name of the form as a string.
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    17th November 2019 - 12:21 AM