本文介绍了多个文本文件“文件已经打开”错误(VB6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是我的代码,我尝试打印与数据库中的月份相对应的多个文本文件(例如,如果1-> 1月.TXT,如果2-> 2月.TXT,依此类推......)。数据库表中有3行,其中输出应该是3个带有数据的文件,一个是January.TXT,February.TXT和March.TXT。错误文件已经打开出现了。它应该打印3个文本文件对于数据,所有3个都被打印但是,只有第一个有数据,其余两个是空的,错误文件已经打开。请帮助谢谢。 vSQL = vSQL& GROUP BY SOA_COMPNO,SOA_FAC,EEP_NAME,EEP_IC,EEP_SOCSONO,dtjoin,dtresign ORDER BY SOA_COMPNO,SOA_FAC,EEP_NAME uSQL =SELECT soa_month FROM DMF_SocsoArrears WHERE SOA_PAYMONTH =&月(dtpSocsoDate.Value)& 和& _ SOA_PAYYEAR =&年(dtpSocsoDate.Value)& AND(SOA_SOAMTEE + SOA_SOAMTER)> 0 RS1.Open vSQL,Conn,adOpenStatic,adLockReadOnly RS2.Open uSQL,Conn,adOpenStatic,adLockReadOnly 而非RS2.EOF 选择案例RS2!soa_month 案例1 tFileName =January.TXT案例2 tFileName =2月.TXT Case3 tFileName =March.TXT Case4 tFileName =April.TXT Case5 tFileName =May.TXT案例6 tFileName =June.TXT案例7 tFileName =July.TXT案例8 tFileName =August.TXT案例9 tFileName =September.TXT案例10 tFileName = October.TXT案例11 tFileName =November.TXT案例12 tFileName =December.T XT结束选择 intFileHandle2 = FreeFile Dim sPath As String sPath = txtSocsoLoc.Text& tFileName 打开sPath输出为#intFileHandle2 如果RS1.RecordCount> 0然后而不是RS1.EOF strJoin = IIf(& RS1!dtjoin =,,格式(& RS1!dtjoin,ddMMyyyy)) strResign = IIf(& RS1!dtresign =,,格式(& RS1!dtresign,ddMMyyyy)) strJoinResign = Left(strJoin&空格(8),8)如果strResign<> 然后strJoinResign = strResign strStatus = IIf(strJoin<>,B,) strStatus = IIf(strResign<>, H,strStatus) StrInput2 = StrInput2 =左(RS1!KOD& Space(12),12)&空间(20)&左(RS1!eep_ic& Space(12),12)&左(RS1!eep_name& Space(150),150)&格式(dtpSocsoDate.Value,MMyyyy)&右(替换(空格(14),,0)& RS1!amount_socso,14)& strJoinResign& strStatus 打印#intFileHandle2,StrInput2 RS1.MoveNext Wend 结束如果 RS2。 MoveNext Wend 关闭#intFileHandle2 设置RS1 =无设置RS2 =无致电SDefault(Me) MsgBox每月Socso Arrear文件已成功保存。,vbInformation,Me.Caption 我有什么尝试过: 我试图改变open的位置,并在很多地方关闭intfile,没有工作。 解决方案 Here is my code, I tried to print multiple text file corresponding to the months in the database ( eg, if 1-> January.TXT, if 2-> February.TXT, and so on.. ). There are 3 lines in the database table, where the outputs should be 3 files with data, one is January.TXT, February.TXT and March.TXT.The error "File is already open" came up.It supposed to print 3 textfiles with data, all the 3 are printed but, only the first one has data, the rest two is empty, with the error "File is already open". Please help thank you.vSQL = vSQL & " GROUP BY SOA_COMPNO, SOA_FAC, EEP_NAME, EEP_IC, EEP_SOCSONO, dtjoin, dtresign ORDER BY SOA_COMPNO, SOA_FAC, EEP_NAME " uSQL = "SELECT soa_month FROM DMF_SocsoArrears WHERE SOA_PAYMONTH = " & Month(dtpSocsoDate.Value) & " AND " & _ "SOA_PAYYEAR = " & Year(dtpSocsoDate.Value) & " AND (SOA_SOAMTEE+SOA_SOAMTER) > 0 " RS1.Open vSQL, Conn, adOpenStatic, adLockReadOnly RS2.Open uSQL, Conn, adOpenStatic, adLockReadOnly While Not RS2.EOF Select Case RS2!soa_month Case "1" tFileName = "January.TXT" Case "2" tFileName = "February.TXT" Case "3" tFileName = "March.TXT" Case "4" tFileName = "April.TXT" Case "5" tFileName = "May.TXT" Case "6" tFileName = "June.TXT" Case "7" tFileName = "July.TXT" Case "8" tFileName = "August.TXT" Case "9" tFileName = "September.TXT" Case "10" tFileName = "October.TXT" Case "11" tFileName = "November.TXT" Case "12" tFileName = "December.TXT" End Select intFileHandle2 = FreeFile Dim sPath As String sPath = txtSocsoLoc.Text & tFileName Open sPath For Output As #intFileHandle2 If RS1.RecordCount > 0 Then While Not RS1.EOF strJoin = IIf("" & RS1!dtjoin = "", "", Format("" & RS1!dtjoin, "ddMMyyyy")) strResign = IIf("" & RS1!dtresign = "", "", Format("" & RS1!dtresign, "ddMMyyyy")) strJoinResign = Left(strJoin & Space(8), 8) If strResign <> "" Then strJoinResign = strResign strStatus = IIf(strJoin <> "", "B", " ") strStatus = IIf(strResign <> "", "H", strStatus) StrInput2 = "" StrInput2 = Left(RS1!KOD & Space(12), 12) & Space(20) & Left(RS1!eep_ic & Space(12), 12) & Left(RS1!eep_name & Space(150), 150) & Format(dtpSocsoDate.Value, "MMyyyy") & Right(Replace(Space(14), " ", "0") & RS1!amount_socso, 14) & strJoinResign & strStatus Print #intFileHandle2, StrInput2 RS1.MoveNext Wend End If RS2.MoveNext Wend Close #intFileHandle2 Set RS1 = Nothing Set RS2 = Nothing Call SDefault(Me) MsgBox "Monthly Socso Arrear Files have successfully saved.", vbInformation, Me.CaptionWhat I have tried:I have tried to change the position of the open, and "close intfile"at many places, non worked. 解决方案 这篇关于多个文本文件“文件已经打开”错误(VB6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 22:20