2014-09-21 49 views
2

我遇到了我所做的VBA程序的問題。我想創建一個程序,在整個表格中輸入50,000條記錄(這是我的情況下的員工表),每次我嘗試運行它時,都會顯示一條錯誤,說"Compile Error: Duplicate declaration in current scope."Access VBA中的「當前作用域中的重複聲明」錯誤

我的代碼如下:

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 

我將不勝感激任何修復該問題,對我的代碼的任何建議。

回答

3

您試圖聲明(Dim)變量EmployeeTypeVariant的數組,然後稍後嘗試將其聲明爲String

CompileError.png

你需要使用兩個不同的名字爲這兩個變量。