我正在编写代码来清除Access表并多次将数据导入表中。所以我想我可以将它放入一个函数中并在for循环中调用它11次而不是编写代码来执行它11次。
我的代码如下。 Clear_Import函数中的变量tbl将返回,因为我想要TblNm + I.但它作为字符串而不是变量答案应该是TblNm1 =“Emb Enrollment”。我是以错误的方式解决这个问题吗?
Option Compare Database
Global fileID_1 As String
Global fileID_2 As String
Global FileNm As String
Global TblNm1 As String
Global TblNm2 As String
Global TblNm3 As String
Global TblNm4 As String
Global TblNm5 As String
Global TblNm6 As String
Global TblNm7 As String
Global TblNm8 As String
Global TblNm9 As String
Global TblNm10 As String
Global TblNm11 As String
Global FldrPath As String
Global UserInputPath As Variant
Global var As String
''''''''''''''''''''''''''''''''
'All Clear and Import Functions
''''''''''''''''''''''''''''''''
Function AllClearImport()
MsgFileRun = MsgBox("Did you save ALL the necessary files?", vbYesNo, "File Check")
If MsgFileRun = vbYes Then
'Yes Answer
'''Variables
'Hard Code Path
'FldrPath
'Database Path
FldrPath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
FileNm = "SA_Report_Template.xlsm"
TblNm1 = "Emb Enrollment"
TblNm2 = "Emb Escalations"
TblNm3 = "Emb Non-Phone"
TblNm4 = "Emb Pending"
TblNm5 = "Mcc Electronic"
TblNm6 = "Mcc Non-Phone"
TblNm7 = "Mcc Non-Phone 210"
TblNm8 = "Mcc Pending"
TblNm9 = "Nsa All Loc"
TblNm10 = "Nsa Grp"
TblNm11 = "Sbu Written"
fileID = Format(Date, "MMDDYYYY")
fileID1 = Format(Date, "MMDDYYYY")
For i = 1 To 11
var = "TblNm" & i
Clear_Import ("TblNm" & i)
Next i
'No Answer
Else
'User Cancel or Blank Check
If UserInputPath = "" Then
MsgBox ("You clicked NO! Stopping Process.")
'CrashAllBat
Dim PathCrnt As String
PathCrnt = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
Call Shell(PathCrnt & "\CrashAll.bat ")
Exit Function
End If
End If
End Function
Function Clear_Import(tbl As String)
'''Clear Stuff
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete [" & tbl & "].* FROM [" & tbl & "];"
DoCmd.SetWarnings True
'''Import Stuff
DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel9, "" & tbl & "", _
"" & FldrPath & "" & tbl & "", True, "" & tbl & "$"
End Function
答案 0 :(得分:3)
您无法通过以字符串形式给出的名称来访问变量。你应该使用一个数组。它允许您遍历其值。您可以将阵列视为具有抽屉的橱柜,每个抽屉可以容纳一个值。您可以通过索引访问各个抽屉(在答案中也是Dave),或者使用For Each语句迭代所有抽屉。
Function AllClearImport()
Dim TableNames As Variant, tbl As Variant
'...
TableNames = Array("Emb Enrollment", "Emb Escalations", "Emb Non-Phone", ... )
For Each tbl In TableNames
Clear_Import tbl
Next
'...
End Function
Sub Clear_Import(ByVal tbl As String)
'...
End Sub
此外,由于for-each-statement的迭代变量是Variant
,因此将表名称ByVal
参数传递给Clear_Import
,否则参数默认为{{ 1}}和那些无法从ByRef
转换为Variant
。
您在模块开头缺少String
。使用它是一种好习惯。它在使用之前强制使用Option Explicit
声明变量。这是一项更多的工作,但它使代码更易读,更不容易出错。
更可读,因为在声明像
这样的变量时Dim
它使程序员的意图对任何人都清楚。现在很明显Dim x As Double
应该包含一个双精度值。
不易出错。如果没有x
,当变量名拼写错误时,您将不会立即注意到,因为VBA会自动创建一个带有拼写错误名称的新的空Variant变量。使用Option Explicit
,您将在编译时收到错误消息。
也可以使用局部声明(Dim语句),即尽可能在Functions和Subs中声明。制作仅在全球范围内可访问的变量毫无意义。
答案 1 :(得分:0)
你正在传递字符串" TblNm" &安培;我(1到11岁)......
TblNm = Array("Emb Enrollment","Emb Escalations","Emb Non-Phone","Emb Pending","Mcc Electronic","Mcc Non-Phone","Mcc Non-Phone 210","Mcc Pending","Nsa All Loc","Nsa Grp","Sbu Written")
fileID = Format(Date, "MMDDYYYY")
fileID1 = Format(Date, "MMDDYYYY")
For i = 0 To UBound(TblNm)
Clear_Import (TblNm(i))
Next i
尝试在数组中设置表名并将其传入,如图所示
答案 2 :(得分:0)
与提到的Dave相同,您要求Clear_Import
根据需要使用TblNm1
而不是Emb Enrollment
。
下面是一个选项(与提供的Dave相同,但可能更清楚一点)。
Option Compare Database
Global fileID_1 As String
Global fileID_2 As String
Global FileNm As String
Global AryTbls(10) As String 'Declared an array for your table names
Global FldrPath As String
Global UserInputPath As Variant
Global var As String
Public Function AllClearImport()
Dim LngCounter As Long
'Use the result of the question directly (rather than drop it in a variable then read the variable
If MsgBox("Did you save ALL the necessary files?", vbYesNo, "File Check") = vbYes Then
FldrPath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
FileNm = "SA_Report_Template.xlsm"
'Set your table names in our array
AryTbls(0) = "Emb Enrollment"
AryTbls(1) = "Emb Escalations"
AryTbls(2) = "Emb Non-Phone"
AryTbls(3) = "Emb Pending"
AryTbls(4) = "Mcc Electronic"
AryTbls(5) = "Mcc Non-Phone"
AryTbls(6) = "Mcc Non-Phone 210"
AryTbls(7) = "Mcc Pending"
AryTbls(8) = "Nsa All Loc"
AryTbls(9) = "Nsa Grp"
AryTbls(10) = "Sbu Written"
fileID = Format(Date, "MMDDYYYY")
fileID1 = Format(Date, "MMDDYYYY")
'Go from the bottom of the array to the top
For LngCounter = 0 To ubound(AryTbls,1)
Clear_Import AryTbls(LngCounter)
Next
Else
MsgBox "You clicked NO! Stopping Process."
'You was setting a variable to the same path as a variable you had already set (FldrPath) to the same thing so I re-used it here.
Call Shell(FldrPath & "\CrashAll.bat ")
End If
End Function
Function Clear_Import(tbl As String)
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete [" & tbl & "].* FROM [" & tbl & "];"
DoCmd.SetWarnings True
DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel9, "" & tbl & "", _
"" & FldrPath & "" & tbl & "", True, "" & tbl & "$"
End Function