vba - Access VBA 中的 "Duplicate declaration in current scope"错误

标签 vba ms-access-2010

我制作的 VBA 程序有问题。我想创建一个程序,该程序在整个表中输入 50,000 条记录(在我的例子中是 Employee 表),每次我尝试运行它时,它都会显示一个错误 "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

我将不胜感激对此问题的任何修复以及对我的代码的任何建议。

最佳答案

您已尝试声明 ( Dim ) 变量 EmployeeType作为 Variant 的数组然后你尝试将它(再次)声明为 String .

CompileError.png

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

关于vba - Access VBA 中的 "Duplicate declaration in current scope"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958574/

相关文章:

excel - 从单个单元格中提取以逗号分隔的数据

vba - 您能否帮助我更多地了解有关VBA错误处理的良好做法?

vba - 过滤表后范围类的删除方法失败

vba - 从 VBA 访问串行端口的最佳方法是什么?

java - "AFTER"不支持使用ucanaccess ALTER TABLE?

vba - 在 VBA 中根据可变单元格范围设置图表源数据

mysql - 微软 Access : Query to add "If" condition

vba - TreeView 在点击时与点击时的行为不同

java select 语句不起作用

sql - 计算并增加该记录的重复实例数的查询