Option ExplicitDim sx() As Double
Dim sy() As Double
Dim str As String
Dim x() As Double
Dim y() As Double
Dim p As Integer
Dim a As IntegerPrivate Sub Command1_Click()Dim textline As String
Dim i As Integer
CommonDialog1.CancelError = TrueOn Error GoTo ErrHandlerCommonDialog1.Flags = cdlOFNHideReadOnlyCommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt"CommonDialog1.FilterIndex = 2
CommonDialog1.ShowOpen
str = CommonDialog1.FileNameOpen "str" For Input As #1
  
  i = 0
  Do While Not EOF(1)
   Line Input #1, textline
   
       ReDim Preserve x(i) As Double
       ReDim Preserve y(i) As Double
       x(i) = Left(textline, 3)
       y(i) = Right(textline, 5)      
       i = i + 1
   
  Loop
  a = i - 1Close #1p = x(1) - x(0)Exit SubErrHandler:  
   Exit SubEnd SubPrivate Sub Command2_Click()   Call caculate_1
   
End SubPrivate Sub Form_Load()   Dim i As Integer
   Dim sql As String
   Dim dbname As String
   
   
'On Error GoTo loaderror  
   
   dbname = App.Path
   If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
   dbname = dbname & "dataprocess.mdb"
   Set db = OpenDatabase(dbname, False, False)
   sql = "SELECT datatable.* FROM datatable ORDER BY datatable.nm"
   Set rs = db.OpenRecordset(sql, dbOpenDynaset)
   
    i = 0
   If rs.RecordCount > 0 Then
      Do Until rs.EOF
         ReDim Preserve sx(i) As Double
         ReDim Preserve sy(i) As Double
         sx(i) = rs![sx]
         sy(i) = rs![sy]
         rs.MoveNext
         i = i + 1
      Loop
   Else
      MsgBox "没有数据", vbExclamation + vbOKOnly, ""
      Exit Sub
   End If
   
'loaderror:
   'MsgBox Err.Description
   
End SubPrivate Sub caculate_1()   Dim i As Integer
   Dim x0 As Double, k0 As Double
   Dim kk As Double, xend As Double
   Dim xx() As Double, k() As Double
   
   
   x0 = 0
   k0 = 0
   
   For i = 0 To a - 1
      
      ReDim Preserve xx(i) As Double
      ReDim Preserve k(i) As Double
      xx(i) = p * sx(i) * y(i)
      x0 = x0 + xx(i)
      k(i) = sy(i) * y(i)
      k0 = k0 + k(i)
   Next i
      
   Text1(0).Text = x0  
'在这里,无论是x0还是p,或是k0,结果都是0,这是什么原因???
End Sub
我觉得是与动态数组的使用有关系,因为上面的打开文本和数据库,并将数据读入相应数组,这些都在单独的程序中调试通过。只有在caculate_1的过程中计算出现了问题,请大家解答! 谢谢!

解决方案 »

  1.   

    写简单了,我怕大家看不懂呀, 我描述一下: 在form_load 过程中,分别将数据库中的数据赋给sx(),sy()。在Command1_Click()过程中,分别将一个文本中的数据赋给x()和y()。这两个过程应该是没什么问题的。
    但是在caculate_1的过程中,发现计算有问题,即Text1(0).Text 这个语句,不论是Text1(0).Text = p,还是Text1(0).Text = x0,或Text1(0).Text = y(1)(或者x(1)),其结果都是在text1(0)中显示为零!
      

  2.   

    Open "str" For Input As #1  ???
    -> Open str For Input As #1
      

  3.   

    str = CommonDialog1.FileName 
    Open "str" For Input As #1     这个可以的!
      

  4.   

    问题解决了,应该改成Open str For Input As #1