尽管VB6已经是一个相对较老的编程语言,但许多程序员仍在使用它。本文旨在为这些程序员提供如何使用VB6打印发票的指导。本教程将介绍一个包含三个表单的项目:
这个表单用于绑定数据网格(DataGrid),以显示来自Northwind数据库文件的所有订单。
此表单用于选择一个订单,以便打印其发票。
这个表单用于在数据网格上显示发票,然后可以预览或作为报告打印发票。
确保在项目中添加了“Microsoft ActiveX Data Objects 2.x Library”。为打印发票添加了“Data Report”。可以使用任何数据库文件替代Northwind.mdb,并修改代码以连接到数据库文件。同时,也可以修改SQL字符串以绑定数据网格与数据。
以下是连接数据库文件并加载所有订单的代码(可以在frmInvoice表单中阅读此代码):
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim MyPass As String
Dim MyDataFile As String
Dim InvSql As String
Dim strCon As String
MyPass = ""
MyDataFile = App.Path & "\DataFile\Northwind.mdb"
strCon = "provider=microsoft.jet.oledb.4.0;data source=" & MyDataFile & ";Jet OLEDB:Database Password=" & MyPass & ";"
InvSql = "SELECT Customers.CompanyName, Customers.City, Employees.FirstName & Space(1) & Employees.LastName AS Salesperson, Orders.OrderID, Orders.OrderDate, [Order Details].ProductID, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount, CCur([Order Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 AS ExtendedPrice, Orders.Freight FROM Products INNER JOIN ((Employees INNER JOIN (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) ON Employees.EmployeeID = Orders.EmployeeID) INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID;"
Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.Open strCon
Set rs = New ADODB.Recordset
rs.Open InvSql, cn, adOpenStatic, adLockOptimistic
Set datGrid.DataSource = rs
以下是将数据网格与一个订单绑定的代码(可以在frmOrder表单中阅读此代码):
Dim rs As ADODB.Recordset
Dim intOrder As Integer
Dim InvSql As String
Dim strCon As String
' InvoiceOrder是选择的订单号:
intOrder = Val(InvoiceOrder)
' 获取发票数据:
InvSql = "SELECT [Order Details].ProductID, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount, CCur([Order Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 AS ExtendedPrice FROM Products INNER JOIN [Order Details] ON Products.ProductID=[Order Details].ProductID WHERE [Order Details].OrderID = " & intOrder
Set rs = New ADODB.Recordset
rs.Open InvSql, cn, adOpenStatic, adLockOptimistic
Set ordGrid.DataSource = rs
以下是将数据报告与一个订单绑定的代码(可以在frmOrder表单中阅读此代码):
Dim repSql As String
repSql = "SHAPE {SELECT Orders.OrderID,Orders.OrderDate,Orders.Freight, Customers.CustomerID,Customers.CompanyName, Customers.City,Customers.Phone, (Employees.FirstName + Space(1) + Employees.LastName) As SalesName FROM ((Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) WHERE Orders.OrderID = " & intOrder & "} AS ParentCMD APPEND ({SELECT DISTINCTROW [Order Details].OrderID, Products.ProductID,Products.ProductName, [Order Details].UnitPrice,[Order Details].Quantity,[Order Details].Discount, CCur([Order Details].UnitPrice*[Order Details].Quantity*((1-[Discount])/100)*100) As ExtendedPrice FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductID} AS ChildCMD RELATE OrderID TO OrderID)"
Set invReport = New ADODB.Recordset
invReport.ActiveConnection = cn
If invReport.State = adStateOpen Then
invReport.Close
End If
invReport.Open repSql, cn
invReport.Requery
Set repInvoice.DataSource = invReport
报告头部(第4节):包含一个PictureBox控件用于设置任何图像(如果有的话)和四个Label控件用于发票标题。
组头部(第6节):包含客户名称、城市、销售人员、订单ID、订单日期和数据表头部。
详细信息(第1节):包含六个TextBox控件用于显示数据表字段。