我在一个窗口(1窗口)里用Dtreport.Show vbModal调用一个数据报表窗口(2窗口)
而2窗口里的代码
 If Printer.Width <= reportwidth Or Printer.Width - reportwidth <= 1600 Then
   MsgBox "报表宽度大于打印纸宽度", vbOKOnly, "提示"
 ' Unload Me
  frmrpt.Show
  Exit Sub
end if
我想让他停止加载报表窗口
可是用Unload Me的话在Dtreport.Show vbModal处报错对象变量或with块变量未设置
不用他的话就打开一个白板数据报窗口还说 未找到数据域datafield'(空)'请大虾指点下,该怎么弄啊 ???

解决方案 »

  1.   

    Unload Me语句不能用在Load时间中,可以在Activate事件中使用。
      

  2.   

    把你的代码放到Activate事件中去啊。
      

  3.   

    我测试过,unload me 在Activate事件运行是正常的。
    估计你其它代码还有问题,把窗口2的相关代码都贴出来看看吧。
      

  4.   

    Private Sub DataReport_Initialize()
    Dim leftpos As Long 'leftpos为存放控件left属性的变量
    Dim reportwidth As Long 'reportwidth为存放Datareport总宽度的变量
    Dim reportwidth1 As Long
    'Dim lst_selected As ListBox
    Dim i As Integer
    Dim j As Integer
     Dim k As Integer
    If frmrpt.lst_selected.ListCount <> 0 Then
    'Call frmrpt.fieldwidth(frmrpt.lst_selected) '设置字段宽度
     Const wordwidth As Integer = 201 '五号字的宽度为201缇
    For i = 0 To frmrpt.lst_selected.ListCount - 1
    Select Case frmrpt.lst_selected.List(i)
    Case "学号"
    frmrpt.lst_selected.ItemData(i) = wordwidth * 5
    Case "姓名"
     .......
     End Select
    Next i
     
    '求总宽度
    For i = 0 To frmrpt.lst_selected.ListCount - 1
    reportwidth = reportwidth + frmrpt.lst_selected.ItemData(i)Next
    '初始化
    With Dtreport
    '设置数据源,页边距,标题,横向分割线,section1、2区第一条竖分割线
    .LeftMargin = 0
    .RightMargin = 0
    .TopMargin = 1440
    .BottomMargin = 1000
    'reportwidth1 = Printer.Width - .LeftMargin
    .reportwidth = Printer.Width - 1000 - 20
    Dim strSQL As String Set rs = ExecuteSQL(qtcxr.txSQL, MsgText)
      'rs.Open strSQL, AdoCon
     
    Set .DataSource = rsDtreport.Sections.Item("section2").Controls.Item("Label41").Caption = frmrpt.txt_caption
    If Printer.Width <= reportwidth Or Printer.Width - reportwidth <= 1600 Then
       MsgBox "报表宽度大于打印纸宽度", vbOKOnly, "提示"
     ' Unload Me
     Call DataReport_Activate
      frmrpt.Show
      Exit Sub
       
    End If
    Dtreport.Sections.Item("section2").Controls.Item("Label41").Left = (Printer.Width - reportwidth) / 2 - 800
    Dtreport.Sections("section2").Controls.Item("Label41").Width = reportwidth
     ......
    End With'为section1,2区设置数据
    leftpos = (Printer.Width - reportwidth) / 2 - 800For i = 0 To frmrpt.lst_selected.ListCount - 1
    k = i - 1
    Me.Sections("section2").Controls.Item("Label2" & (i + 1)).Caption = frmrpt.lst_selected.List(i)
    Me.Sections("section2").Controls.Item("Label2" & (i + 1)).Width = frmrpt.lst_selected.ItemData(i)
    Me.Sections("section1").Controls.Item("Text1" & (i + 1)).Width = frmrpt.lst_selected.ItemData(i)
    If leftpos <= wordwidth * 5 Then
       MsgBox "报表宽度大于打印纸宽度", vbOKOnly, "提示"
        'Unload Me
       Exit Sub
       Unload Me
       
       Load frmrpt
    End If
    Me.Sections("section1").Controls.Item("Text1" & (i + 1)).Left = leftpos - wordwidth * 5
     
       GoTo Label1
    End If
    Next jLabel1:
    'Me.Sections("section1").Controls.Item("Text1" & (i + 1)).DataField = rs.Fields.Item(i).NameNext i'对不用的text控件必须设置其datafield属性,如frmrpt.lst_selected.List(0),否则出错,但一定让其不可见
    '其他不用的label,line控件同样均不可见
    i = frmrpt.lst_selected.ListCount
    While i < (Me.Sections("section1").Controls.Count - 19)
    '10 为section1区域的非text控件的控件总数
    i = i + 1
    Me.Sections("section1").Controls.Item("Text1" & i).DataField = rs.Fields.Item(0).Name
     
    Wend
     End If
     'Me.PrintReport True
     End Sub
      

  5.   

    DataReport是窗体吗?如果是的话,这段代码不应该放在Initialize事件里,放在而是“Activate”事件里。
      

  6.   

    放在Initialize跟放在Activate有什么区别?
      

  7.   

    Initialize事件:发生在对象创建的时候,类似于类的构造函数,当使用New关键字时,它会被自动调用。这个事件说明窗体对象已被创建,但没有用于窗口。Load事件:发生于窗体加载的时候,也就是使用Load语句时。这时候窗体已经拥有窗口了,并且窗体内的控件也已经被加载。Activate事件:当窗体被要求显示的时候,也就是调用Show方法时。所以调用Form.Show函数时,实际上经历了以上3个步骤,从Initialize到Load再到Activate,这样就能理解为什么在Initialize和Load事件里使用Unload me语句会出现问题了,因为使用Unload后,对象就被卸载了,而这是Show还没执行完成。
      

  8.   

    放在这里不对了 
    出现无效数据源的问题了
    -----------------
    这个原因是因为你下面的语句有BUG:
     Set rs = ExecuteSQL(qtcxr.txSQL, MsgText)
      'rs.Open strSQL, AdoCon
     
    Set .DataSource = rs你这样修改一下:Set rs = ExecuteSQL(qtcxr.txSQL, MsgText)If not rs is nothing Then  with Dtreport
         Set .DataSource = rs
         .....
      End With  .....End if
      

  9.   

    不是哪条
    是直接跳出一个消息框来告诉无效数据源
    如果把代码放在Initialize里就没这个错误
    是可以通过的
      

  10.   

    在窗体设计的时候。你是不是设置了窗体的DataSource?
      

  11.   

    方便的话,代码给我看看。[email protected]
      

  12.   

    我大概知道原因了,你把下面的语句放在Dtreport.Show之前,然后把DataReport报表里的rs变量都换成Me.DataSource,DataReport在Show之前必须要先设置好DataSource
    Dim strAppPath As String
       strAppPath = App.Path
       If Right(strAppPath, 1) <> "\" Then
           strAppPath = strAppPath & "\"
       End If
        strAppPath = App.Path & "\贫困生档案.mdb"
         
    Dim AdoCon As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strSQL As String
    Set AdoCon = New ADODB.Connection
     AdoCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strAppPath & ""
      AdoCon.Open
      
       
       strSQL = "select 学号,姓名 from 学生基本信息 "
       Set rs = ExecuteSQL(strSQL, MsgText)
       rs.Open strSQL, AdoCon
     Set .DataSource = rs
      

  13.   

    代码没有问题,只是在show之前,先load一下就可以了.
    Load Dtreport
    Dtreport.show vbmodal
      

  14.   

    Me.DataSource.Fields.Count
    在这报错“找不到方法Fields”
      

  15.   

    哦,忘了进行后期绑定了.
    你在Dtreport报表内定义一个变量dim m_Rst As ADODB.RecordSet在Initialize事件中加入如下代码:set m_Rst=Me.DataSource然后把Dtreport里的rs都换成m_Rst
      

  16.   

    Set Dtreport.DataSource = rs
    这句有问题,运行过程中我把鼠标放上去
    有提示“Dtreport.DataSource = nothing”
      

  17.   

    他的上一句
    Set rs = ExecuteSQL(qtcxr.txSQL, MsgText)
    没错,是正常的
      

  18.   

    如果OPEN就提示对象打开时不允许操作
      

  19.   

    晕,我一般用DataReport都是用DataEnviroment和它配合使用。建议你还是在设计的时候,设置好DataSource吧。