在VBA Access中构建表名称

时间:2014-11-14 17:47:29

标签: sql vba ms-access access-vba

我正在尝试从access vba中的用户输入构建表名。他们将输入与已存在的表对应的特定日期。当我尝试使用字符串连接执行此操作时,我在尝试编译时遇到无效限定符错误。我创建的名称声明为字符串。我应该宣布它是不同的东西吗?任何帮助将不胜感激。

Sub RevH()
Dim dte As String, db As Database, tableName As String

Set db = CurrentDb


dte = InputBox("What date was the Data Dump run?", "Please Input a date")

tableName = "FN_DataDump_ALL_" & dte
tableName.MoveFirst
Do While Not tableName.EOF
If tableName("[Client ID]") <> clientTable("[Client ID]") Then
   MsgBox ("No Match.")
   clientTable.MoveNext
Else: MsgBox ("Match.")
End If




Loop







End Sub

1 个答案:

答案 0 :(得分:0)

您需要打开您所引用的表格。仅使用字符串是不够的。

像这样的东西

sub someSub()
Dim dte As String, db As Database, tableName As String, _
    rs as RecordSet, tbl as TableDef, tbl_found as boolean

Set db = currentDb()
dte = InputBox("What date was the Data Dump run?", "Please Input a date")
' Somehow you need to validate that the date is well formed... I leave that to you
tableName = "FN_DataDump_ALL_" & dte
' First check if the table is in the database
tbl_found = false
for each tbl in db.tableDefs
    if tbl.name = tableName then
        tbl_found = true
        exit for
    end if
next tbl
' If the table is not in the database, show an error message and exit the sub
if not tbl_found then
    msgBox("Table not found!")
    exit sub
end if
' Now open the table
set rs = db.OpenRecordset(tableName, dbOpenDynaset, dbEditAdd)
' And NOW you can iterate over the table (the RecordSet)
with rs
    .moveFirst ' Go to the first row of the record set
    do
        ' Do whatever you want to do
        .moveNext
    loop until .EOF
    .close ' Always close recordsets
end with
db.close ' Also, always close database objects (this won't close your database, it will clean-up the object)
end sub