[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Working with files using Visual Basic

I have a txt file set up with 2 fields.  A product number and a blox number.  Now, when I try to pull that information from the txt file to be used in Visual Basic something goes wrong.  The first record pulls perfect, but after that I'm getting a lot of junk data pulling incorrectly.  Below is my code.

Module :

Type Blox
  ProdNum As String * 18
  'BloxNum As String
End Type

Command button:

Private Sub cmdRead_Click()

Dim LineData As String
Dim Prod As String
Dim Blx As String
Dim BloxR As Blox
Dim RecLength As Long
Dim Position As Integer

RecLength = 18
Position = 1

Open "C:\blox1.txt" For Random As 1 Len = RecLength


Do Until EOF(1)
 
  Get 1, Position, BloxR
  LineData = BloxR.ProdNum '& " " & BloxR.BloxNum
  Prod = Left(LineData, 5)
  Blx = Right(LineData, 12)
  Select Case Prod
    Case "00225"
      lblProcess.Caption = "Marination"
    Case "00223"
      lblProcess.Caption = "Marination"
    Case "02214"
      lblProcess.Caption = "Marination"
    Case "02223"
      lblProcess.Caption = "Marination"
    Case "24550"
      lblProcess.Caption = "Grinding"
    Case "24551"
      lblProcess.Caption = "Marination"
    Case "25550"
      lblProcess.Caption = "FrankoMatic"
    Case "25551"
      lblProcess.Caption = "Grinding"
    Case "25552"
      lblProcess.Caption = "Grinding"
    Case "25560"
      lblProcess.Caption = "Grinding"
    Case "25561"
      lblProcess.Caption = "Grinding"
    Case "35810"
      lblProcess.Caption = "FrankOMatic"
    Case "43963"
      lblProcess.Caption = "Peel"
    Case "45550"
      lblProcess.Caption = "Peel"
    Case "45552"
      lblProcess.Caption = "Peel"
    Case "45553"
      lblProcess.Caption = "Peel"
  End Select
  txtProdNum(0).Text = Prod
  txtBloxNum(1).Text = Blx
  lblCheck.Caption = LineData
  Timer1.Enabled = True
  Position = Position + 1
 
 
 
Loop

txtProdNum(0).Text = "All Done"

End Sub



The Timer1.Enabled line is just there so that I can put a tab stop for debugging purposes.  Any help would be much appreciated.  


0
Rikol
Asked:
Rikol
  • 7
  • 3
  • 2
  • +2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is how you read/write to the file:

Option Explicit

Private Type Blox
    ProdNum As String * 5
    BloxNum As String * 12
End Type

Private fileName As String

Private Sub Form_Load()
    fileName = App.Path & "\blox1.txt"
End Sub

Private Sub Command1_Click()
   
    ' create a test file to work with
   
    Dim i As Integer
    Dim b As Blox
   
    Open fileName For Random As #1 Len = Len(b)
    For i = 1 To 5
        b.ProdNum = "Item" & i
        b.BloxNum = "BloxNumber_" & i
        Put #1, , b
    Next i
    Close #1
End Sub

Private Sub Command2_Click()

    ' read in the file

    Dim b As Blox
   
    Open fileName For Random As #1 Len = Len(b)
    While Not EOF(1)
        Get #1, , b
        Debug.Print b.ProdNum, b.BloxNum
    Wend
    Close #1
End Sub
0
 
RikolAuthor Commented:
Here is some of the data in the file.

00225,094281071852
25560,094287131965
35810,094288162037
25560,094241060642
25553,094257061148
25560,094257131214
25560,094259141309

This is what gets returned:

1st pass : Prod = 00225  Blx = 0942281071852
2cd pass: Prod = [][]255  Blx = 0,0942871319

Where [] is equal to a small square.
0
 
RikolAuthor Commented:
I've had some success with deleting and putting the data all on one line.  That might be my problem.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RikolAuthor Commented:
Ok, that got it.  Heh.  Sorry about that and thanks for your time.
0
 
Michael_DCommented:
Try this:

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(sFileName)
Set ts = f.OpenAsTextStream(1, -2)

Do While ts.AtEndOfStream = False
    Temp = split(ts.ReadLine,",")
    Debug.Print "ProdID="  & Temp(0)
    Debug.Print "Blox="  & Temp(1)
Loop

This way you can store your data separated by lines as it was originally.

Good luck
0
 
tonsofpcsCommented:
What does timer1 do? You keep enabling it for each run through, but I don't see it stopping (or what it does).  
0
 
RikolAuthor Commented:
I put a tab stop to the left of the timer1.enable option so that it stops on each run through.  It's just for debugging purposes.

0
 
fds_fatboyCommented:
The two small squares were carriage return (character 0x0D) and line feed (0x0A) This structure would have allowed you to use your initial multi-line field:

Private Type Blox
    ProdNum As String * 5
    bloxComma As String * 1
    BloxNum As String * 12
    bloxCRLF As String * 2
End Type

When reading you would ignore bloxComma and bloxCRLF

When writing one of these structures to the file, you would
    blox.bloxComma = ","
    bloxCRLF = vbCRLF

Ah Random access files with dummy data this takes me back to COBOL and the FILLER statement ;-)

Nostalgia - it ain't what it was.
0
 
RikolAuthor Commented:
Hehe.  I don't even remembe the FILLER statement in COBOL and I took 3 classes on it.  *shakes head*  Haven't used it in so long I've lost it.  Working for Help Desks doesn't keep your programming skills honed very well.  lol
0
 
Michael_DCommented:
Keep practicing here on EE :)
0
 
tonsofpcsCommented:
You don't actually need a timer, in fact, its a waste of processor.  You could just put a 'doevents' call, or a simple comment and set a break point there.  
0
 
RikolAuthor Commented:
Just a FYI, you can't put a breakpoint on a comment line in VB.
0
 
Michael_DCommented:
But you COULD put breakpoint here


bp>  lblCheck.Caption = LineData
        'Timer1.Enabled = True
bp>   Position = Position + 1

One thing is for sure you DON'T NEED a timer for this reason
0
 
RikolAuthor Commented:
I know I don't NEED a timer.  :)  Was just going to use one for something else and changed my mind so I was using it as a breakpoint.  :)

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now