gpt4 book ai didi

vba - 打印成功和失败消息到输出文件,错误处理程序来不及?

转载 作者:行者123 更新时间:2023-12-03 08:44:23 24 4
gpt4 key购买 nike

我在VBA中做了一些代码,可以将链接表导入MS-Access。成功添加表后,应将其打印到输出文件中。发生错误时,应在输出文件中说明。我的代码的当前输出为添加时有错误的表返回成功和失败的行。我需要在代码中进行哪些更改以仅在输出文件中显示成功或失败的行?

Sub CallAddTable()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim exportLocation As String
Dim exportFile As String

exportLocation = "xxxx"
exportFile = exportLocation & "\yyyy.csv"

Set db = CurrentDb
Set rst = db.OpenRecordset("ToBeAdded")

Open exportFile For Output As #1

Do While Not rst.EOF
On Error GoTo ErrorHandler
Call AddTable(rst!Acces_table_name, rst!Source_table_name)
rst.MoveNext
Print #1, "Succes: " & rst!Acces_table_name & vbTab & rst!Source_table_name
Loop

Close #1

Exit Sub

ErrorHandler:
Print #1, "Failed: " & rst!Acces_table_name & vbTab & rst!Source_table_name & vbTab & Err.Number & vbTab & Err.Description
Resume Next
End Sub

Sub AddTable(AccessTableName As String, SourceTableName As String)
' we will need to create this table using DAO
Dim tdf As DAO.TableDef

' Some variable to make the code more generic
Dim strConnectionString As String
Dim strNameInAccess As String
Dim strNameInSQLServer As String

' set the connection string
strConnectionString = "ODBC;DRIVER={xxxx};Uid=xxxx;Pwd=xxxx;Dbq=xxxxx;Trusted_Connection=Yes"

' specify the tables you want to link. The table can be
' known by a different name in Access than the name in SQL server

strNameInAccess = AccessTableName
strNameInSQLServer = SourceTableName

' Create a table using DAO give it a name in Access.
' Connect it to the Source.
' Say which table it links to in Source.

Set tdf = CurrentDb.CreateTableDef(strNameInAccess)
tdf.Connect = strConnectionString
tdf.SourceTableName = strNameInSQLServer

' Add this table Definition to the collection
' of Access tables
CurrentDb.TableDefs.Append tdf

End Sub

日志文件首先将表显示为成功,如果存在错误,则下一行将表显示为失败。
"Succes: SOURCETBL_VALUE    SOURCETBL.VALUE"
"Failed: SOURCETBL_VALUE SOURCETBL.VALUE 3011 Description."

最佳答案

根据注释,更改代码,以便在AddTable函数中进行错误处理。现在,每个添加的表仅一行被写入.csv文件。

关于vba - 打印成功和失败消息到输出文件,错误处理程序来不及?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57607123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com