UDTs (as was vb6)

  • Thread starter Thread starter StrandElectric
  • Start date Start date
S

StrandElectric

Hello

Aftre producing good effective apps in vb6, I am trying to see if it is
worthwhile relearning for vb.net. I have vb express 2008.

One of the things I want to do is read old legacy random access files. I
used UDTs in vb6, where the string variables had various fixed lengths
making up fields concatenated to records.

Another thing I want to do is print out accouting reports with aligned
columns, using variables from my programs. I do NOT want to print out forms!
This was easy in vb6 using the printer object. How can I do it in dot.net?

How can I do this is vb.net?
 
HI again all

I see that I already enquired re both of these. I must confess I did not
understand the replies to the UDT problem. Also, it appears there were no
solutions to the print problem.

Cheers
 
StrandElectric wrote :
Hello

Aftre producing good effective apps in vb6, I am trying to see if it is
worthwhile relearning for vb.net. I have vb express 2008.

One of the things I want to do is read old legacy random access files. I used
UDTs in vb6, where the string variables had various fixed lengths making up
fields concatenated to records.

It is possible in VB.NET using the built in file io functions, but I
would suggest you not do this. It is way slow.

With the proliferation of file oriented databases - many of them a
single dll (such as SQLite), I would consider converting your files to
actual real databases.

If you feel you must continue to use standard files, then for all that
is good and right in the universe do not use VB.NET's native FileXXX
functions... Use the classes from System.IO.
Another thing I want to do is print out accouting reports with aligned
columns, using variables from my programs. I do NOT want to print out forms!
This was easy in vb6 using the printer object. How can I do it in dot.net?

How can I do this is vb.net?

Don't do much with printing, but I'm guessing you can do quite a bit
with the stuff from System.Drawing.Printing - and I would be suprised
if there wasn't some useless My.Computer.Printer crap that you could
use as well :)
 
Thanks Jason

None of that worked in 2008 Express. Neither did I find the printer
operation where someone else gave me URLs. I imagine that I may have been
given URLs that other people found via Google? I'd really like to hear from
anyone who has actually successfully done either the UDT thing and got a
printer to print nicely formatted reports bearing variables.

If these two things are not possible in dot.net, I really see no reason why
I should change from vb6. I'm willing to be convereted but at the moment
dot.net seems to have very considerable disadvantages.
 
StrandElectric wrote :
Thanks Jason

None of that worked in 2008 Express. Neither did I find the printer operation
where someone else gave me URLs. I imagine that I may have been given URLs
that other people found via Google? I'd really like to hear from anyone who
has actually successfully done either the UDT thing and got a printer to
print nicely formatted reports bearing variables.

If these two things are not possible in dot.net, I really see no reason why I
should change from vb6. I'm willing to be convereted but at the moment
dot.net seems to have very considerable disadvantages.

Random File Access (the easy, but incredibly slow way)

Module Module1

Structure FileRecord
Public anInteger As Integer
<VBFixedString(20)>
Public aString As String
<VBFixedString(50)>
Public anotherString As String
Public aSingle As Single
End Structure

Sub Main()
' create the randome access file
Dim hFile As Integer = FreeFile()
FileOpen(hFile, "testing.rnd", OpenMode.Random,
OpenAccess.Write)

Dim rec As FileRecord
With rec
.anInteger = 2
.aString = "hello, guy!"
.anotherString = "what?"
.aSingle = 1.3
End With
FilePut(hFile, rec)

With rec
.anInteger = 3
.aString = "hello, gal"
.anotherString = "who?"
.aSingle = 2.4
End With
FilePut(hFile, rec)
FileClose(hFile)

rec = New FileRecord
hFile = FreeFile()
FileOpen(hFile, "testing.rnd", OpenMode.Random,
OpenAccess.Read)
FileGet(hFile, rec, 2)
Console.WriteLine("*{0}*", rec.aString)
End Sub

End Module

Personally, I would abandon this method. Like I said in my earlier
reply, your going to get lots better results using a rdbms then using a
file like this. I think you are under the impression that this needs
to be something huge? No, check out sqlite - particularly

http://sqlite.phxsoftware.com/

It's a single dll, you reference in the references dialog and you have
a complete file based rdbms. With transaction support and everythign.
You can easily create the databases right in vs, though i actually
prefer to use the free sqlite administrator
(http://sqliteadmin.orbmu2k.de/)

But, whatever.. Don't know anything about printing except that it can
be done :)
 
Hi Tom

Thanks for your patience. In my existing vb6 application, I have as follows:

In a module

'AccountsFileModule
'
Option Explicit
Public FBU As DataBlock1
....etc
'-----------------------------------------------------------------------------------------
Public Type DataBlock1 ' company master information (FBU)
BusName As String * 30
Address1 As String * 30
Address2 As String * 30
Postcode As String * 6
Tel As String * 20
Fax As String * 20
Email As String * 30
RegForGST As String * 1
ABN As String * 15
GSTPeriod As String * 1
STS As String * 1
OpeningCapital As Currency ' 8
GSTRate As Currency ' 8
CodesAlreadySetFlag As String * 1
Spare1 As String * 54
End Type

and then in the forms where file processing is needed...

Get #1, 1, FBU
'Label showing all
Text1.Text = Trim(FBU.BusName)
Text2.Text = Trim(FBU.Address1)
Text3.Text = Trim(FBU.Address2)
Text4.Text = Trim(FBU.Postcode)
Text5.Text = Trim(FBU.Tel)
Text6.Text = Trim(FBU.Fax)
Text7.Text = Trim(FBU.Email)
Text8.Text = "Y" ' hardcoded
Text9.Text = Trim(FBU.ABN)
Text10.Text = "Q" ' hardcoded
Text11.Text = 10 ' hardcoded
Text12.Text = "Y" 'hardcoded
Text13.Text = FBU.OpeningCapital
End Sub
....and so on.

I am trying to ascertain how I should code that. We'll leave printing for
now until I have mastered the above. Most of what you have said is a lttle
over my head; I'm a real beginner!
...
 
StrandElectric said:
Thanks Jason

None of that worked in 2008 Express.

Are you saying that you can't create a reference to

Microsoft.VisualBasic.PowerPacks.Vs

and use

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

at the top of your module, in VS 2008 Express?
 
Tom said:
Structure FileRecord
Public anInteger As Integer
<VBFixedString(20)>
Public aString As String
<VBFixedString(50)>
Public anotherString As String
Public aSingle As Single
End Structure

That's simple. I think I knew about <VBFixedString(x)> at one time.
Thanks for the reminder. :-)
 
Jason Keats wrote :
That's simple. I think I knew about <VBFixedString(x)> at one time. Thanks
for the reminder. :-)

Of course, I should point out - that code won't work in 2008 :) In
2008, you would need to use line continuation characters after the
attributes:

Structure FileRecord
Public anInteger As Integer
<VBFixedString(20)> _
Public aString As String
<VBFixedString(50)> _
Public anotherString As String
Public aSingle As Single
End Structure

That's one nice improvement in VB10 - not as many _ :)
 
Jason Keats said:
That's simple. I think I knew about <VBFixedString(x)> at one time. Thanks
for the reminder. :-)

I can't make this work in Express 2008. How do I relate this to the 'Get' or
is it 'FileGet'?
 
Jason Keats said:
Are you saying that you can't create a reference to

Microsoft.VisualBasic.PowerPacks.Vs

and use

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

at the top of your module, in VS 2008 Express?

All that is over my head, Jason! What should I do exactly? Have a separate
module (not the form?) and put that 'Imports...' line at the top?
 
Hi both!

This is what I'm trying...

Public Class UDTExperiment

Public aEndOfFile As Integer

Public aTest As Integer

Public aRecords As Integer

Public RecLength As Long

Public aNameOfFile = "C:\CLAIMS\DATA\claimall.dat"

Dim Position As Integer

Public aRecordNo As Integer

Public FileRecord As Structure

Structure FileRecord

<VBFixedString(20)> _

Public aString As String

<VBFixedString(30)> _

Public anotherString As String

End Structure



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RecLength = 255

aRecordNo = 60

FileOpen(1, aNameOfFile, OpenMode.Random, , , RecLength) '### OK!

aRecords = FileLen(aNameOfFile) / RecLength '### OK! (calc not needed?);
name of fle m/be explicit

FileGet(1, FileRecord, aRecordNo)

LengthOfFile.Text = aRecords '### OK!

TextBox1.Text = aString

TextBox2.Text = anotherString

FileClose(1)

End Sub

End Class

I think I'm going wrong at the Dim stage and also the FileGet!.
 
I can't make this work in Express 2008. How do I relate this to the 'Get' or
is it 'FileGet'?

FileGet

And note, you will need the line continuation characters in 2008.
 
StrandElectric said:
All that is over my head, Jason! What should I do exactly? Have a separate
module (not the form?) and put that 'Imports...' line at the top?

Adding a reference is a basic skill.

Go to the "Solution Explorer" window. If References does not appear in
the treeview, then click the "Show All Files" icon.
Expand References.
If "Microsoft.VisualBasic.PowerPacks.Vs" does not appear in the list
then try to add it by right-clicking References and selecting "Add
Reference".
Scroll down in the .NET tab until you get find it - select it then click OK.

I don't have the Express version installed, so don't know if it's
available to you. It works fine in VS 2008 Pro.

If you can do the above then you will be able to print using (most of)
your old VB6 code.
 
StrandElectric said:
Hi both!

This is what I'm trying...

Public Class UDTExperiment

Public aEndOfFile As Integer

Public aTest As Integer

Public aRecords As Integer

Public RecLength As Long

Public aNameOfFile = "C:\CLAIMS\DATA\claimall.dat"

Dim Position As Integer

Public aRecordNo As Integer

Public FileRecord As Structure

Structure FileRecord

<VBFixedString(20)> _

Public aString As String

<VBFixedString(30)> _

Public anotherString As String

End Structure



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

RecLength = 255

aRecordNo = 60

FileOpen(1, aNameOfFile, OpenMode.Random, , , RecLength) '### OK!

aRecords = FileLen(aNameOfFile) / RecLength '### OK! (calc not needed?);
name of fle m/be explicit

FileGet(1, FileRecord, aRecordNo)

LengthOfFile.Text = aRecords '### OK!

TextBox1.Text = aString

TextBox2.Text = anotherString

FileClose(1)

End Sub

End Class

I think I'm going wrong at the Dim stage and also the FileGet!.

Some problems I see:
- most variables should normally be Private (not Public)
- aNameOfFile should be a string
- Public FileRecord As Structure is wrong
- your structure has a length of 50, but you've set RecLength = 255 !

First you need to create a structure that matches your file, claimall.dat.

If the records in claimall really are 255 long and match the VB6 Type
you mentioned previously, then start converting that to a Structure.

Note that Currency will become Decimal in .NET.

Tom has already provided working code. Why don't you refer to it to
learn how to declare a variable as a structure?
 
Jason Keats said:
Some problems I see:
- most variables should normally be Private (not Public)
- aNameOfFile should be a string
- Public FileRecord As Structure is wrong
- your structure has a length of 50, but you've set RecLength = 255 !

First you need to create a structure that matches your file, claimall.dat.

If the records in claimall really are 255 long and match the VB6 Type you
mentioned previously, then start converting that to a Structure.

Note that Currency will become Decimal in .NET.

Tom has already provided working code. Why don't you refer to it to learn
how to declare a variable as a structure?

Thanks. I was trying to read the first few bytes as an experiment rather
than coding the whole lot! I suppose I could read them all at once with a
dummy string 255 long. The records are definitely 255 long. My vb6 app using
the UDT works perfectly. Getting the whole string would show me I'm on the
right track.
 
and Jason, aNameofFile has worked successfully by naming the actual file.
One part of my app that *does* work is the part that looks at the length of
the file, as this divided by 255 shows the number of records that I know are
there!
 
Back
Top