1. ホーム
  2. vba

[解決済み] Access VBAで "Duplicate declaration in current scope "というエラーが発生する。

2022-02-26 20:33:46

質問

VBAで作ったプログラムに問題があります。あるテーブル(私の場合はEmployee Table)に5万件のレコードを入力するプログラムを作りたいのですが、実行するたびに次のようなエラーが表示されます。 "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 .

この2つの変数には、2つの異なる名前を使用する必要があります。