Dim myReport As CRAXDRT.Report
Dim Report_App As CRAXDRT.Application
  
Set Report_App = New CRAXDRT.Application
Set myReport = Report_App.OpenReport("d:\123.rpt")
  
'绑定水晶报表数据源
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "select * from 购物记录表", dbConnection, adOpenKeyset, adLockReadOnly
myReport.Database.Tables(1).SetDataSource rs
Set rs = Nothing
 
‘替换参数字段
Call EditField("xm", "张三")                   '假定你在水晶报表里面定义了一个名为xm的参数字段,这里用“张三”替换对应的内容
Call EditField("totalmoney", "35.22") 
…………'显示报表预览  
With CRViewer1
        .ReportSource = myReport
          
        .EnableGroupTree = False
        .EnableAnimationCtrl = False
        .EnableDrillDown = False
        .EnableHelpButton = False
        .EnablePopupMenu = False
        .EnableSearchControl = False
        .EnableSearchExpertButton = False
        .EnableSelectExpertButton = False
        .EnableStopButton = False
  
        .ViewReport
End With
 
Private Sub EditField(strSource As String, strReplace As String)
    Dim i As Long
    For i = 1 To myReport.FormulaFields.Count
        If myReport.FormulaFields(i).FormulaFieldName = strSource Then
            myReport.FormulaFields(i).Text = "'" & strReplace & "'"
            Exit Sub
        End If
    Next
End Sub

解决方案 »

  1.   


    非常感谢。汇总一些有用的资料:
    一:
      '进行参数设置演示:报表模板中增加了3个参数
        '阿泰:2004-12-15,新增
        '让模板中设置的参数不出现提示框!!!!
        objCRReport.EnableParameterPrompting = False      '不进行报表参数提示
        
        '先清理可能存在的参数默认值等,一定要做。这里使用序号来依次操作
        '如果参数多的话可以使用循环等技巧来清理
        objCRReport.ParameterFields(1).ClearCurrentValueAndRange
        objCRReport.ParameterFields(2).ClearCurrentValueAndRange
        objCRReport.ParameterFields(3).ClearCurrentValueAndRange
        '进行赋值
        '注意如果有时候此处出现错误提示,那么就要查看
        '1-传入的参数是否有值
        '2-参数的类型是否匹配,可能要进行类型转换后才能传入
        '3-传入参数的值的序号是否对应
        Call objCRReport.ParameterFields(1).AddCurrentValue("公司名称测试1")
        Call objCRReport.ParameterFields(2).AddCurrentValue(CDate("2004-12-10"))
        Call objCRReport.ParameterFields(3).AddCurrentValue(100) 
    二、
        Report.DiscardSavedData
        Report.ParameterFields.GetItemByName("companyname").ClearCurrentValueAndRange
        Report.ParameterFields.GetItemByName("companyname").AddCurrentValue ("荣华富贵物业")