Solved

Trouble with converting a VB6 program to VB2008 - gives error in FileOpen statement.  Help?

Posted on 2008-10-16
6
784 Views
Last Modified: 2013-11-27
I am converting a piece of code (a whole working program) from Visual Basic 6 to Visual Basic Express 2008.  I am almost finished but cannot get my print module to work.  It seems to hang on the section where I am trying to Open a Port to print with and gives Error # 5.  Can anyone assist me here.  I know this has to be simple.  Everything else is working up to this.
Here's my code:

Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Print_Form
	Inherits System.Windows.Forms.Form
	Private Structure LabelRecord ' Create user-defined type.
		<VBFixedString(50),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=50)> Public Id() As Char ' Name of tag
		<VBFixedString(50),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=50)> Public Value() As Char 'Value of Tag
	End Structure
	
	Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
		Me.Hide()
		Main_Screen_V3.Show()
	End Sub
	
	Private Sub Print_Form_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		Main_Screen_V3.Show()
	End Sub
	
	Private Sub Printit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Printit.Click
        Dim TextLine As String = ""
        Dim TheDate As Date
        Dim TheTime As Date
        Dim I As Integer
        Dim Description As String = ""
        Dim Part_Number As String = ""
        Dim ALC_Code As String = ""
        Dim Barcode_Data As String = ""
        Dim Job_Code As String = ""
		
		'
		' Setup to Handle Errors
		'
		ErrorLabel.Text = ""
		On Error GoTo ErrorHandler ' Enable error-handling routine.
		
		'
		' Build the Text Label
		'
		TheDate = VB6.Format(Now, "mm/dd/yyyy") ' Assign current system date and time.
		TheTime = VB6.Format(Now, "hh:mm AMPM")
		
        FileOpen(3, "LPT1", OpenMode.Output) ' Open Printer Port
		
		'
		' Read Label Tags
        '
 
        TextLine = "^XA"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^JM8"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^MD16"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^LH15,15"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO40,20^ADN,35,15^FD"
        TextLine += Main_Screen_V3.m_dtLabelInfo.Rows(Main_Screen_V3.m_rowPosition)("Description").ToString()
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO100,100^ADN,35,15^FD"
        TextLine += Main_Screen_V3.m_dtLabelInfo.Rows(Main_Screen_V3.m_rowPosition)("Job_Code").ToString()
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO250,100^ADN,35,15^FD"
        TextLine += Main_Screen_V3.m_dtLabelInfo.Rows(Main_Screen_V3.m_rowPosition)("ALC_Code").ToString()
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO450,100^ADN,35,15^FD"
        TextLine += Main_Screen_V3.m_dtLabelInfo.Rows(Main_Screen_V3.m_rowPosition)("Part_Number").ToString()
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO315,150^BC,125,Y,N,N,N^FD"
        TextLine += Main_Screen_V3.m_dtLabelInfo.Rows(Main_Screen_V3.m_rowPosition)("Barcode_Data").ToString()
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO100,325^ADN,35,15^FD"
        TextLine += TheDate
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^FO450,325^ADN,35,15^FD"
        TextLine += TheTime
        TextLine += "^FS"
        PrintLine(3, TextLine) ' Send to printer
        TextLine = "^XZ"
        PrintLine(3, TextLine) ' Send to printer
        FileClose(3) ' Close Printer Port.
        Exit Sub ' Exit to avoid handler.
 
ErrorHandler:  ' Error-handling routine.
        Select Case Err.Number ' Evaluate error number.
            Case 53 ' "File Not found" error.
                ErrorLabel.Text = "Label Definition File Not Found"
            Case 57
                ErrorLabel.Text = "Error writing to printer port"
            Case Else
                ' Handle other situations here...
                ErrorLabel.Text = Err.Number
        End Select
        Exit Sub ' Let user try again
	End Sub
End Class

Open in new window

0
Comment
Question by:jwr-isitech
[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
  • 3
  • 2
6 Comments
 
LVL 1

Assisted Solution

by:patricka_0377
patricka_0377 earned 100 total points
ID: 22733464
I would use StreamWriter instead
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22733885
Printing in .Net is usually not so close to the hardware as it sometimes was in VB6.

The usual method is to instantiate a PrintDocument and "draw" text or objects on it.

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx
0
 

Author Comment

by:jwr-isitech
ID: 22734098
Isn't StreamWriter used for file creation only?  I need to embed some data from a datatable into my text stream and send it to my printer.  Is "LPT1" valid for the filename?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jwr-isitech
ID: 22739773
I am not grasping something.  I cannot get the information out of this program and onto paper.  What do I need to do to get a line of text onto paper?  I don't get the StreamWriter command yet.  Can someone explain a way to setup and use it for my case?

The more I try to code this, the more things that seem to need to be declared.  It is driving me 'batty.'
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 400 total points
ID: 22739876
OK, advanced articles are at http://www.vbdotnetheaven.com/Articles/ArticleListing.aspx?SectionID=2&SubSectionID=117 ... but for a basic example ...

This is simplified from the MSDN section I first linked: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx


                Dim pd As New PrintDocument()                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage                pd.Print()    ' The PrintPage event is raised for each page to be printed.    Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)  ev.Graphics.DrawString ("Hello World")End Sub
0
 

Author Closing Comment

by:jwr-isitech
ID: 31506845
I used the information from both of your inputs.  I have been able to get the project going and actually got the output.  I have also discovered the areas of my weakness in OOP.  Many thanks for your directions.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…

726 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