How do I use a function on a button? (1 Viewer)

YuvalH

New member
Local time
Today, 22:21
Joined
Jan 21, 2022
Messages
13
I have this code in a module and I don't know how do I use it in a button.
[
Function Create_Folder(sFolder As String)
If Len(Dir(sFolder, vbDirectory)) = 0 Then
MkDir sFolder
End If
End Function

Function FolderCheck()
Create_Folder ("E:\" & Format(Date, "yyyy"))
Create_Folder ("E:\" & Format(Date, "yyyy") & "\" & Format(Date, "mm") & " " & Format(Date, "mmmm"))
End Function
]

Thx for the help
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:21
Joined
Oct 29, 2018
Messages
19,186
In the Click event of the button, you can use something like:
Code:
Create_Folder
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:21
Joined
Feb 28, 2001
Messages
23,179
If you can use VBA well enough to show the function as you did, then the rest is relatively easy.

In Form Design mode, create a command button on the desired form. If the wizard is enabled, you really don't care what the button wizard asks to do. You would in fact do just as well if it did nothing else than create the button.

Still in design mode, select that button. From the ribbon, open the button's property sheet and select the Events tab. Find the Click event and RIGHT-click in the box to the right of the line containing OnClick. It will offer you a choice of Macro or Event Code. Select event code. Access will create an empty button-click routine and switch to the VBA window. You put your FolderCheck code in that routine. You leave the Create_Folder code in a general module.

That is HOW you do it. I use a variant of this method all the time when I want to execute some code. I look at whatever the Button Wizard builds as a scaffold that I can use to build what I really wanted, and I can remove the code offered by the wizard if I don't really what that.

The other question is some minor technicalities. The code you showed first at least tests for possible conflicts, but there can be other issues than having that file already in existence. There is also the fact that you called Create_Folder a function but never return a value for it. Access might object because it wants to return a value for a function. FolderCheck is also a function that doesn't return anything. Both of these functions could be defined as SUB rather than FUNCTION and would execute perfectly well, but Access would not complain about not returning a value. Finally, the Create_Folder routine, if it exists in a general module, should be prefaced with PUBLIC so that code in other modules (like a form's class module) can see it and call it. NOTE: If not specified, a SUB in a general module is public anyway, but including the keyword PUBLIC makes it clear to any subsequent maintainer that the routine was intended for PUBLIC vs. PRIVATE access.

 

YuvalH

New member
Local time
Today, 22:21
Joined
Jan 21, 2022
Messages
13
1645890347257.png
1645890392448.png

Didn't work
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:21
Joined
Oct 29, 2018
Messages
19,186
Oh, I missed the argument part. Try something like:
Code:
Create_Folder CurrentProject.Path & "\TESTFOLDER"
 

YuvalH

New member
Local time
Today, 22:21
Joined
Jan 21, 2022
Messages
13
Thank you very much "The Doc Man", it worked!
 

Users who are viewing this thread

Top Bottom