I am using a barcode software that a company gave some visual basic code to auto-generate a serial number via a file it reference. I have the code listed below but I want to be able to change the path of where the file is located. I am not familiar enough with visual basic to do this so I need some help. The code is below:
'Called whenever this sub string needs to be displayed on the screen.
name = "SN"
Dim objFSO
Dim objStream
Set objFSO = CreateObject("Scripting.FilesystemObject")
If NOT objFSO.FileExists(Format.Directory & "\" & name & ".txt") Then
Set objFile = objFSO.CreateTextFile(Format.Directory & "\" & name & ".txt")
objFile.WriteLine "0"
objFile.Close
set objFile = Nothing
End If
Set objFile = objFSO.OpenTextFile(Format.Directory & "\" & name & ".txt")
Value = objFile.Readline
objFile.Close
set objFile = Nothing
set objFSO = Nothing
Programming
Last Comment
MajorBigDeal
8/22/2022 - Mon
MajorBigDeal
There are 3 places in this script where the file name is specified.
1. In the If statement where it checks to see if the file exists.
2. Under the if statement where it creates the file if it does NOT already exist.
3. After the "end if" statement where is opens the file and tries to read a line.
So in each of these 3 locations, you could remove the stuff between parentheses (starts with Format and ends with .txt) and the replace it with the full path and name of the file you are trying to reference.
You need to make sure that all 3 locations are the same, so it might be a good idea to put the file name in a variable and then just use that variable in all three places. That way you would only have to change it in one place from then on.
MajorBigDeal
And of course, the full file name and path would be inside quotes, for example, "C:\Users\sharris\Desktop\test.txt"
sharris_glascol
ASKER
Is this correct below?
If NOT objFSO.FileExists(O:\Labels\new\Serial\SN.txt) Then
Set objFile = objFSO.CreateTextFile((O:\Labels\new\Serial\SN.txt) )
objFile.WriteLine "0"
objFile.Close
set objFile = Nothing
End If
Set objFile = objFSO.OpenTextFile((O:\Labels\new\Serial\SN.txt) )
1. In the If statement where it checks to see if the file exists.
2. Under the if statement where it creates the file if it does NOT already exist.
3. After the "end if" statement where is opens the file and tries to read a line.
So in each of these 3 locations, you could remove the stuff between parentheses (starts with Format and ends with .txt) and the replace it with the full path and name of the file you are trying to reference.
You need to make sure that all 3 locations are the same, so it might be a good idea to put the file name in a variable and then just use that variable in all three places. That way you would only have to change it in one place from then on.