【问题标题】:VBA6 Syntax and OPOS Printer OCX Driver in Access 2007Access 2007 中的 VBA6 语法和 OPOS 打印机 OCX 驱动程序
【发布时间】:2013-06-19 21:48:12
【问题描述】:

我正在使用“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 驱动程序进行通信?

【问题讨论】:

  • 您是否尝试将其添加为参考?还是将其作为 activex 控件添加到访问表单中?
  • 如果你能告诉我如何用一个可以作为答案的驱动程序来做这件事。我的谷歌搜索技能很差,或者在这里太晚了,但我没有发现任何有用的东西。

标签: ms-access ms-access-2007 vba opos


【解决方案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]

之后,我从同样隐藏的“插入 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 控件。检查为其选择的名称 Access。

导入opos.bas 很简单,可能是因为Ribbon® 根本没有参与到这个过程中。在代码编辑器中右键单击项目树并单击“导入...”。 .bas 文件逐字导入为“模块”,如图。也许是因为它只是一堆变量定义。

打印命令也不变编译:

Private Sub Command1_Click()
    OPOSPOSPrinter1.PrintNormal PTR_S_RECEIPT, "Hello OPOS" + vbCrLf
End Sub

我还没有测试过这个怪物,但我轻轻地保证我会在收集到设备的反馈后更新。

最后,回答我自己的愚蠢问题。我的问题 1) 中的语法是 VBA6 声明新控件的方式。 Access 似乎正在其他地方执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 2010-10-22
    • 2015-11-02
    • 2017-12-16
    • 2017-01-26
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多