Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2007, 09:17 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting Data from a VB5 Random File

I have a file created in VB5 containing data I need to retrieve in VB.net
The file was created using the Type statement below and a Put statement after the file is opened for Random.

Public Type OWNER
    RecordNum As Integer
    DateOfPurch As Long
    RegNum As String * 9
    UsesRemaining As Integer
    VersionNum As Integer
    DiskSize As String * 2
    DiskCount As Integer
    DateMade As String * 3
    VersionName As String * 4
End Type

I've tried numerous approaches that I found in the various Help systems but none work.

Can anyone help me with VB.NET code that will get the various elements from the file created by the VB5 Put statement.

Thanks.

 
Old April 16th, 2007, 02:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You get the data back into a variable of the Type OWNER, and then the elements o that “gotten” variable will have the data in the various elements.
 
Old April 16th, 2007, 09:00 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unfortunately I don't understand your reply.

 
Old April 17th, 2007, 02:05 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

VB 6 code:

Given
Code:
Type Employee
  ID         As Integer
  FirstName  As String * 10
  LastName   As String * 10
  Department As Integer
  HireDate   As Date
  Salary     As Currency
End Type

Function ReadEmp(strFile As String, emp As Employee) As Boolean

  Dim hFile As Long
  Dim empIn As Employee

  ' Open file for random access
  hFile = FreeFile
  Open strFile For Random Access Read Write _
               As hFile Len = Len(empIn)

  ' Try to find the employee in existing records
  Do Until EOF(hFile)

    ' Read in the record
    Get hFile, , empIn

    ' Check IDs
    If empIn.ID = emp.ID Then
      emp = empIn
      ReadEmp = True
      Exit Do
    End If
  Loop

  Close hFile

End Function

Function SaveEmp(strFile As String, empToSave As Employee) As Boolean

  Dim hFile  As Long
  Dim empIn  As Employee
  Dim lngRec As Long

  ' Open the file for random access
  hFile = FreeFile
  Open strFile For Random Access Read Write _
               As hFile Len = Len(empIn)

  ' Try to find employee in existing rec.s
  Do Until EOF(hFile)
    lngRec = lngRec + 1

    ' Read in the record
    Get hFile, lngRec, empIn

    ' Check IDs
    If empIn.ID = empToSave.ID Then

      ' Write the new data & get out
      Put hFile, lngRec, empToSave
      GoTo ExitHere
    End If
  Loop

  ' Record doesn't exist so write at end
  Put hFile, , empToSave

ExitHere:
  dhSaveEmp = True
  Close hFile

End Function
But .NET is different. Structures are used instead of types, and the string lengths are established with attributes:
Code:
Structure Employee_Data
        Dim BirthDate As Date
        <VBFixedString(3)> Dim Title As String
        <VBFixedString(20)> Dim LastName As String
        <VBFixedString(20)> Dim FirstName As String
        <VBFixedString(40)> Dim Address As String
        <VBFixedString(20)> Dim City As String
        <VBFixedString(2)> Dim State As String
        <VBFixedString(10)> Dim ZIP As String
        <VBFixedString(20)> Dim Phone As String
        <VBFixedString(20)> Dim JobTitle As String
        Dim Salary As Decimal
        Dim StartDate As Date
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        Dim employee As Employee_Data
        Dim filenum As Integer
        Dim filename As String
        Dim temp As String

        'set the textbox font to fixed
        Me.Font = New Font("Courier New", 10)

        'find a free file number
        filenum = FreeFile()

        'set the filename
        filename = Directory.GetCurrentDirectory() & "\Employee.dat"

        'open the file
        FileOpen(filenum, filename, OpenMode.Random, OpenAccess.ReadWrite, _
            OpenShare.Default, Len(employee))

        'fill the employee record
        With employee
            .Title = "Mr."
            .LastName = "Smith"
            .FirstName = "John"
            .BirthDate = CDate("04/01/1980")
            .Address = "1234 Stupendous Street"
            .City = "Washington"
            .State = "DC"
            .ZIP = 10001
            .Phone = "212-555-0101"
            .JobTitle = "District Manager"
            .Salary = 75000
            .StartDate = CDate("01/01/2002")
        End With

        'write the record
        FilePut(filenum, employee, 1)

        'close the file
        FileClose(filenum)

        'open the file again
        FileOpen(filenum, filename, OpenMode.Random, _
            OpenAccess.ReadWrite, OpenShare.Default, Len(employee))

        'read the first record
        FileGet(filenum, employee)

        'display the record
        With employee
            'employee name
            temp = "Employee name".PadRight(15) & ": "
            temp &= .Title.Trim() & " " & FirstName.Trim()
            temp &= " " & .LastName.Trim() & vbCrLf
            'job title
            temp &= "Job title".PadRight(15) & ": "
            temp &= .JobTitle.Trim() & vbCrLf
            'birth date
            temp &= "Birth date".PadRight(15) & ": "
            temp &= .BirthDate & vbCrLf
            'address
            temp &= "Address".PadRight(15) & ": "
            temp &= .Address.Trim() & vbCrLf
            'city,state,zip
            temp &= "City,State,Zip".PadRight(15) & ": "
            temp &= .City.Trim() & ", " & .State.Trim()
            temp &= " " & .ZIP.Trim() & vbCrLf
            'phone
            temp &= "Phone number".PadRight(15) & ": "
            temp &= .Phone.Trim() & vbCrLf
            'salary
            temp &= "Salary".PadRight(15) & ": "
            temp &= Format(.Salary, "Currency") & vbCrLf
            'start date
            temp &= "Start date".PadRight(15) & ": "
            temp &= .StartDate
        End With
        TextBox1.Text = temp

        'remove the text selection
        TextBox1.Select(0, 0)

        'close the file
        FileClose(filenum)
    End Sub
 
Old April 17th, 2007, 09:41 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As with virtually every other method I have tried this one works fine if you create the file ("employee.dat" in the case of your example) in VB.NET and then open it to retrieve the data in VB.NET.

However, that is not what I'm trying to do. The file was created in VB5 and I'm trying to open it in VB.NET. Your code runs OK (without any unhandled exceptions) but it doesn't give anything close to the correct results. Where I should have:
"6303CD0001701WLPg" for a result,
I get:
"538976288 0000 "
instead.

Do you have any other suggestions?

Thanks, Lowell


 
Old April 18th, 2007, 01:31 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I think that the structure of your data using the proffered code is not the same as the storage of the data in your existing file that you need to work with.

Try creating a file with the posted code (which you say works), and compare the data that results therein against the data that you need to work with in something like NotePad.EXE.
 
Old April 18th, 2007, 11:36 PM
Registered User
 
Join Date: Apr 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I don't got your point the problem is i want to display all the file and folders of "C:\" drive for this i am using the following code

for each strfile as string in my.computer.filesystem.getdirectories("c:\ ")
listbox1.items.add(strfile)
next



The error massage coming while compiling this code " Name my not declared "

please guide me how i declare "my" namespace



Thanks
Ajay Kumar



ajay
 
Old April 19th, 2007, 10:12 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Ajay,

Don't hijack threads.
You have something that you are trying to do, and you are having something not work right.
So you have come to the right place, but you need to post your [u]own</u> question.

The subject would be something like: "Name My" not declared error

When posting the literal code, click the “Insert Code” button (has a “#” on its face), and post your code between the [ code] and [/ code] tags.
 
Old April 23rd, 2007, 06:25 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been working on this for a while without success. The file I'm trying to read when looked at in Notepad looks like this:
[][][][][][] .[]Ÿ[]CD[][]701WLPg
where "[]" represents a box that I couldn't duplicate on this forum.

I can get the "." and the "701WLPg" to come up if I change all the parameters to String. But I can't get the numbers represented by the boxes to come up.

Got any other ideas?

Thanks

 
Old April 24th, 2007, 10:35 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Those boxes are values in the file that NotePad doesn’t recognize as printable. This is to be expected when the file contains data that are not string-type.

Integers are stored as two bytes. If you had a file where the first two bytes make up an integer, then if your structure that you are reading into has an Integer as its 1st element that Integers value will be established by the 1st two bytes during the reading process, moving the file-reading pointer to the 3rd byte. (Just describing the machinery; you don't really have control over this.)

So let’s say your file's format was an integer, a 5-character string, and a Long integer. If I use i for a byte of an integer, s for a string byte and L for a long byte, the order of the bytes in the file for the 1st record would be iisssssLLLL and would most likely look like [][]HELLO[][][][] in NotePad.

The structure to read that would have to be
Code:
    Structure <whatever>
        Dim FirstVal As Integer
        <VBFixedString(5)> Dim SecondVal As String
        Dim ThirdVal As Long
    End Structure
    or
Code:
Type <whatever>
Code:
    Dim FirstVal As Integer
    Dim SecondVal As String * 5
    Dim ThirdVal As Long
End Type
Now, as you have seen, reading data directly is often less than fruitful. But if you create a file with your structure, where the data is the same as what you are expecting to read (both the format and the values of the fields of that format), then you can open both files in Notepad and see if the scrambled mess looks the same. If not, then you have the structure wrong.

This is a 1st-pass validation of the machinery you're building. You certainly will never get meaningful results if the structure you are reading into does not match—ex[u]act</u>ly—the structure of the data you are trying to read.

So the gobbledy-gook you posted is not something you can easily read, but it can be used in a meaningful way to test the code you are writing.

Do you have access to the process that creates the files you are trying to read?

If you have access at the level that will let you create files only, that can be useful because you can write one spate of data, look at the resultant file, then change one piece and do it again to see where the change was manifested.

If you have access to the actual code, even better. If that's the case you can see what the format is of the data being written. That should help you write the structure to recieve the data. Plus you’ll be able to see if the code has situations under which it fails to be consistent in the form it writes data.

If it usually puts two bytes at the start of a record, but when the value is zero it writes nothing (as an example), you will never be able to retrieve these data with the paradigm you are using (a structure with a Get() statement). That would be good to know before you waste your life away trying to retrieve data in a way that cannot work.





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB5.0 to VB .Net upgradation issue related to DAO setu VB How-To 1 October 10th, 2007 03:15 PM
display random items from xml file rahimzai XML 2 November 26th, 2006 06:47 PM
Help: dropdownlist posting back gives random data vickeyurs ADO.NET 5 September 3rd, 2005 09:28 AM
Do VB5 and Access 2003 play together? zontar Beginning VB 6 1 March 8th, 2005 09:58 AM
random file generation Ashleek007 Beginning PHP 1 February 24th, 2005 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.