[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Working with files using Visual Basic

Posted on 2004-10-19
14
Medium Priority
?
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 86

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 500 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

649 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