现在想保存一些窗体的list1里的数据,现在代码如下: 
在1窗体下 VB code
Private Sub Command7_Click()
Dim i As Integer, j As Integer, n As Integer
Screen.MousePointer = vbHourglass
CommonDialog2.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
CommonDialog2.FilterIndex = 2
CommonDialog2.DefaultExt = "txt"
CommonDialog2.ShowSave
If CommonDialog2.FileName <> "" Then
If Dir(CommonDialog2.FileName) <> vbNullString Then
If MsgBox("原文件存在,是否覆盖?", vbYesNo, "提示框") = vbYes Then
Kill CommonDialog2.FileName
Else
Exit Sub
End If
End If
Open CommonDialog2.FileName For Output As #2
For i = 0 To Form9.List1.ListCount - 1
Print #2, "009" & Form9.List1.List(i)
Next
For i = 0 To Form7.List1.ListCount - 1
Print #2, "007" & Form7.List1.List(i)
Next
For i = 0 To Form10.List1.ListCount - 1
Print #2, "010" & Form10.List1.List(i)
Next
Close #2
End If
Screen.MousePointer = vbDefault
End Sub这段代码,新建几个调试窗体调试什么毛病也没有 
但是放到我的工程中,现在如果7,9,10窗体的list1都有数据则可以保存,如果只保存排序第一个的(现在是9窗体)也好使,把这3个窗体都打开后,然后,即使选择一个也好使,或者在Open CommonDialog2.FileName For Output As #2前加进 
form7.hide 
form9.hide 
form10.hide 
这样也都好使,不想这么添加 
但是打开工程,然后进入某一个窗体,向这个窗体的list1添加内容,然后再隐藏退出,回到1窗体保存(除排在第一位的外),或,任意选择两个窗体进行保存,那就会错误,“错误的文件或号码”,如我现在进入工程,进入10窗体,添加内容,然后回来1窗体进行保存,这时,“错误的文件或号码”,调试结果Print #2, "010" & Form10.List1.List(i),我尝试用 
老鸟老师说的 FreeFile() 也不行 
我的7,9,10这3个窗体下都有以下两个代码,如果把这3个窗体以下代码都删除就好使了,也就是说在这冲突了 VB code
Private Sub Form_Load()
Dim i As Integer
    Dim sCol()
    Dim ii As Long
    Dim FG As Long, nn As Long, m As Long
    sCol = Array("一", "二"," 三")
    NDataInfo = OpenFile(App.Path & "\cc.txt")
    For i = LBound(NDataInfo) To UBound(NDataInfo)
        Combo1(0).AddItem NDataInfo(i).PartName
        Combo1(1).AddItem NDataInfo(i).PartName
    Next
    Combo1(0).ListIndex = 0
    Combo1(1).ListIndex = Combo1(0).ListCount - 1
    MSFlexGrid1.Cols = UBound(sCol) + 1
    For i = 0 To MSFlexGrid1.Cols - 1
        MSFlexGrid1.TextMatrix(0, i) = sCol(i)
        MSFlexGrid1.ColWidth(i) = 800
        MSFlexGrid1.ColAlignment(i) = flexAlignCenterCenter
    Next
End Sub和 VB code
Private Function OpenFile(ByVal FileName As String) As DataInfo()
    Dim Data() As DataInfo
    Dim count As Long
    Dim sData() As String
    Open FileName For Input As #1
    sData = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
    Close
    ReDim Data(1 To UBound(sData) + 1)
    For count = 0 To UBound(sData)
        Data(count + 1).PartName = Trim(Split(Trim(sData(count)), ",")(0))
        Data(count + 1).PartNumber = Trim(Split(Trim(sData(count)), ",")(1))
    Next
    OpenFile = Data
End Function最好改动保存的那个代码,不要动这3个窗体的这两个代码,该如何解决,是否最后NDataInfo = OpenFile(App.Path & "\cc.txt")有问题,难道是他没有关闭

解决方案 »

  1.   

    这么执著,向你致敬。第二次建议,把代码缩进好并以代码形式插入进来,像下面这样:Private Sub Command7_Click()
        Dim i As Integer, j As Integer, n As Integer
        
        Screen.MousePointer = vbHourglass
        CommonDialog2.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
        CommonDialog2.FilterIndex = 2
        CommonDialog2.DefaultExt = "txt"
        CommonDialog2.ShowSave
        If CommonDialog2.FileName <> "" Then
            If Dir(CommonDialog2.FileName) <> vbNullString Then
                If MsgBox("原文件存在,是否覆盖?", vbYesNo, "提示框") = vbYes Then
                    Kill CommonDialog2.FileName
                Else
                    Exit Sub
                End If
            End If
            Open CommonDialog2.FileName For Output As #2
            For i = 0 To Form9.list1.ListCount - 1
                Print #2, "009" & Form9.list1.List(i)
            Next
            For i = 0 To Form7.list1.ListCount - 1
                Print #2, "007" & Form7.list1.List(i)
            Next
            For i = 0 To Form10.list1.ListCount - 1
                Print #2, "010" & Form10.list1.List(i)
            Next
            Close #2
        End If
        Screen.MousePointer = vbDefault
    End Sub
      

  2.   

    文件号用 FreeFile,不要#1,#2这样...
    Private Function OpenFile(ByVal FileName As String) As DataInfo() 
        Dim Data() As DataInfo 
        Dim count As Long 
        Dim sData() As String 
        dim hFile as long
        
        hFile=freefile
        Open FileName For Input As hFile 
            sData = Split(StrConv(InputB(LOF(hFile), hFile), vbUnicode), vbCrLf) 
        Close 
        ReDim Data(1 To UBound(sData) + 1) 
        For count = 0 To UBound(sData) 
            Data(count + 1).PartName = Trim(Split(Trim(sData(count)), ",")(0)) 
            Data(count + 1).PartNumber = Trim(Split(Trim(sData(count)), ",")(1)) 
        Next 
        OpenFile = Data 
    End Function