以前对文件没有太大兴趣 也没有认真的研究过,前几天帮朋友做了几道题,才知道他也挺重要的呀?问题有两个
第一个 为什么每次把数据库里的全部记录写到随机文件里都多写一条空记录呢?
当我把随机文件在全部写到数据库里回有一条是(每个字段都是0的记录呢?)错误的记录 代码如下:Private Type Rect
Xh As Double
Na As String * 5
Sx As Integer
Yw As Integer
En As Integer
Su As Integer
End Type
Dim re As RectPrivate Sub 数据导出_Click()
Dim Ie As Integer
Static i As Integer
Dim j As Integer
Ie = FreeFileIf Text2.Text = "" Then
MsgBox "请选择文件入径!", vbExclamation, "提示"
Exit Sub
End IfOn Error GoTo d:Open Text2.Text For Random As #Ie Len = Len(re)
While Not EOF(Ie)
i = i + 1
Get #Ie, i, bb
Wend
Data1.Refresh
Data1.Recordset.MoveFirst
For j = 0 To Data1.Recordset.RecordCount - 1
re.Xh = Data1.Recordset.Fields(0).Value
re.Na = Data1.Recordset.Fields(1).Value
re.Sx = Data1.Recordset.Fields(2).Value
re.Yw = Data1.Recordset.Fields(3).Value
re.En = Data1.Recordset.Fields(4).Value
re.Su = Data1.Recordset.Fields(5).Value
Put #Ie, i, re
Data1.Recordset.MoveNext
i = i + 1
Next j
Close #Ie
Data1.Refresh
MsgBox "数据成功导出!", vbExclamation, "提示"
Exit Subd:
MsgBox "选择要打导出的文件不对", vbCritical, "提示"
Close #Ie
Exit SubEnd SubPrivate Sub 数据导入_Click()
Dim Ie As Integer
Static i As Integer
If Text2.Text = "" Then
MsgBox "请选择文件入径!", vbExclamation, "提示"
Exit Sub
End IfIe = FreeFile
On errror GoTo d:Open Text2.Text For Random As #Ie Len = Len(re)
While Not EOF(Ie)
i = i + 1
Get #Ie, i, re
Wend
If re.Xh = 0 And re.Na = "0" And re.Su = 0 Then
MsgBox "请确认你选择的导入文件是否正确!", vbExclamation, "提示"
Close #Ie
Exit Sub
Else
For j = 1 To i - 1
Get #Ie, j, re
Data1.Recordset.AddNew
Data1.Recordset.Fields(0).Value = Val(re.Xh)
Data1.Recordset.Fields(1).Value = re.Na
Data1.Recordset.Fields(2).Value = Val(re.Sx)
Data1.Recordset.Fields(3).Value = Val(re.Yw)
Data1.Recordset.Fields(4).Value = Val(re.En)
Data1.Recordset.Fields(5).Value = Val(re.Su)
Data1.Recordset.Update
Next j
End If
Close #Ie
MsgBox "数据成功导入!", vbInformation, "提示"
Exit Subd:
MsgBox "打开导入的文件有错误,请重新选择!", vbCritical, "提示"
Close #Ie
End Sub-----------------------------------------------------------------------
第二个问题:用什么办法删除一条随机文件,为什么我删完了以后还是以前那些记录,
最后面回多一条跟最后面第二条一样记录。Nu是一个全局变量 用来记录当前要删记录的记录号
Private Sub Command4_Click()
Dim Ie As Integer
Dim i As Integer
Dim j As Integer
Dim a As Integer
Ie = FreeFile
If Val(Text11.Text) > Val(Label12.Caption) Then
MsgBox "你输入要显示的记录号不对,请重新输入!", vbExclamation, "提示"
Exit Sub
End If a = MsgBox("你真要删除这条记录吗!", vbOKCancel + vbExclamation, "提示")
If a = vbOK Then
Open App.Path & "\ww.ee" For Random As #Ie Len = Len(Re)
If Nu = 1 Then
Get #Ie, Nu + 1, Re
Text11.Text = Nu
Else
Get #Ie, Nu - 1, Re
Text11.Text = Nu
End If
Text1.Text = Re.ZhuenKaoZheng
Text2.Text = Re.Name
If Re.XingBie = 1 Then
opt1.Value = True
Else
opt2.Value = True
End If
If Re.JiangLi = True Then
ch1.Value = 1
Else
ch1.Value = 0
End If
Text6.Text = Re.ShuXue
Text7.Text = Re.YuWen
Text8.Text = Re.WaiYu
Text9.Text = Re.WuLi
Text10.Text = Re.ZhongChengJi
Text3.Text = Re.ZhiYuan1
Text4.Text = Re.ZhiYuan2
Text5.Text = Re.LuQuXueXiao
While Not EOF(Ie)
i = i + 1
Get #Ie, i, Aa
Wend
For j = Nu To i - 1
a = j + 1
Get #Ie, a, Re
Put #Ie, j, Re Next j
Close #Ie
Nu = Nu - 1
MsgBox "记录已经删除!", vbExclamation, "提示"
End If
End Sub等大家来解决 谢谢
第一个 为什么每次把数据库里的全部记录写到随机文件里都多写一条空记录呢?
当我把随机文件在全部写到数据库里回有一条是(每个字段都是0的记录呢?)错误的记录 代码如下:Private Type Rect
Xh As Double
Na As String * 5
Sx As Integer
Yw As Integer
En As Integer
Su As Integer
End Type
Dim re As RectPrivate Sub 数据导出_Click()
Dim Ie As Integer
Static i As Integer
Dim j As Integer
Ie = FreeFileIf Text2.Text = "" Then
MsgBox "请选择文件入径!", vbExclamation, "提示"
Exit Sub
End IfOn Error GoTo d:Open Text2.Text For Random As #Ie Len = Len(re)
While Not EOF(Ie)
i = i + 1
Get #Ie, i, bb
Wend
Data1.Refresh
Data1.Recordset.MoveFirst
For j = 0 To Data1.Recordset.RecordCount - 1
re.Xh = Data1.Recordset.Fields(0).Value
re.Na = Data1.Recordset.Fields(1).Value
re.Sx = Data1.Recordset.Fields(2).Value
re.Yw = Data1.Recordset.Fields(3).Value
re.En = Data1.Recordset.Fields(4).Value
re.Su = Data1.Recordset.Fields(5).Value
Put #Ie, i, re
Data1.Recordset.MoveNext
i = i + 1
Next j
Close #Ie
Data1.Refresh
MsgBox "数据成功导出!", vbExclamation, "提示"
Exit Subd:
MsgBox "选择要打导出的文件不对", vbCritical, "提示"
Close #Ie
Exit SubEnd SubPrivate Sub 数据导入_Click()
Dim Ie As Integer
Static i As Integer
If Text2.Text = "" Then
MsgBox "请选择文件入径!", vbExclamation, "提示"
Exit Sub
End IfIe = FreeFile
On errror GoTo d:Open Text2.Text For Random As #Ie Len = Len(re)
While Not EOF(Ie)
i = i + 1
Get #Ie, i, re
Wend
If re.Xh = 0 And re.Na = "0" And re.Su = 0 Then
MsgBox "请确认你选择的导入文件是否正确!", vbExclamation, "提示"
Close #Ie
Exit Sub
Else
For j = 1 To i - 1
Get #Ie, j, re
Data1.Recordset.AddNew
Data1.Recordset.Fields(0).Value = Val(re.Xh)
Data1.Recordset.Fields(1).Value = re.Na
Data1.Recordset.Fields(2).Value = Val(re.Sx)
Data1.Recordset.Fields(3).Value = Val(re.Yw)
Data1.Recordset.Fields(4).Value = Val(re.En)
Data1.Recordset.Fields(5).Value = Val(re.Su)
Data1.Recordset.Update
Next j
End If
Close #Ie
MsgBox "数据成功导入!", vbInformation, "提示"
Exit Subd:
MsgBox "打开导入的文件有错误,请重新选择!", vbCritical, "提示"
Close #Ie
End Sub-----------------------------------------------------------------------
第二个问题:用什么办法删除一条随机文件,为什么我删完了以后还是以前那些记录,
最后面回多一条跟最后面第二条一样记录。Nu是一个全局变量 用来记录当前要删记录的记录号
Private Sub Command4_Click()
Dim Ie As Integer
Dim i As Integer
Dim j As Integer
Dim a As Integer
Ie = FreeFile
If Val(Text11.Text) > Val(Label12.Caption) Then
MsgBox "你输入要显示的记录号不对,请重新输入!", vbExclamation, "提示"
Exit Sub
End If a = MsgBox("你真要删除这条记录吗!", vbOKCancel + vbExclamation, "提示")
If a = vbOK Then
Open App.Path & "\ww.ee" For Random As #Ie Len = Len(Re)
If Nu = 1 Then
Get #Ie, Nu + 1, Re
Text11.Text = Nu
Else
Get #Ie, Nu - 1, Re
Text11.Text = Nu
End If
Text1.Text = Re.ZhuenKaoZheng
Text2.Text = Re.Name
If Re.XingBie = 1 Then
opt1.Value = True
Else
opt2.Value = True
End If
If Re.JiangLi = True Then
ch1.Value = 1
Else
ch1.Value = 0
End If
Text6.Text = Re.ShuXue
Text7.Text = Re.YuWen
Text8.Text = Re.WaiYu
Text9.Text = Re.WuLi
Text10.Text = Re.ZhongChengJi
Text3.Text = Re.ZhiYuan1
Text4.Text = Re.ZhiYuan2
Text5.Text = Re.LuQuXueXiao
While Not EOF(Ie)
i = i + 1
Get #Ie, i, Aa
Wend
For j = Nu To i - 1
a = j + 1
Get #Ie, a, Re
Put #Ie, j, Re Next j
Close #Ie
Nu = Nu - 1
MsgBox "记录已经删除!", vbExclamation, "提示"
End If
End Sub等大家来解决 谢谢
RS.SAVE 文件名,文件格式.
*注:文件格式=ADPERSISTADTG,将记录集以MICROSOFT所有的ADVANCED DATA TABLEGRAM格式保存,是默认方式.用该格式文件很小.
文件格式=ADPERSISTXML,则保存为XML格式.用XML文件有点点"胖".加载
RS.OPEN 文件名,,ADOPENSTATIC
这样,你就可以用RS操作该文件了.比自已另起一类要方便快捷得多.
即临时表类型和锁类型.
而且,这样做以后,你一旦设定了一个CNN给RS,你一样可以象更新其它记录集一样更新该记录集.兄弟,这样省事了吧?
最后面回多一条跟最后面第二条一样记录。
----------------------------------
我以前也遇到你的问题,可以这样看看吧:
当查找到你要删除的记录,并完成读取后,记录的读指针就指向下一条,
如果写操作时,就会出现你的问题了,所以当写时候,要把读指针重新定位到要
删除的记录上。我是用PASCAL写的。
另外我看你的删除动作为何那么复杂? 可不可以在结构中建立一个删除标志字段?
当找到时候,置一个标志就行。也可以象我前面那样用临时文件处理,速度也很快。
我试试 看
过几天揭贴
my e-mail: [email protected]