Solved

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

Posted on 2008-10-16
6
778 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now