Solved

Working with files using Visual Basic

Posted on 2004-10-19
14
594 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
ID: 12349560
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
ID: 12349694
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
ID: 12349795
I've had some success with deleting and putting the data all on one line.  That might be my problem.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Comment

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

Expert Comment

by:Michael_D
ID: 12350199
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
ID: 12352995
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
ID: 12354430
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
 
LVL 10

Accepted Solution

by:
fds_fatboy earned 125 total points
ID: 12356583
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
ID: 12357155
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
ID: 12357874
Keep practicing here on EE :)
0
 
LVL 8

Expert Comment

by:tonsofpcs
ID: 12365405
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
ID: 12371097
Just a FYI, you can't put a breakpoint on a comment line in VB.
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 12373239
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
ID: 12376171
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

773 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