我正在使用“OPOS”打印机和Access 2007,我正试图让一些访问VBA代码与驱动程序进行交互。
到目前为止,我得到了这个VBA6程序,我正试图咀嚼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
另外
包含此其他文件:OposAll.bas
...可以在VBA项目文件中看到:
Type=Exe
Form=Step1.frm
Module=OPOS; ..\..\..\..\Include\OposAll.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
我正在努力弄清楚如何将其转换为可用的Access VBA代码。
我的问题可能看起来很愚蠢,但请记住,我不知道VBA6和Access 2007 VBA。但使用DLL版本相当容易,我认为这也是可行的。
1)愚蠢的问题:这段代码代表什么?
是初始化一些对象吗? Access似乎不喜欢OPOSPOSPrinter1
部分,是否有其他方法可以在Access中完成此操作?
Begin OposPOSPrinter_CCOCtl.OPOSPOSPrinter OPOSPOSPrinter1
Left = 3120
OleObjectBlob = "Step1.frx":0000
Top = 960
End
2)这可能是最重要的问题:是否有更好的方式与Access 2007 VBA中的OCX驱动程序进行通信?
答案 0 :(得分:1)
正如我在MS website中发现的那样,在Access 2007中,您需要以一种模糊的方式召唤ActiveX注册对话框:
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
接下来,我注册了我的异国情调的OCX控件并将其列为OPOS POSPrinter Control 1.13.001 [Public, by CRM/RCS-Dayton]
。
之后,我从同样隐藏的Insert ActiveX按钮添加了新注册的控件:
将控件添加到表单后,我继续添加用于加载和卸载驱动程序的代码:
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
正如您所看到的,与原始VBA6表单相比,它完全没有变化。哦,这个语法:WITH <resource> (Body) END WITH
只是VBA等同于C#using
或Java中的全新try (<autoclosable>)
。所以,当你说WITH OPOSPOSPrinter1
时,你指的是你刚刚添加的activex控件。检查为其选择的访问名称。
导入opos.bas
非常简单,可能是因为Ribbon®在此过程中根本没有参与。在代码编辑器中右键单击项目树,然后单击“导入...”。 .bas
文件逐字导入为“模块”,如图所示。也许是因为它只是一堆变量定义。
print命令也可以不加改变地编译:
Private Sub Command1_Click()
OPOSPOSPrinter1.PrintNormal PTR_S_RECEIPT, "Hello OPOS" + vbCrLf
End Sub
我还没有测试过这个怪物,但是我很轻松地保证在收集设备的反馈后我会更新。
最后,回答我自己的愚蠢问题。我的问题 1)中的语法是VBA6声明新控件的方式。访问似乎在其他地方进行。