To all
playing around with a code which output a string to a text file using the StreamWriter(). A the moment the code loops around a series of input, create some results, extract specific data, format the data and write the formatted data to a text file. Work like a charm (-ish)
In effect each loop create a text file which has a unique name using
Dim createdTime As DateTime = DateTime.Now
theFileName = LCase("" & theRespSimName & "_" & theEventName & "_" & Format(createdTime, "ddmmyy_hhmmss") & ".log")
I am thinking/playing around with the idea of having an (user) option to have only 1 output file written at the end. I have therefore modified the code so that the 'appendmode' is set based on the user's choice (if 1 output file wanted then append mode=True, else false)
The catch is that I do not want the file name to be something like "mycombineddata.txt" otherwise everytime the macro is run (and the file exists in the folder) it will append the data.
I am therefore thinking about a constant for the file name which is set at the 1st pass/loop, something like,
Private Const theFileNameFixed As String = LCase("combineddata_" & Format(createdTime, "ddmmyy_hhmmss") & ".log")
My thinking is that every time the code is run a unique combined file is created
The code does not like this syntax. What is the correct syntax to use (private!) constant?
Thanks
Regards
JXB
re: private constant
Rather than a constant, try using a variable. I don't think the compiler likes the fact that you are appending a variable value, createdTime, to the constant string value.
Private theFileNameFixed As String = LCase("combineddata_" & Format(createdTime, "ddmmyy_hhmmss") & ".log")
re: private constant - Thanks. Please delete discussion
Thanks NXJournaling for the tip. Could I request you delete the whole thread/discussion as some users believe the query was "inappropriate" in this forum. Much appreciated.
Thanks
Regards
A Const must be known at compile time
A Const variable must have a value that's known at compile time. The output of the "Format" function is not known at compile time, so that's why you get an error.
I don't see why you want your fileName to be "Const" anyway.