I have some code that I think should print to my brother printer but it appears to do absolutely nothing. It is driving me mad!!
Anyone any ideas at all please
Dim PrinterName As String = ComboBox1.Text
If PrinterName.Contains("Brother") Then
Dim objDoc As bpac.Document
objDoc = CreateObject("bpac.Document")
Dim STRlbl As String = "C:\BarcodeScans\Application\Label.lbx"
If (objDoc.Open("C:\BarcodeScans\Application\Label.lbx")) Then
objDoc.GetObject("objCompany").Text = Txtbox1.Text
objDoc.GetObject("objName").Text = Txtbox2.Text
objDoc.SetPrinter(PrinterName, True)
objDoc.StartPrint(STRlbl, PrintOptionConstants.bpoDefault)
objDoc.PrintOut(1, PrintOptionConstants.bpoDefault)
objDoc.EndPrint()
objDoc.Close()
End If
It doesn't fail or error,Steps through every line as it should, it just does not print.
The label/template prints fine from my PC connected direct to the Brother printer via usb. Printer is a PT-9700PC
I'm stumped.
Imports bpac
Imports System.Drawing.Printing
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim pkInstalledPrinters As String
' Find all printers installed
For Each pkInstalledPrinters In _
PrinterSettings.InstalledPrinters
ComboBox1.Items.Add(pkInstalledPrinters)
Next pkInstalledPrinters
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim PrinterName As String = ComboBox1.Text
Debug.Print(PrinterName.ToString)
If PrinterName.Contains("Brother") Then
Dim objDoc As bpac.Document
objDoc = CreateObject("bpac.Document")
Dim STRlbl As String = "C:\Users\...\UPC-A.lbx"
If (objDoc.Open(STRlbl)) Then
objDoc.GetObject("tUPC-A").Text = TxtBox1.Text
objDoc.GetObject("tRetail").Text = TxtBox2.Text
objDoc.GetObject("tBarCode").Text = TxtBox3.Text
objDoc.SetPrinter(PrinterName, True)
objDoc.StartPrint(STRlbl, PrintOptionConstants.bpoDefault)
objDoc.PrintOut(1, PrintOptionConstants.bpoDefault)
objDoc.EndPrint()
objDoc.Close()
End If
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text <> "" Then
Button1.Enabled = True
End If
End Sub
End Class
Related
I have some code which reads in a tab delimited file where cell reference B2 matches the reference in the first column in the tab delimited file. This works fine where the text file is small. The below works on a sample file with aa bb and cc as the headers with dummy data.
Option Explicit
Sub TestImport()
Call ImportTextFile(Sheet1.Range("B1"), vbTab, Sheet2.Range("A4"))
End Sub
Public Sub ImportTextFile(strFileName As String, strSeparator As String, rngTgt As Range)
Dim lngTgtRow As Long
Dim lngTgtCol As Long
Dim varTemp As Variant
Dim strWholeLine As String
Dim intPos As Integer
Dim intNextPos As Integer
Dim intTgtColIndex As Integer
Dim wks As Worksheet
Set wks = rngTgt.Parent
intTgtColIndex = rngTgt.Column
lngTgtRow = rngTgt.Row
Open strFileName For Input Access Read As #1
While Not EOF(1)
Line Input #1, strWholeLine
varTemp = Split(strWholeLine, strSeparator)
If CStr(varTemp(0)) = CStr(Range("B2")) Then
wks.Cells(lngTgtRow, intTgtColIndex).Resize(, UBound(varTemp) + 1).Value = varTemp
lngTgtRow = lngTgtRow + 1
End If
Wend
Close #1
Set wks = Nothing
End Sub
I am trying to get the below bit of code to work using ADO as this will run much faster on a text file with a couple of million records however I am getting an error on the '.Open str' part of the code (no value given for one or more required parameters).
It looks like it is to do with how I am defining the string- could you review and see if there is something I am missing...?
Sub QueryTextFile()
t = Timer
Dim cnn As Object
Dim str As String
Set cnn = CreateObject("ADODB.Connection")
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn.ConnectionString = "Data Source=C:\Users\Davids Laptop\Documents\Other Ad Hoc\Test Files\;Extended Properties=""text;HDR=Yes;FMT=Delimited;"""
cnn.Open
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
str = "SELECT * FROM [test1.txt] WHERE [aa]=" & Chr(34) & Range("B2") & Chr(34)
With rs
.ActiveConnection = cnn
.Open str
Sheet1.Range("A4").CopyFromRecordset rs
.Close
End With
cnn.Close
MsgBox Timer - t
End Sub
I am stuck here. I can't get the solution to print out into the listbox. I just can't find out what I am missing. I need help. Anything you see would be a big help. This is all the code from my program:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For years As Integer = 3 To 20
Lifebox.Items.Add(years.ToString)
Next years
Lifebox.SelectedIndex = 0
End Sub
Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
Me.Close()
End Sub
Private Sub DisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayButton.Click
Dim cost As Double
Dim life As Double
Dim period As Double
Dim numberperiod As Double
Dim salvage As Double
Dim depreciation As Double
Dim isConvertedCost As Boolean
Dim isConvertedLife As Boolean
Dim isConvertedSalvage As Boolean
isConvertedCost = Double.TryParse(Assetbox.Text, cost)
isConvertedLife = Double.TryParse(Lifebox.Text, life)
isConvertedSalvage = Double.TryParse(SalavageBox.Text, salvage)
For numberperiod = 1 To period Step 1
depreciation = Financial.DDB(cost, salvage, life, numberperiod)
DepBox.Text += numberperiod.ToString & " -> " & Convert.ToString(depreciation) _
& ControlChars.NewLine
Next numberperiod
If isConvertedCost AndAlso isConvertedLife AndAlso isConvertedSalvage Then
DepBox.Text = " Year Depreciation "
Else
MessageBox.Show("The cost, life, and salvage values must be numeric.",
"Sonheim Manufacturing Company", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Assetbox.Focus()
End If
End Sub
End Class
I have a List Box which have certain dimentions
Some of the Items are loger than the width of the ListBox control.
When I pass the mouse over these Items I want to see all the name.
I'm using vb.net
Try this:
Private Sub ListBox1_MouseMove (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
Dim ListMousePosition As Point = Me.ListBox1.PointToClient(Me.MousePosition)
Dim itemIndex As Integer = Me.ListBox1.IndexFromPoint(ListMousePosition)
If itemIndex > -1 Then
Dim s As String = Me.ListBox1.Items(itemIndex).ToString()
Dim g As Graphics = Me.ListBox1.CreateGraphics()
If g.MeasureString(s, Me.ListBox1.Font).Width > Me.ListBox1.ClientRectangle.Width Then
Me.ToolTip.SetToolTip(Me.ListBox1, s)
else
Me.ToolTip.SetToolTip(Me.ListBox, "")
End If
g.Dispose()
End If
End Sub
I want to send my SSRS report directly to Printer. For that I have rendered my report into array of bytes and then sent these array of Bytes to Printer.
When printing the report from SSRS print button the size of the report printed is exactly the size of the report. But the problem is: when printed through my code the size of the report is not correct.
Code to generate the bytes:
Private Function ExporttoPrinter()
'Start : Code to Check the Page Count -- Binit
Dim deviceInfo As String
Dim format As String = "IMAGE"
Dim encoding As String = ""
Dim mimeType As String = ""
Dim extension As String = ""
Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim bytes As Byte() = New Byte(0) {&H0}
Dim numberOfPages As Integer = 1
Dim pages As Byte()() = New [Byte](-1)() {}
Dim currentPageStream As Byte() = New Byte(0) {&H0}
While currentPageStream.Length > 0
deviceInfo = String.Format("<DeviceInfo><OutputFormat>EMF</OutputFormat><PrintDpiX>200</PrintDpiX><PrintDpiY>200</PrintDpiY> <StartPage>{0}</StartPage><PageWidth>15in</PageWidth><PageHeight>12in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>0in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>", numberOfPages)
currentPageStream = ReportViewer1.ServerReport.Render(format, deviceInfo, mimeType, encoding, extension, streamids, warnings)
If currentPageStream.Length = 0 AndAlso numberOfPages = 1 Then
'renderException = EnumRenderException.ZERO_LENGTH_STREAM;
Exit While
End If
'Add the byte stream of current page in pages[] array so that we can have complete report in pages[][] array
If currentPageStream.Length > 0 Then
Array.Resize(pages, pages.Length + 1)
pages(pages.Length - 1) = currentPageStream
numberOfPages += 1
End If
End While
numberOfPages = numberOfPages - 1
' new Checking End
Dim objPrint As New clsReportPrinting
objPrint.PrintReport(pages, "CutePDF Writer")
Return True
End Function
Code To Sent Bytes into Printer:
Public Class clsReportPrinting
Private byaPagesToPrint()() As Byte = Nothing, _
m_oMetafile As Metafile = Nothing, _
m_iNumberOfPages As Int32 = 0, _
m_iCurrentPrintingPage As Int32 = 0, _
m_iLastPrintingPage As Int32 = 0, _
m_oCurrentPageStream As MemoryStream = Nothing, _
m_oDelegate As Graphics.EnumerateMetafileProc = Nothing
Public Function PrinterExists(ByVal PrinterName As String) As Boolean
'Returns TRUE if the named printer is amongst installed printers, FALSE otherwise
Try
PrinterName = PrinterName.Trim.ToUpper
For Each sPrinter As String In PrinterSettings.InstalledPrinters
'Printers may have UNC names, so we use .EndsWith to deal with \\Server\SharedPrinter
'if we pass "SharedPrinter" as PrinterName
If sPrinter.Trim.ToUpper.EndsWith(PrinterName) Then
Return True
End If
Next
Return False
Catch oEx As Exception
Throw (oEx)
Return False
End Try
End Function
Public Function MetafileCallback(ByVal oRecType As EmfPlusRecordType, _
ByVal iFlags As Int32, _
ByVal iDataSize As Int32, _
ByVal dpData As IntPtr, _
ByVal oCallbackData As PlayRecordCallback _
) As Boolean
Dim byaDataArray() As Byte = Nothing
If dpData <> IntPtr.Zero Then
'Copy unmanaged data to managed array for PlayRecord call
Array.Resize(Of Byte)(byaDataArray, iDataSize)
Marshal.Copy(dpData, byaDataArray, 0, iDataSize)
End If
'Play the record
m_oMetafile.PlayRecord(oRecType, iFlags, iDataSize, byaDataArray)
Return True
End Function
Private Function MoveToPage(ByVal lPage As Int32) As Boolean
'Check current page does exist
If Me.byaPagesToPrint(m_iCurrentPrintingPage - 1) Is Nothing Then
Return False
End If
'Set current page stream to desired rendered page
m_oCurrentPageStream = New MemoryStream(Me.byaPagesToPrint(m_iCurrentPrintingPage - 1))
'Set curernt stream position to its start
m_oCurrentPageStream.Position = 0
'Get rid of any former metafile
If Not m_oMetafile Is Nothing Then
m_oMetafile.Dispose()
m_oMetafile = Nothing
End If
'Set local metafile to page
m_oMetafile = New Metafile(m_oCurrentPageStream)
'Must always return TRUE
Return True
End Function
Public Sub pd_PrintPage(ByVal oSender As Object, _
ByVal oEV As PrintPageEventArgs)
oEV.HasMorePages = False
If (m_iCurrentPrintingPage <= m_iLastPrintingPage) And _
(MoveToPage(m_iCurrentPrintingPage)) Then
'Draw the page
DrawPage(oEV.Graphics)
'Point to next page
m_iCurrentPrintingPage += 1
'If there are more pages, flag so.
oEV.HasMorePages = (m_iCurrentPrintingPage <= m_iLastPrintingPage)
End If
End Sub
'This draws the current selected stream into a metafile
Public Sub DrawPage(ByVal oGrx As Graphics)
If m_oCurrentPageStream Is Nothing Or _
m_oCurrentPageStream.Length = 0 Or _
m_oMetafile Is Nothing Then
Return
End If
'Critical section follows (no more than one thread a time)
SyncLock Me
Dim iWidth As Int32 = m_oMetafile.Width, _
iHeight As Int32 = m_oMetafile.Height, _
oDestPoint As Point = Nothing
'Prepare metafile delegate
m_oDelegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback)
'Draw in the rectangle
oDestPoint = New Point(0, 0)
oGrx.EnumerateMetafile(m_oMetafile, oDestPoint, m_oDelegate)
'Clean up
m_oDelegate = Nothing
End SyncLock
End Sub
Public Function PrintReport(ByVal byaReport()() As Byte, _
ByVal sPrinterName As String _
) As Boolean
'Report data is an array of pages. Each page in turn is another byte array.
Me.byaPagesToPrint = byaReport
m_iNumberOfPages = Me.byaPagesToPrint.Length
Try
Dim oPS As PrinterSettings = New PrinterSettings
oPS.MaximumPage = m_iNumberOfPages
oPS.MinimumPage = 1
oPS.PrintRange = PrintRange.SomePages
oPS.FromPage = 1
oPS.ToPage = m_iNumberOfPages
oPS.PrinterName = sPrinterName
'oPS.DefaultPageSettings.Landscape = False
'oPS.DefaultPageSettings.Margins.Top = 0.5
'oPS.DefaultPageSettings.Margins.Bottom = 0.5
'oPS.DefaultPageSettings.Margins.Left = 0.83
'oPS.DefaultPageSettings.Margins.Right = 0
'oPS.DefaultPageSettings.PaperSize.Kind = PaperKind.Custom
oPS.DefaultPageSettings.PaperSize = New PaperSize("Custom", 1500, 1200)
'Dim value As Boolean
'value = oPS.LandscapeAngle
'oPS.LandscapeAngle = value
Dim oPD As PrintDocument = New PrintDocument
m_iCurrentPrintingPage = 1
m_iLastPrintingPage = m_iNumberOfPages
oPD.PrinterSettings = oPS
'Do print the report
AddHandler oPD.PrintPage, AddressOf Me.pd_PrintPage
oPD.Print()
Return True
Catch oEx As Exception
Throw (oEx)
End Try
End Function
'Private Function RenderReport(ByVal reportPath As String, ByVal parameters As ExecutionService.ParameterValue()) As Byte()()
' ' Private variables for rendering
' Dim historyId As String = Nothing
' Dim execHeader As New ExecutionService.ExecutionHeader()
' Try
' rs.Timeout = 300000
' rs.ExecutionHeaderValue = execHeader
' rs.LoadReport(reportPath, historyId)
' If (parameters IsNot Nothing) Then
' rs.SetExecutionParameters(parameters, "en_us")
' End If
' Dim pages As Byte()() = New [Byte](-1)() {}
' Dim format As String = "IMAGE"
' Dim numberOfPages As Integer = 1
' Dim currentPageStream As Byte() = New Byte(0) {&H0}
' ' put a byte to get the loop started
' Dim extension As String = Nothing
' Dim encoding As String = Nothing
' Dim mimeType As String = Nothing
' Dim streamIDs As String() = Nothing
' Dim warnings As ExecutionService.Warning() = Nothing
' While currentPageStream.Length > 0
' Dim deviceInfo As String = [String].Format("<DeviceInfo><OutputFormat>EMF</OutputFormat><PrintDpiX>200</PrintDpiX><PrintDpiY>200</PrintDpiY>" & "<StartPage>{0}</StartPage></DeviceInfo>", numberOfPages)
' 'rs.Render will render the page defined by deviceInfo's <StartPage>{0}</StartPage> tag
' currentPageStream = rs.Render(format, deviceInfo, extension, encoding, mimeType, warnings, _
' streamIDs)
' If currentPageStream.Length = 0 AndAlso numberOfPages = 1 Then
' 'renderException = EnumRenderException.ZERO_LENGTH_STREAM;
' Exit While
' End If
' 'Add the byte stream of current page in pages[] array so that we can have complete report in pages[][] array
' If currentPageStream.Length > 0 Then
' Array.Resize(pages, pages.Length + 1)
' pages(pages.Length - 1) = currentPageStream
' numberOfPages += 1
' End If
' End While
' numberOfPages = numberOfPages - 1
' Return pages
' Catch ex As System.Web.Services.Protocols.SoapException
' Console.WriteLine(ex.Detail.InnerXml)
' Catch ex As Exception
' Console.WriteLine(ex.Message)
' ' Console.WriteLine("Number of pages: {0}", pages.Length);
' Finally
' End Try
' Return Nothing
'End Function
End Class
I have an ASP.NET 2010 app hitting a SQL 2005 db. I am attempting to build a stored proc dynamically, depending on the values selected on a search screen. I have done this many times in previosu versions of .net & sql with no problem. However, now, my IF statement always always acts as though no data was passed in. I have debugged the app and am sure that data is being set. I should note that when I run the procedure directly, with or without data, the correct data is returned.
Here is the rather simple stored proc..
ALTER PROCEDURE get_cases_by_search_criteria
#vin as varchar(30) = null
AS
declare #sqlstr varchar(1000)
set #sqlstr = 'SELECT
[Case].CaseID,
[Case].VIN,
[Case].Make,
[Case].Model,
[Case].VehicleYear,
if #vin is not null and #vin <> ''
set #sqlstr = #sqlstr + ' and ' + ('[Case].VIN = ''' + convert(varchar,#vin) + '''')
exec(#sqlstr)
RETURN
And here is the code that calls the stored proc...
Public Function GetCases(ByVal oSearchCriteria As SearchCriteria) As List(Of BE.Case)
Dim lstCase As New List(Of BE.Case)
Dim oCase As BE.Case
Dim oProviderFactory As New ProviderFactory
Dim oConnection As DbConnection
Dim oReader As System.Data.IDataReader
Dim oFactory As DbProviderFactory
Dim oCmd As DbCommand
Dim param1 As System.Data.Common.DbParameter
Try
'call my class to get an instance of the DBProviderFactory class
oFactory = oProviderFactory.GetFactory
'call another class of mine. pass in the DBProviderFactory class which will create a non-provider-specific connection object
oConnection = oProviderFactory.GetProviderConnection(oFactory)
'non-specific create command
oCmd = oConnection.CreateCommand
'non-specific parameter
If oSearchCriteria.VIN.Length = 0 Then
param1 = oFactory.CreateParameter()
param1.ParameterName = "#vin"
param1.DbType = DbType.String
param1.Value = DBNull.Value
oCmd.Parameters.Add(param1)
Else
param1 = oFactory.CreateParameter()
param1.ParameterName = "#vin"
param1.DbType = DbType.String
param1.Value = oSearchCriteria.VIN
oCmd.Parameters.Add(param1)
End If
oCmd.CommandType = CommandType.StoredProcedure
oCmd.CommandText = "get_cases_by_search_criteria"
Using (oConnection)
oConnection.Open()
oReader = oCmd.ExecuteReader()
While oReader.Read
oCase = New BE.Case
'Case
If oReader("CaseID") IsNot System.DBNull.Value Then oCase.CaseID = oReader("CaseID")
If oReader("Make") IsNot System.DBNull.Value Then oCase.Make = oReader("Make")
If oReader("Model") IsNot System.DBNull.Value Then oCase.Model = oReader("Model")
If oReader("VehicleYear") IsNot System.DBNull.Value Then oCase.VehicleYear = oReader("VehicleYear")
If oReader("VIN") IsNot System.DBNull.Value Then oCase.VIN = oReader("VIN")
lstCase.Add(oCase)
End While
oConnection.Close()
End Using
Catch ex As Exception
Throw ex
End Try
Return lstCase
End Function