VBA6 Syntax and OPOS Printer OCX Driver in Access 2007
I'm using a "OPOS" printer and Access 2007, and I'm trying to get some access VBA code interact with the driver.
So far I got this VBA6 program that I'm trying to chew into Access VBA:
Form1.frm:
VERSION 5.00
Object = "{CCB90150-B81E-11D2-AB74-0040054C3719}#1.0#0"; "OPOSPOSPrinter.ocx"
Begin VB.Form Step1
BorderStyle = 1 'fixed
Caption = "Step1: ""Hello OPOS"" is printed "
ClientHeight = 1590
ClientLeft = 45
ClientTop = 330
ClientWidth = 3795
LinkTopic = "MDIForm1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 1590
ScaleWidth = 3795
StartUpPosition = 3 'Windows default value
Begin VB.CommandButton cmdPrint
Caption = "Print Now"
Height = 450
Left = 1155
TabIndex = 0
Top = 525
Width = 1515
End
Begin OposPOSPrinter_CCOCtl.OPOSPOSPrinter OPOSPOSPrinter1
Left = 3120
OleObjectBlob = "Step1.frx":0000
Top = 960
End
End
Attribute VB_Name = "Step1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Step 1: "Hello OPOS" gets printed
Option Explicit
Private Sub cmdPrint_Click()
'send a string to the printer using method PrintNormal
'vbCrLf is VisualBasic standard newline
OPOSPOSPrinter1.PrintNormal PTR_S_RECEIPT, "Hello OPOS" + vbCrLf
End Sub
Private Sub Form_Load()
With OPOSPOSPrinter1
'open device
.Open "Unit1"
'Claim exclusive access
.ClaimDevice 1000
'enable the device
.DeviceEnabled = True
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
With OPOSPOSPrinter1
'stop device
.DeviceEnabled = False
'Release exclusive access
.ReleaseDevice
'Done with the printer
.Close
End With
End Sub
Also
This other file is included: OposAll.bas
...as can be seen in the VBA project file:
Type=Exe
Form=Step1.frm
Module=OPOS; ........IncludeOposAll.bas
Object={CCB90150-B81E-11D2-AB74-0040054C3719}#1.0#0; OPOSPOSPrinter.ocx
IconForm="Step1"
Startup="Step1"
HelpFile=""
Title="SamplePrint1"
Command32=""
Name="SamplePrint1"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="MECS"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
I'm having a hard time trying to figure out how to translate this into working Access VBA code.
My questions may seem silly but keep in mind I don't know either VBA6 nor Access 2007 VBA. But working with the DLL version was rather easy and I thought this would be a doable one too.
1) Silly question: What does this piece of code stand for? Is it initializing some object? Access seems to dislike the OPOSPOSPrinter1
part, is there another way to get this done in Access?
Begin OposPOSPrinter_CCOCtl.OPOSPOSPrinter OPOSPOSPrinter1
Left = 3120
OleObjectBlob = "Step1.frx":0000
Top = 960
End
2) This is maybe the most important question: Is there a saner way to communicate with an OCX driver from Access 2007 VBA?
As I found in MS website, in Access 2007 you need to summon the ActiveX registering dialog in some obscure way:
Open a trusted database, or enable macros in the database.
Press CTRL+G to open the Immediate window.
Type the following code, and then press ENTER.
DoCmd.RunCommand acCmdRegisterActiveXControls
Next, I registered my exotic OCX control and got it listed as OPOS POSPrinter Control 1.13.001 [Public, by CRM/RCS-Dayton]
.
After that, I added the newly registered control from the equally hidden Insert ActiveX button:
Once the control was added to my form, I proceeded to add the code for loading and unloading the driver:
Private Sub Form_Load()
With OPOSPOSPrinter1
'open device
.Open "Unit1"
'Claim exclusive access
.ClaimDevice 1000
'enable the device
.DeviceEnabled = True
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
With OPOSPOSPrinter1
'stop device
.DeviceEnabled = False
'Release exclusive access
.ReleaseDevice
'Done with the printer
.Close
End With
End Sub
As you can see, it didn't change at all in comparison to the original VBA6 form. Oh, and this syntax: WITH <resource> (Body) END WITH
is just the VBA equivalent to C# using
or the brand new try (<autoclosable>)
in Java. So when you say WITH OPOSPOSPrinter1
you are pointing to the activex control you just added. Check the name Access picked for it.
Importing the opos.bas
was straightforward, probably because the Ribbon® wasn't involved at all in the process. In the code editor right click the project tree and click "Import...". The .bas
files are imported as "Modules" verbatim, go figure. Maybe because it's just a bunch of variable definitions.
The print command also compiles unchanged:
Private Sub Command1_Click()
OPOSPOSPrinter1.PrintNormal PTR_S_RECEIPT, "Hello OPOS" + vbCrLf
End Sub
I haven't yet tested the monstruosity but I lightly promise I will update after I gather feedback from the device.
Finally, to answer my own silly question. The syntax in my question 1) is VBA6's way of declaring a new control. Access seems to be doing it somewhere else.
链接地址: http://www.djcxy.com/p/54028.html