在access vba中添加DBfailonerror会导致编译错误无效使用属性

时间:2014-01-11 23:53:12

标签: ms-access access-vba

在访问中创建的导入子工作正常但是当添加DBfailonerror时编译错误当从vb编辑器运行子时遇到无效的属性使用。

任何建议:最值得赞赏的。代码如下:

Sub Importcl()

'DATA DECLARATIONS
Dim fso As New FileSystemObject
Dim t As TextStream
Dim strFilePath As String
Dim Cnr As String
Dim Cnri As String
Dim Cnrii As String
Dim Cnriii As String
Dim Sqlstr As String
Dim Db As DAO.Database


'SET COUNTERS TO ZERO
Cnr = 0
Cnri = 0
Cnrii = 0
Cnriii = 0

'Point to DB
Set Db = CurrentDb()

'SET TXT FILE PATH
strFilePath = "C:\Users\Vlad\CSV import\EV WORK\Book1.txt"


'ERROR HANLDER FOR TXT FILE PATH AND COUNTING OF TXT FILE LINE ITEMS
If fso.FileExists(strFilePath) Then
Set t = fso.OpenTextFile(strFilePath, ForReading, False)
    Do While t.AtEndOfStream <> True
        t.SkipLine
        Cnr = Cnr + 1
    Loop
        t.Close
Else: MsgBox ("Txt File not Found - Check File Path")
Exit Sub
End If

'DISPLAY LINE RECORDS COUNTED IN TXT FILE TO BE ADDED TO TABLE
Debug.Print Cntr; " Incl header"
MsgBox (Cnr - 1 & " records to be added")

'COUNT & DISPLAY CURRENT RECORD COUNT IN TARGET TABLE
Cnri = DCount("[Case Date]", "All Caseload Data New")


If MsgBox(Cnri & " -Current Records in table- All Caseload Data New - Continue
with Import?", vbYesNo, "Import") = vbYes Then
Db.Execute _
"INSERT INTO [All Caseload Data New] SELECT * FROM[Text;FMT=Delimited;HDR=Yes;
DATABASE=C:\Users\Dev\CSV import\DEV WORK\].[Book1#txt];"
dbFailOnError
Db.TableDefs.Refresh
Else: Exit Sub

End If

Cnrii = DCount("[Case Date]", "All Caseload Data New")

Cnriii = Cnrii - Cnri

MsgBox (Cnriii & " New records added to table All Caseload Data New")


End Sub

1 个答案:

答案 0 :(得分:1)

使用此代码开始......

Dim db As DAO.database
Dim strInsert As String
strInsert = "INSERT INTO tblFoo (some_text) VALUES ('bar');"
Set db = CurrentDb

然后这两个Execute语句..

db.Execute strInsert
dbFailOnError ' triggers error
db.Execute strInsert, dbFailOnError ' compiles without error

dbFailOnErrorExecute放在同一行。将dbFailOnError放在单独的行上会触发“无效使用属性”编译错误。