Solved

Visual Basic Help

Posted on 2013-01-03
9
319 Views
Last Modified: 2013-10-04
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
0
Comment
Question by:sharris_glascol
  • 7
  • 2
9 Comments
 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
And of course,  the full file name and path would be inside quotes, for example, "C:\Users\sharris\Desktop\test.txt"
0
 

Author Comment

by:sharris_glascol
Comment Utility
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) )

Value = objFile.Readline
objFile.Close

set objFile = Nothing
set objFSO = Nothing
0
 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
Looks good to me - what happens when you run it? If you get an error, what is the error message?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
OK, wait, I see some problems even before you run it.  I think the file name needs to be inside quotes, i.e., "O:\Labels\new\Serial\SN.txt".

Also, the last one has double parentheses.
0
 

Author Comment

by:sharris_glascol
Comment Utility
yep it works!!!!!  THANKS
0
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
Comment Utility
I tested this and it displayed the first line in the file in a message box.

name = "SN"
Dim objFSO
Dim objStream

Set objFSO = CreateObject("Scripting.FilesystemObject")

If NOT objFSO.FileExists("C:\Users\mbd\Desktop\test.txt") Then
      Set objFile = objFSO.CreateTextFile("C:\Users\mbd\Desktop\test.txt")
      objFile.WriteLine "0"
      objFile.Close
      set objFile = Nothing
End If

Set objFile = objFSO.OpenTextFile("C:\Users\mbd\Desktop\test.txt")

Value = objFile.Readline
objFile.Close

MsgBox(Value)

set objFile = Nothing
set objFSO = Nothing
0
 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
I didn't see that it worked before I posted that last comment.  Glad you got it working!
0
 
LVL 11

Expert Comment

by:MajorBigDeal
Comment Utility
BTW,  since it is working now, can you go ahead and accept the answer or answers that helped you the most?  Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 87
scoresAverage challenge 8 74
White board coding practice 3 60
parse convert xml feed to text (python) 2 26
This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now