I am creating a form that will prompt the user to enter a file name upon loading the file. The issue I encounter is that my variable I use to store the input is not recognized in another one of my procedures. The code here shows my current set up.

Imports System.IO

Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
    Dim strFileName = InputBox("Please name the file you would like to save the data to: ")

End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtEmail.Clear()
    txtExtension.Clear()
    txtFirst.Clear()
    txtLast.Clear()
    txtMiddle.Clear()
    txtNumber.Clear()
    txtPhone.Clear()

End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

Dim inputFile As New StreamWriter(strFileName)

    If File.Exists(strFileName) = True Then
        inputFile = File.CreateText(strFileName)
        inputFile.Write(txtEmail.Text)
        inputFile.Write(txtExtension.Text)
        inputFile.Write(txtFirst.Text)
        inputFile.Write(txtLast.Text)
        inputFile.Write(txtMiddle.Text)
        inputFile.Write(txtNumber.Text)
        inputFile.Write(txtPhone.Text)
        inputFile.Write(cmbDepart.Text)
    Else
        MessageBox.Show("" & strFileName & "Cannot be created or found.")
    End If
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

End Class

The load event handler is where I want the user to input the name of the file.

3

There are 3 answers

0
OneFineDay On BEST ANSWER

You could use My.Settings and load the file string there and save it. All forms can now access that. When you close the the app set it to String.Empty if you want it to be.

You also could be taking advantage of a class object for the fields you are capturing then using either BinaryFormatter or XmlSerializer to store the data. Better databinding, creation and reconstruction using an object.

My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()
2
Trevor On

Change the scope of your variable to outside of your load method...

 Public Class frmEmployee

 Private strFileName As String

 Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
  strFileName = InputBox("Please name the file you would like to save the data to: ")
 End Sub     
2
Dom Sinclair On

The problem with the approach you have there is that your variabl strFileName is scoped to the class frmEmployee.

You need to set up a genuinely global variable (at the application startup) an then use that. So you will need a Sub Main as an entry point to your application See here, and just before that create a public variable to hole the file name.

So you might have something like this for your application startup:

Public fileNametoUse as string

Public Sub Main()

    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New Form1)

End Sub

Then in you load sub for frmEmployee you would have:

fileNametoUse =  InputBox("Please name the file you would like to save the data to: ")