CommonDialog1.ShowPrinter我用了一个ShowPrinter方法调出一个打印对话框,对话框有两个按钮,一个是“打印”,一个是“取消”,无论我点哪一个,都会打印。我想知道怎么让他点“取消”的时候不打印。

解决方案 »

  1.   


    dialog1.CancelError =true 
    On Error GoTo Cancel 
    ... 
    Cancel: 
    printer.killdoc
      

  2.   

    不会呀!可能你代码写的不对。Private Sub Command1_Click()
        ' 设置“取消”为 True
        CommonDialog1.CancelError = True
        On Error GoTo ErrHandler
        ' 显示“打印”对话框
        CommonDialog1.ShowPrinter
        '写需执行的打印代码
        MsgBox "START"
        Exit Sub
    ErrHandler:
        ' 用户按了“取消”按钮
        MsgBox "STOP"
        Exit Sub
    End Sub
      

  3.   

    Private   Sub   Command1_Click()   
      On   Error   GoTo   Err   
              CommonDialog1.CancelError   =   True   
              CommonDialog1.ShowPrinter   
        
      Err:   
              If   Err.Number   =   32755   Then   
                      MsgBox   "你按了取消!",   vbCritical,   "系统提示"   
              End   If   
      End   SubCancelError   属性返回或设置一个值,该值指示当选取“取消”按钮时是否出错。当该属性设置为   True   时,无论何时选取“取消”按钮,均产生   32755   (cdlCancel)   号错误。你可以利用On   Error捕捉它。     
              另外,如果用户在“打开”、“另存为”对话框中选择“取消”按钮,Filename属性得到的是空串 
      

  4.   

    各位大虾提出的方法都试过了,没问题。可是又有了新的问题。因为我想点击“Commandprint”的同时自动存储数据到数据库。点击“取消”,是不打印了,但是还是存储数据。如何让他既不打印又不存储数据呢?代码如下:Private Sub Commandprint_Click()CommonDialog1.CancelError = True 
    On Error GoTo Cancel 
        
    If Dir("printdb.mdb") = "" Then      '判断当前路径方法
    Set s = DAO.Workspaces(0).CreateDatabase(App.Path + "/printdb.mdb", dbLangGeneral)
    's.NewPassword "", "1" '设置数据库密码为1
    str_SQL1 = "Create Table 计量单(日期 Date,编号 long ,收货单位 text(50), 销售订单号 text(50) ,生产订单号 text(50) ,产品名称 text(50) ,卡号 text(50) ,钢号 text(50) ,等级 text(50) ,箱号 text(50),规格 text(50) ,表面 text(50) ,产品标准 text(100) ,计量毛重 text(50) ,计量皮重 text(50) ,计量净重 text(50) ,质量扣重 text(50) ,包装扣重 text(50) ,交货重量 text(50))"    'Single,,,,,,,,,,,,,,, ,,
    s.Execute str_SQL1
    str_SQL2 = "Create Table 标签(牌号 text(50),规格 text(50),标准 text(50),C100厚度 text(50),卷(箱)号 text(50),表面加工 text(50),卡片号 text(50),等级 text(50),毛重 single,边部 text(50),净重 single,日期 Date ,检查员 text(10))"    '
    s.Execute str_SQL2Else
    Set mydatabase = New ADODB.Connection
    Set myrecordset1 = New ADODB.Recordset
    mydatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\printdb.mdb;Persist Security Info=False"
    mydatabase.OpenSql = "Select * from 计量单"Set myrecordset1.ActiveConnection = mydatabase
    myrecordset1.CursorLocation = adUseClient
    myrecordset1.Source = Sql
    myrecordset1.CacheSize = 30
    myrecordset1.Open , mydatabase, adOpenKeyset, adLockOptimistic, adCmdText
    myrecordset1.AddNewmyrecordset1("日期") = Date
    myrecordset1("收货单位") = Text12.Text
    myrecordset1("销售订单号") = Text9.Text
    myrecordset1("生产订单号") = Text4.Text
    myrecordset1("产品名称") = Text7.Text
    myrecordset1("卡号") = Text2.Text
    myrecordset1("钢号") = Comboganghao.Text
    myrecordset1("等级") = Combodengji.Text
    myrecordset1("箱号") = Text3.Text
    myrecordset1("规格") = Text1.Text & Text6.Text
    myrecordset1("表面") = Combobiaomian.Text
    myrecordset1("产品标准") = Combobiaozhun.Text
    myrecordset1("计量毛重") = Text10.Text
    myrecordset1("计量皮重") = Text13.Text
    myrecordset1("计量净重") = Text15.Text
    myrecordset1("质量扣重") = Text11.Text
    myrecordset1("包装扣重") = Text14.Text
    myrecordset1("交货重量") = Text16.Textmyrecordset1.Update
    myrecordset1.Close
    End IfCommonDialog1.ShowPrinterPrinter.FontSize = 20
    Printer.Print
    Printer.FontSize = 14
    Printer.CurrentX = 1600
    Printer.Print Combobiaozhun.Text;
    Printer.CurrentX = 4772 + 3170
    Printer.Print Text8.Text
    。省略了Printer.EndDoc '把这一句加上
    Cancel:
    Printer.KillDoc    Exit Sub
    End Sub
      

  5.   

    Private Sub Commandprint_Click()    CommonDialog1.CancelError = True
        
        On Error GoTo Cancel
            
        
        CommonDialog1.ShowPrinter
        
        Printer.FontSize = 20
        Printer.Print
        Printer.FontSize = 14
        Printer.CurrentX = 1600
        Printer.Print Combobiaozhun.Text;
        Printer.CurrentX = 4772 + 3170
        Printer.Print Text8.Text
        '????省略了
        
        Printer.EndDoc '把这一句加上
        Call sub_SaveData
        
        Exit Sub
    Cancel:
        Printer.KillDoc    Exit Sub
    End SubPrivate Sub sub_SaveData()
        If Dir("printdb.mdb") = "" Then      '判断当前路径方法
            Set s = DAO.Workspaces(0).CreateDatabase(App.Path + "/printdb.mdb", dbLangGeneral)
            's.NewPassword "", "1" '设置数据库密码为1
            str_SQL1 = "Create Table 计量单(日期 Date,编号 long ,收货单位 text(50), 销售订单号 text(50) ,生产订单号 text(50) ,产品名称 text(50) ,卡号 text(50) ,钢号 text(50) ,等级 text(50) ,箱号 text(50),规格 text(50) ,表面 text(50) ,产品标准 text(100) ,计量毛重 text(50) ,计量皮重 text(50) ,计量净重 text(50) ,质量扣重 text(50) ,包装扣重 text(50) ,交货重量 text(50))"    'Single,,,,,,,,,,,,,,, ,,
            s.Execute str_SQL1
            str_SQL2 = "Create Table 标签(牌号 text(50),规格 text(50),标准 text(50),C100厚度 text(50),卷(箱)号 text(50),表面加工 text(50),卡片号 text(50),等级 text(50),毛重 single,边部 text(50),净重 single,日期 Date ,检查员 text(10))"    '
            s.Execute str_SQL2
        
        Else
            Set mydatabase = New ADODB.Connection
            Set myrecordset1 = New ADODB.Recordset
            mydatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\printdb.mdb;Persist Security Info=False"
            mydatabase.Open
            
            Sql = "Select * from 计量单"
            
            Set myrecordset1.ActiveConnection = mydatabase
            myrecordset1.CursorLocation = adUseClient
            myrecordset1.Source = Sql
            myrecordset1.CacheSize = 30
            myrecordset1.Open , mydatabase, adOpenKeyset, adLockOptimistic, adCmdText
            myrecordset1.AddNew
            
            myrecordset1("日期") = Date
            myrecordset1("收货单位") = Text12.Text
            myrecordset1("销售订单号") = Text9.Text
            myrecordset1("生产订单号") = Text4.Text
            myrecordset1("产品名称") = Text7.Text
            myrecordset1("卡号") = Text2.Text
            myrecordset1("钢号") = Comboganghao.Text
            myrecordset1("等级") = Combodengji.Text
            myrecordset1("箱号") = Text3.Text
            myrecordset1("规格") = Text1.Text & Text6.Text
            myrecordset1("表面") = Combobiaomian.Text
            myrecordset1("产品标准") = Combobiaozhun.Text
            myrecordset1("计量毛重") = Text10.Text
            myrecordset1("计量皮重") = Text13.Text
            myrecordset1("计量净重") = Text15.Text
            myrecordset1("质量扣重") = Text11.Text
            myrecordset1("包装扣重") = Text14.Text
            myrecordset1("交货重量") = Text16.Text
            
            myrecordset1.Update
            myrecordset1.Close
        End IfEnd Sub
      

  6.   

    Private Sub Commandprint_Click()    CommonDialog1.CancelError = True
        
        On Error GoTo Cancel
            
        
        CommonDialog1.ShowPrinter
        
        Printer.FontSize = 20
        Printer.Print
        Printer.FontSize = 14
        Printer.CurrentX = 1600
        Printer.Print Combobiaozhun.Text;
        Printer.CurrentX = 4772 + 3170
        Printer.Print Text8.Text
        '????省略了
        
        Printer.EndDoc '把这一句加上
        Call sub_SaveData
        
        Exit Sub
    Cancel:
        Printer.KillDoc    Exit Sub
    End SubPrivate Sub sub_SaveData()
        If Dir("printdb.mdb") = "" Then      '判断当前路径方法
            Set s = DAO.Workspaces(0).CreateDatabase(App.Path + "/printdb.mdb", dbLangGeneral)
            's.NewPassword "", "1" '设置数据库密码为1
            str_SQL1 = "Create Table 计量单(日期 Date,编号 long ,收货单位 text(50), 销售订单号 text(50) ,生产订单号 text(50) ,产品名称 text(50) ,卡号 text(50) ,钢号 text(50) ,等级 text(50) ,箱号 text(50),规格 text(50) ,表面 text(50) ,产品标准 text(100) ,计量毛重 text(50) ,计量皮重 text(50) ,计量净重 text(50) ,质量扣重 text(50) ,包装扣重 text(50) ,交货重量 text(50))"    'Single,,,,,,,,,,,,,,, ,,
            s.Execute str_SQL1
            str_SQL2 = "Create Table 标签(牌号 text(50),规格 text(50),标准 text(50),C100厚度 text(50),卷(箱)号 text(50),表面加工 text(50),卡片号 text(50),等级 text(50),毛重 single,边部 text(50),净重 single,日期 Date ,检查员 text(10))"    '
            s.Execute str_SQL2
        
        Else
            Set mydatabase = New ADODB.Connection
            Set myrecordset1 = New ADODB.Recordset
            mydatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\printdb.mdb;Persist Security Info=False"
            mydatabase.Open
            
            Sql = "Select * from 计量单"
            
            Set myrecordset1.ActiveConnection = mydatabase
            myrecordset1.CursorLocation = adUseClient
            myrecordset1.Source = Sql
            myrecordset1.CacheSize = 30
            myrecordset1.Open , mydatabase, adOpenKeyset, adLockOptimistic, adCmdText
            myrecordset1.AddNew
            
            myrecordset1("日期") = Date
            myrecordset1("收货单位") = Text12.Text
            myrecordset1("销售订单号") = Text9.Text
            myrecordset1("生产订单号") = Text4.Text
            myrecordset1("产品名称") = Text7.Text
            myrecordset1("卡号") = Text2.Text
            myrecordset1("钢号") = Comboganghao.Text
            myrecordset1("等级") = Combodengji.Text
            myrecordset1("箱号") = Text3.Text
            myrecordset1("规格") = Text1.Text & Text6.Text
            myrecordset1("表面") = Combobiaomian.Text
            myrecordset1("产品标准") = Combobiaozhun.Text
            myrecordset1("计量毛重") = Text10.Text
            myrecordset1("计量皮重") = Text13.Text
            myrecordset1("计量净重") = Text15.Text
            myrecordset1("质量扣重") = Text11.Text
            myrecordset1("包装扣重") = Text14.Text
            myrecordset1("交货重量") = Text16.Text
            
            myrecordset1.Update
            myrecordset1.Close
        End IfEnd Sub