|
回复 54楼 hysj02 的帖子
我使用access还发现,Docmd.Runsql似乎无法在模块中成功运行,总的来说就是单步运行可以成功,但一次运行就会失败,不知是何原因。http://club.excelhome.net/thread-415211-1-1.html,麻烦哪位帮忙解答一下,谢谢。
'=======================
不存在此问题,关键是朋友你把简单的问题复杂化,初用者,要用使用EXCELL的眼光去理解用ACCESS,ACCESS象EXCELL一样是个平民化的数据库设计平台。
你的例子是导入 .txt 文件,主要是 .txt 文件一般都达不到ACCESS的直接导入条件,比如你例子中的 .txt 文件没标题和数据导入后多空格,必须处理,设计数据库的第一步先导入一个标准表设置好标准me,使ACCSS认识的标准后,就可正常使用了。
下面所用到的全部代码
Option Compare Database
Private Sub Command0_Click()
'=====================µ¼ÈëÊý¾Ý
DoCmd.SetWarnings False
If ExistTable("t_TmpSUP") = True Then
DoCmd.RunSQL "delete * from t_TmpSUP"
End If
Dim ss, ss1, tempstr, iline, aa, txetname
ss = ""
txetname = Me.TxetA
aa = CurrentProject.Path & "\" & txetname & ".txt"
Open aa For Input As #1
iline = 0
Do While Not EOF(1)
Line Input #1, tempstr
tempstr = Trim(Mid(tempstr, 1, 20)) & "," & Trim(Mid(tempstr, 21, 22)) & "," & Trim(Mid(tempstr, 34))
ss = ss & tempstr & vbCrLf
iline = iline + 1
Loop
Close #1
Open CurrentProject.Path & "\abc.txt" For Output As #2
Print #2, ss
Close #2
DoCmd.TransferText acImportDelim, "me", "t_TmpSUP", CurrentProject.Path & "\abc.txt", False
If Dir(CurrentProject.Path & "\abc.txt") <> "" Then
Kill CurrentProject.Path & "\abc.txt"
End If
'=====================×·¼ÓÊý¾Ý
DoCmd.RunSQL "delete * from t_SUP where SUP in (select SUP from t_TmpSUP);", False
DoCmd.RunSQL "INSERT INTO t_SUP SELECT * FROM t_TmpSUP;", False
DoCmd.SetWarnings True
End Sub
Private Function ExistTable(strTableName As String) As Integer
Dim i As Integer
ExistTable = False
For i = 0 To CurrentData.AllTables.Count - 1
If strTableName = CurrentData.AllTables(i).Name Then
ExistTable = True
Exit For
End If
Next i
End Function |
|