Solved

Working with files using Visual Basic

Posted on 2004-10-19
14
588 Views
Last Modified: 2013-12-26
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
Comment
Question by:Rikol
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 1

Author Comment

by:Rikol
Comment Utility
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
 
LVL 1

Author Comment

by:Rikol
Comment Utility
I've had some success with deleting and putting the data all on one line.  That might be my problem.
0
 
LVL 1

Author Comment

by:Rikol
Comment Utility
Ok, that got it.  Heh.  Sorry about that and thanks for your time.
0
 
LVL 13

Expert Comment

by:Michael_D
Comment Utility
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
 
LVL 8

Expert Comment

by:tonsofpcs
Comment Utility
What does timer1 do? You keep enabling it for each run through, but I don't see it stopping (or what it does).  
0
 
LVL 1

Author Comment

by:Rikol
Comment Utility
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
IT, Stop Being Called Into Every Meeting

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 10

Accepted Solution

by:
fds_fatboy earned 125 total points
Comment Utility
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
 
LVL 1

Author Comment

by:Rikol
Comment Utility
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
 
LVL 13

Expert Comment

by:Michael_D
Comment Utility
Keep practicing here on EE :)
0
 
LVL 8

Expert Comment

by:tonsofpcs
Comment Utility
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
 
LVL 1

Author Comment

by:Rikol
Comment Utility
Just a FYI, you can't put a breakpoint on a comment line in VB.
0
 
LVL 13

Expert Comment

by:Michael_D
Comment Utility
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
 
LVL 1

Author Comment

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

16 Experts available now in Live!

Get 1:1 Help Now