Question Combining two function in one

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
Hello Experts. I have a question.
i wrote 2 functions. I need to combine them into one function.
Can you help me please ?

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Using myWriter As New StreamWriter("C:\Users\ELIANE\Desktop\Exported-to-Exel.csv", True)
myWriter.Write("{0},{1},{2},{3},{4},{5}", txtCode.Text, cmbTitle.Text, txtName.Text, cmbPosition.Text, txtAddress.Text, txtPhoneNumber.Text)
End Using
End Sub


Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Using myWriter As New StreamWriter("C:\Users\ELIANE\Desktop\Exported-to-Notepad.txt", True)
myWriter.Write("{0},{1},{2},{3},{4},{5}", txtCode.Text, cmbTitle.Text, txtName.Text, cmbPosition.Text, txtAddress.Text, txtPhoneNumber.Text)
End Using
End Sub






Thank you :)
 
Hi,

It sounds like you just need to create a simple Subroutine to do the saving of the file and send the File Name and Path that you want to use as an argument to the routine. i.e:-

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
  SaveToFile("C:\Users\ELIANE\Desktop\Exported-to-Exel.csv")
  SaveToFile("C:\Users\ELIANE\Desktop\Exported-to-Notepad.txt")
End Sub
 
Private Sub SaveToFile(ByVal myFilePath As String)
  Using myWriter As New StreamWriter(myFilePath, True)
    myWriter.Write("{0},{1},{2},{3},{4},{5}", txtCode.Text, cmbTitle.Text, txtName.Text, cmbPosition.Text, txtAddress.Text, txtPhoneNumber.Text)
  End Using
End Sub


Hope that helps.

Cheers,

Ian
 
You presumably don't realise this but what you're doing there by saving the data twice is completely pointless. You are ending up with two identical files so having both of them is useless.

Firstly, there's no such thing as exporting to Notepad. Notepad is an application, not a file format. All you're doing there is saving to a text file. Notepad is an application that can read and write text files but text files don't inherently belong to Notepad. Text files are not even specifically a Windows thing. There are numerous applications on numerous platforms that can read and write text files. Notepad just happens to be the one that Microsoft supply by default with Windows but you can use any other that you like.

Secondly, your save to Excel doesn't. You're saving to a CSV file, which is... a text file. The file you create is, bit for bit, exactly the same as the other one. If you actually wanted to export to an Excel format then you'd use XLS or XLSX, which takes far more work to write and read but supports far more than plain text. A CSV file is simply a text file that contain character-delimited data. If Excel is installed then it will be the default application to read such files but it's not a native Excel or even Windows format.

What you should be doing is simply saving once to CSV. The user can then open that in Excel by default or they can open it in Notepad if they want, either by selecting Open With or else opening it from within Notepad.
 
Back
Top