combine two buttons in one

steve2

Registered User.
Local time
Today, 15:31
Joined
Nov 24, 2017
Messages
22
I have the following question:

I have created two working buttons: button 278 and button 282
in my access form
--------------------------------------------

button 278 = to open an existing .txt document and if not exists, create one

Private Sub Command278_Click()
On Error GoTo Err_Knop278_Click


Dim shlApp As String
Dim stDocName As String
Dim stnaamfile As String
stDocName = "bezoekrapport"
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
LResult = LCase("stnaamfile")
shlApp = "notepad.exe c:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile & "" & stDocName & ".txt"

Call Shell(shlApp, vbMaximizedFocus)



Exit_Knop278_Click:
Exit Sub

Err_Knop278_Click:
MsgBox Err.Description
Resume Exit_Knop278_Click
End Sub
--------------------------------------------------------

button 282 = button to open a folder (containing all the clients documents)
and if not exists create one


Private Sub Command282_Click()
On Error GoTo Err_Knop282_Click

Dim Foldername As String
Dim stnaamfile As String
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
LResult = LCase("stnaamfile")

Foldername = "c:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile


On Error Resume Next
MkDir Foldername
Shell "C:\Windows\explorer.exe """ & Foldername & "", vbNormalFocus
On Error GoTo 0

Exit_Knop282_Click:
Exit Sub

Err_Knop282_Click:
MsgBox Err.Description
Resume Exit_Knop282_Click

End Sub

----------

Both buttons are working fine and do wath I want them to do.

But I would like the button 278 to do one thing more. And that is:
If nor the folder nor the text file exist, it should first create
the new folder and afterwards the new text file.

(this is because my colleague also uses the program, and I don't want him to think to much....)


Is this possible?
 
I'm sure we can arrange for your colleague not to think too much ....

First of all you need to start naming your controls so they mean something
You will have no idea what Command 282 or 278 (etc) do in a few weeks time

Yes its possible - have you tried just putting the code from the second button into the first button code?
 
Call Command282_Click in Command278_Click event ?

However if you want to do that, why just not have one button (323?) that does both regardless of whether they exist or not?
 
Collin, true. I've never done that, and I sometimes have to go back to the form to see why and how I created the command button.
I'll try to think about it.

As for the 323 button: I sometimes only want to open the folder to look what's in it.
The .txt file is only necessary if I had a conversation with a client.
 
For myself, I would move all of the code for each button into its own sub.

Code:
Private Sub Command278_Click()
On Error GoTo Err_Knop278_Click


Dim shlApp As String
Dim stDocName As String
Dim stnaamfile As String
stDocName = "bezoekrapport"
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
LResult = LCase("stnaamfile")
shlApp = "notepad.exe c:\Users\eigenaar\Documents\BEHEER\beheerklantenma ppen" & stnaamfile & "" & stDocName & ".txt"

Call Shell(shlApp, vbMaximizedFocus)



Exit_Knop278_Click:
Exit Sub

Err_Knop278_Click:
MsgBox Err.Description
Resume Exit_Knop278_Click
End Sub

becomes

Code:
Private Sub Command278_Click()
   TCO_OpenText
End Sub

Private Sub TCO_OpenText

On Error GoTo Err_OpenText

Dim shlApp As String
Dim stDocName As String
Dim stnaamfile As String
stDocName = "bezoekrapport"
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
LResult = LCase("stnaamfile")
shlApp = "notepad.exe c:\Users\eigenaar\Documents\BEHEER\beheerklantenma ppen" & stnaamfile & "" & stDocName & ".txt"

Call Shell(shlApp, vbMaximizedFocus)

Exit_OpenText
Exit Sub

Err_OpenText
MsgBox Err.Description
Resume Exit_OpenText
End Sub

This way if you with to combined them you simply call each in order. It also allows you to use the same code in other places, if needed.

More important, if this is something that needs to be called from multiple forms you can put it in a global module and simple call as needed.
 
Call Command282_Click in Command278_Click event ?

However if you want to do that, why just not have one button (323?) that does both regardless of whether they exist or not?

The latter.
I meant move all the code into the one button and delete the other.

However, Mark's suggestion is a good idea
 
Thanks Colin. I learned a long time ago to put code like this into its own little piece. Makes it far easier to debug when you are looking at 7 lines than 207. Once you get each piece working the rest is easy!
 
Steve, If you want to keep both buttons, you should probably change the labels so people don't use both when they only need to use one. The one button that will do the two tasks, simply calls two subs (if you made them subs - not necessary in this case) or if you left the code in the click event, run the second button code from the first.

Call Command282_Click()

That was just for syntax. I forgot which one should call the other.

FYI, When you change the Name property of the control, All the event code will be orphaned so be prepared to find it and rename as needed. Compile and test because it is easy to miss something. In the future, make it a habit of changing the Name property IMMEDIATELY after you add a control to the form to avoid this type of cleanup.
 
Thanks all for the help.

Mark, I tried your code, but I get the following fault (probably because I did not comprehend the remark of Pat to create the 'subs'. - I hope you understand that I'm learning...)

I have the following result:

-------------
Compile error:
Sub or Function not defined
-------------
The thing I don't understand is how to combine:
First I do the TCO-OpenText
with End Sub, so I close the command.
And than I get the end line under my end sub command.
And then I have to start the new Private Sub Command. Which
doesn't work. So I think I'm doing something wrong.

So thanks for your comment on this .
------------------------


When Collin told me to do the 2 commands in one, I made the following code:


Private Sub Command278_Click()
'Creates or opens a text file in existing Folder
'If folder does not exist first creates one
'If Text File does not exist creates one


On Error GoTo Err_Knop278_Click


Dim shlApp As String
Dim stDocName As String
Dim stnaamfile As String
stDocName = "bezoekrapport"
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
Dim Foldername As String
LResult = LCase("stnaamfile")
Foldername = "c:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile
shlApp = "notepad.exe c:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile & "" & stDocName & ".txt"


Call Shell(shlApp, vbMaximizedFocus)

On Error Resume Next
MkDir Foldername
Shell "C:\Windows\explorer.exe """ & Foldername & "", vbNormalFocus

Call Shell(shlApp, vbMaximizedFocus)

Exit_Knop278_Click:
Exit Sub

Err_Knop278_Click:
MsgBox Err.Description
Resume Exit_Knop278_Click
End Sub

------------
That does more or less the job, but It gives the following errors for me:
1) It creates the folder, so that's ok, but this stays open and says it's empty. No problem if I just close it, but of course I want it to do that automatically. 2)Second thing is (and that's of course as it should be), it opens or creates the text file (and asks me if it should create a new one if it does not exist). So I think the only thing I need is to close the new created folder before creating the new text file. How do I do that?

Thanks for your comments!
 
Steve,

Did you
1) Create TCO_OpenText?
2) Copy the code from Command278_Click() into TCO_OpenText?
3) Replace all code in Command278_Click() TCO_OpenText?

Remember, you would need TCO_OpenText to be AFTER the End Sub for Command278_Click().

This is what I did and posted.
 
Hi Mark, I must admit I don't understand the TCO code. (I did not get it to work, and I didn't find much when googling, so I tried to adapt it myself, but no luck. Sorry, probably my lack of programming).

In the mean time I found another solution with a TaskKill- command which finally works fine:


-----------------------------------------------------------------------------
Private Sub Command298_Click()
'Creates or opens a text file in existing Folder
'If folder does not exist first creates one
'If Text File does not exist creates one


Dim vPID As String
Dim shlApp As String
Dim stDocName As String
stDocName = "bezoekrapport"
Dim Foldername As String
Dim stnaamfile As String
stnaamfile = Me.ACHTERNAAM & " " & Me.VOORNAAM
Dim LResult As String
LResult = LCase("stnaamfile")
Foldername = "C:\USERS\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile
vPID = Shell("C:\Windows\explorer.exe "" & Foldername""")


On Error Resume Next
MkDir Foldername
Shell "C:\Windows\explorer.exe """ & Foldername & "", vbNormalFocus
On Error GoTo 0

Call Shell("TaskKill /F /PID " & CStr(vPID), vbHide)


shlApp = "notepad.exe c:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & stnaamfile & "" & stDocName & ".txt"
Call Shell(shlApp, vbMaximizedFocus)


Exit_Knop298_Click:
Exit Sub

Err_Knop298_Click:
MsgBox Err.Description
Resume Exit_Knop298_Click

End Sub

-----------------------------------------------------------------------------
 
Life can be simple

In the mean time, I got another code from Edmoor on a dutch site
(thanks Ed) and it also works perfectly:

--------------------------------------------

Private Sub Command278_Click()
'Creates or opens a text file in existing Folder
'If folder does not exist first creates one
'If Text File does not exist creates one

stDocName = "bezoekrapport.txt"
Foldername = "C:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen" & LCase(Me.ACHTERNAAM & " " & Me.VOORNAAM)

If Dir(Foldername, vbDirectory) = "" Then MkDir Foldername
If Dir(Foldername & "" & stDocName) = "" Then
Open Foldername & "" & stDocName For Output As #1
Close #1
End If
Shell ("Notepad " & Foldername & "" & stDocName), vbNormalFocus
End Sub

------------------
I'm learning....
 
I adjusted the code so it creates .LOG in the first line. This is results in opening a file with time and date everytime it gets opened:
-------------------------------------------------------------------------

Private Sub Command299_Click()

'Creates or opens a text file in existing Folder
'If folder does not exist first creates one
'If Text File does not exist creates one
'write .LOG as first line in the text so it opens with the actual time and date

Dim stDocname As String
Dim Foldername As String

stDocname = "bezoekrapport.txt"
Foldername = "C:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen\" & LCase(Me.ACHTERNAAM & " " & Me.VOORNAAM)

If Dir(Foldername, vbDirectory) = "" Then MkDir Foldername
If Dir(Foldername & "\" & stDocname) = "" Then
Open Foldername & "\" & stDocname For Append As #1
Print #1, ".LOG"

Close #1
End If
Shell ("Notepad " & Foldername & "" & stDocname), vbNormalFocus

End Sub
 
Sorry, I just learned how to use the 'codetags'.
So this should be corrrect
This thread can be closed

------------------------------------------------------------------------

Code:
Private Sub Command299_Click()

    'Creates or opens a text file in existing Folder
    'If folder does not exist first creates one
    'If Text File does not exist creates one
    'writes date and time to the first line
    
    Dim stDocname As String
    Dim Foldername As String
    
        stDocname = "bezoekrapport.txt"
    Foldername = "C:\Users\eigenaar\Documents\BEHEER\beheerklantenmappen\" & LCase(Me.ACHTERNAAM & " " & Me.VOORNAAM)
    
    If Dir(Foldername, vbDirectory) = "" Then MkDir Foldername
    If Dir(Foldername & "\" & stDocname) = "" Then
        Open Foldername & "\" & stDocname For Append As #1
        Print #1, ".LOG"
        
        Close #1
    End If
    Shell ("Notepad " & Foldername & "\" & stDocname), vbNormalFocus

End Sub
 
Hi Steve
Only you can close the thread. To do so, click the thread tools drop down and click mark as solved
 

Users who are viewing this thread

Back
Top Bottom