尝试使用字符串时,创建一个新表错误91

时间:2018-01-21 19:46:18

标签: ms-access access-vba ms-access-2010

我正在尝试从表单创建一个新表。我需要动态表名和动态字段名。我试图将strtablename更改为没有运气的对象。我收到错误91.感谢任何帮助。

Private Sub cmdNewTable_Click()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim NewField As Field
Dim strNewTable As String
Dim objNewTable As Object
Dim x As Long
Dim y As Long
Dim strFieldName As String
Dim FieldTyoe As String
strNewTable = InputBox("Enter Table Name")
If strNewTable = vbNullString Then GoTo Skip:

   'Create New Table
    Set tdf = db.CreateTableDef(strNewTable)

y = InputBox("Enter amount of fields to add")

For x = 1 To y
   With tdf
    strFieldName = InputBox("Enter field Name")
    strfieldType = InputBox("Enter Field Type 1-Text 2-Integer 3-Long 4-
Single 5-Double 6-Decimal 7-Date 8-Currency 9-Yes/No 10-Memo")
    If strfieldType = 1 Then strfieldType = "dbText"
    If strfieldType = 2 Then strfieldType = "dbInteger"
    If strfieldType = 3 Then strfieldType = "dbLong"
    If strfieldType = 4 Then strfieldType = "dbSingle"
    If strfieldType = 5 Then strfieldType = "dbDouble"
    If strfieldType = 6 Then strfieldType = "dbDecimal"
    If strfieldType = 7 Then strfieldType = "dbDate"
    If strfieldType = 8 Then strfieldType = "dbCurrency"
    If strfieldType = 9 Then strfieldType = "dbBoolean"
    If strfieldType = 10 Then strfieldType = "dbMemo"

    Debug.Print strFieldName
    Debug.Print strfieldType
    Stop
    .Fields.Append .CreateField(strFieldName, strfieldType)

End With

1 个答案:

答案 0 :(得分:0)

首先检查您的声明:Dim FieldTyoe As String

这是一个错字。请务必使用Option Explicit。设为Dim strfieldType As String

然后看看:If strfieldType = 1

这是非法评估。

要修复,请将数字放在引号中: If strfieldType = "1"

您结束了With阻止,但我看不到Next xEnd Sub。如果用户输入无效,您还需要在此处进行错误处理。如果他们使用保留字符或单词怎么办?