Link to home
Start Free TrialLog in
Avatar of jwr-isitech
jwr-isitechFlag for United States of America

asked on

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

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

SOLUTION
Avatar of patricka_0377
patricka_0377

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of jwr-isitech

ASKER

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?
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.'
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.