我有一个表,我正在尝试编写一个模块,该模块将进入并向现有表添加两列。我有一个名称中有空格的表,我收到编译错误:预期标识符的错误。
Private Sub AddColumn()
Dim curDatabase As Database
Dim [Final LOS and PAPW TABLE] As TableDef
Dim Yrs_of_Svc As Field
Dim Yrs_of_Svc_Cat As Field
' Get a reference to the current database
Set curDatabase = CurrentDb
' Get a reference to a table named TestTable - NOTE: The table MUST exist
Set [Final LOS and PAPW TABLE] = curDatabase.TableDefs("[Final LOS and PAPW TABLE]")
'define the fields using the CreateField method
' Syntax: .CreateField("FIELD NAME", TYPE, [Length])
' I used the predefined types already in Access
Set Yrs_of_Svc = [Final LOS and PAPW TABLE].CreateField("Yrs_of_Svc", dbDouble, 5)
Set Yrs_of_Svc_Cat = [Final LOS and PAPW TABLE].CreateField("Yrs_of_Svc_Cat", dbText, 2)
If Len(Offc_TRMN_DT) > 1 Then
[Final LOS and PAPW TABLE].Yrs_of_Svc = [Final LOS and PAPW TABLE].Offc_TRMN_DT - [Final LOS and PAPW TABLE].Offc_EMPLMT_DT
Else
[Final LOS and PAPW TABLE].Yrs_of_Svc = [Final LOS and PAPW TABLE].BSE_ISS_RGST_DT - [Final LOS and PAPW TABLE].Offc_EMPLMT_DT
End If
Select Case Yrs_of_Svc ' Evaluate Number.
Case 0 To 1.99
[Final LOS and PAPW TABLE].Yrs_of_Svc_Cat = 1
Case 2.01 To 2.99
[Final LOS and PAPW TABLE].Yrs_of_Svc_Cat = 2
Case 3.01 To 3.99
[Final LOS and PAPW TABLE].Yrs_of_Svc_Cat = 3
Case 4.01 To 4.99
[Final LOS and PAPW TABLE].Yrs_of_Svc_Cat = 4
Case Is > 5#
[Final LOS and PAPW TABLE].Yrs_of_Svc_Cat = 5+
End Select
End Sub
感谢您的帮助。
罗伯特
答案 0 :(得分:1)
变量名称不必与表名相同,在这种情况下它绝对不应该是因为你不能在变量名中有空格。括号在VBA中的功能与在SQL中的功能不同。使用CamelCase(FinalLosAndPapwTable
)或使用下划线(Final_LOS_and_PAPW_TABLE
)代替空格。
有关命名click here的更多信息。
您似乎也在尝试为Yrs_of_Svc
和Yrs_of_Svc_Cat
分配值,但这些是Field
个对象,所以我不认为您可以这样做。如果在修复变量名后收到更多错误,请回复。