【问题标题】:"Duplicate declaration in current scope" error in Access VBAAccess VBA 中的“当前范围内的重复声明”错误
【发布时间】:2016-04-07 11:28:30
【问题描述】:

我制作的 VBA 程序有问题。我想创建一个程序,它在整个表中输入 50,000 条记录(在我的例子中是 Employee Table),每次我尝试运行它时,它都会显示一个错误,上面写着 "Compile Error: Duplicate declaration in current scope."

我的代码如下:

Option Compare Database
Option Explicit

Sub arrayData1()

'This subroutine will pump in 50 k records for the first two columns of EMPLOYEE table.
'Also takes in sample names, attempts to clean the data beofre its entered in the table.
'Declare variable by using keyword DIM
Dim EmployeeFNames() As Variant  'implies array. array is always declared variant datatype.
Dim EmployeeLNames() As Variant
Dim EmployeeType() As Variant
Dim num As Integer, dbs As Database, InsertRecord As Variant, num1 As Long
Dim EmployeeID As Long, EmployeeFName As String, EmployeeLName As String, EmployeeType As String, EmployeeWages As Long

'assign value to variables
Set dbs = CurrentDb() 'assign current db(Stage 2 Project)
EmployeeID = 0 'initialise value.

    For num1 = 0 To 50000
    EmployeeID = EmployeeID + 1 'increment by 1.
    EmployeeWages = EmployeeWages + 1
    ' array is populated with names.
    EmployeeFNames = Array("Peter", "Mary", "Frances", "Paul", "Ian", "Ron", "Nathan", "Jesse", "John", "David")
    EmployeeLNames = Array("Jacobs", "Smith", "Zane", "Key", "Doe", "Patel", "Chalmers", "Simpson", "Flanders", "Skinner")
    EmployeeTypes = Array("Groundskeeper", "Housekeeper", "Concierge", "Front Desk", "Chef", "F&B", "Maintenance", "Accounts", "IT", "Manager")

    'Equation for random generation
    'INT (upperbound - lowerbound +1) * Rnd + lowerbound) ' upper & lower bound are index values of array
    num = Int((9 - 0 + 1) * Rnd + 0) ' equation generates at random a number between 0 & 9.
    EmployeeFName = EmployeeFNames(num) ' name is picked at random from array based on random number.
    EmployeeLName = EmployeeLNames(num)
    EmployeeType = EmployeeTypes(num)

    ' Use SQL INSERT statement to insert record in EPLOYEE table.
    InsertRecord = "INSERT INTO EMPLOYEE(EmployeeID, EmployeeFName, EmployeeLName, EmployeeType, EmployeeWages) VALUES(" _
                   & "'" & EmployeeID & "'" & "," & "'" & EmployeeFName & "'" & "," & "'" & EmployeeLName & "'" & "," & "'" & EmployeeType & "'" & "," & "'" & EmployeeWages & "'" & ")"

    dbs.Execute InsertRecord
    Debug.Print EmployeeID; EmployeeFName; EmployeeLName; EmployeeType; EmployeeWages
    Next

End Sub

如果能对此问题进行任何修复以及对我的代码提出任何建议,我将不胜感激。

【问题讨论】:

    标签: vba ms-access-2010


    【解决方案1】:

    您曾尝试将变量 EmployeeType 声明(Dim)为 Variant 的数组,然后您尝试(再次)将其声明为 String

    您需要为这两个变量使用两个不同的名称。

    【讨论】:

    • 我遇到了同样的错误,我意识到我正在重用的函数具有与我声明的名称相同的参数。
    • @Dean 谢谢,您的建议是我遇到的问题。您应该将其添加为答案,以便我对其进行投票,其他人可以轻松找到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多