以下代码的开头应该在表单上的文本框中获取文件名的BaseName,并将其设置为String" tblnewbid"。 Debug.Print tblnew bid完全按照预期打印出正确的Table名称。我确切地验证了字符匹配,并且有一个与该名称匹配的实际表。
当我运行代码时,我收到错误:无法找到表或约束。如果我手动将tblnew更改为实际的表名,代码运行正常。可能是我需要将tblnewbid作为字符串以外的其他东西进行调整吗?
Private Sub btnInsertRegions_Click()
DoCmd.Hourglass True ' turn on Hourglass
Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim FSO As New FileSystemObject
Dim tblnewbid As String
Set db = CurrentDb
tblnewbid = FSO.GetBaseName(Me.txtFileName)
Debug.Print tblnewbid
db.Execute "ALTER TABLE tblnewbid ADD COLUMN O_StateRegion CHAR", dbFailOnError
db.Execute "ALTER TABLE tblnewbid ADD COLUMN D_StateRegion CHAR", dbFailOnError
Set rs = db.OpenRecordset("Select [OriginState] from [tblnewbid];")
rs.MoveFirst
strSQL = "UPDATE [tblnewbid] INNER JOIN [tblStates]"
strSQL = strSQL & " ON [tblStates].[StateAbbrev] = [tblnewbid].[OriginState]"
strSQL = strSQL & " SET [tblnewbid].[O_StateRegion]=[tblStates].[StateRegion]"
db.Execute (strSQL), dbFailOnError
rs.MoveFirst
strSQL = "UPDATE [tblnewbid] INNER JOIN [tblStates]"
strSQL = strSQL & " ON [tblStates].[StateAbbrev] = [tblnewbid].[DestinationState]"
strSQL = strSQL & " SET [tblnewbid].[D_StateRegion]=[tblStates].[StateRegion]"
db.Execute (strSQL), dbFailOnError
rs.Close
Set rs = Nothing
答案 0 :(得分:1)
当您将变量放在双引号内时,它将停止变为变量并成为文字字符串。
您要做的是将变量评估,然后放在字符串内。您可以通过连接来执行此操作:
即:
db.Execute "ALTER TABLE " & tblnewbid & " ADD COLUMN O_StateRegion CHAR", dbFailOnError
将此串联应用于需要评估变量的每一行。