1
下面的代碼的開始應該獲取窗體上的文本框中的文件名的BaseName並將它設置爲一個字符串「tblnewbid」。 Debug.Print tblnew bid會正確打印正確的表名稱。我驗證了字符完全匹配,並且有一個與該名稱匹配的實際表。字符串不工作作爲表名稱 - 訪問VBA -
當我運行代碼時出現錯誤:找不到表或約束。如果我手動將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
那肯定讓我失去了那個錯誤。非常感謝你。但是,一旦我閱讀了你的答案,就有必要在整個代碼中做這件事。 (見本評論的結尾)但是當我這樣做時,我得到和下一行中的錯誤,說:太少的參數。預計1.'設置rs = db.OpenRecordset(「從」&tblnewbid & " ;「選擇[OriginState])'任何想法? –
Nevermind。我發現實際表中的錯誤。字段名稱不正確。但我確信它可以正常工作 –
它確實運行得非常好,謝謝! –